import { filter as fuzzyFilter , match } from 'fuzzaldrin-plus' ; import nvim from '../workers/neovim-api' ; interface FilterResult { line : string ; start : { line : number ; column : number ; } ; end : { line : number ; column : number ; } ; } const buffers = new Map < string , string [ ] > ( ) ; const getLocations = ( str , query , buffer ) => { const line = buffer . indexOf ( str ) ; const locations = match ( str , query ) ; return { start : { line , column : locations [ 0 ] } , end : { line , column : locations [ locations . length - 1 ] } , } ; } ; const asFilterResults = ( results , lines , query ) : FilterResult [ ] => [ ... new Set ( results ) ] . map ( ( m ) => ( { line : m , ... getLocations ( m , query , lines ) , } ) ) ; export const fuzzy = async ( file , query , maxResults = 20 ) : Promise < FilterResult [ ] > => { const bufferData = buffers . get ( file ) || [ ] ; const results = fuzzyFilter ( bufferData , query , { maxResults } ) ; return asFilterResults ( results , bufferData , query ) ; } ; export const fuzzyVisible = async ( query ) : Promise < FilterResult [ ] > => { const { editorTopLine : start , editorBottomLine : end } = nvim . state ; const visibleLines = await ( await nvim . buffer ) . getLines ( { start , end : end + 1 , strictIndexing : true , } ) ; const results = fuzzyFilter ( visibleLines , query ) ; return asFilterResults ( results , visibleLines , query ) ; } ;