import { CompletrSettings , WordInsertionMode } from "../settings" ; import { SuggestionContext , SuggestionProvider } from "./provider" ; export abstract class DictionaryProvider implements SuggestionProvider { abstract readonly wordMap : Map < string , Iterable < string > > ; abstract isEnabled ( settings ) ; getSuggestions ( context , settings ) { if ( ! this . isEnabled ( settings ) || ! context . query ) return [ ] ; const ignoreCase = settings . wordInsertionMode != WordInsertionMode . MATCH_CASE_REPLACE ; const query = ignoreCase ? context . query . toLowerCase ( ) : context . query ; const firstChar = query . charAt ( 0 ) ; const list = ignoreCase ? [ ( this . wordMap . get ( firstChar ) ?? [ ] ) , ( this . wordMap . get ( firstChar . toUpperCase ( ) ) ?? [ ] ) ] : [ this . wordMap . get ( firstChar ) ] ; if ( ! list || list . length < 1 ) return [ ] ; let result = [ ] ; for ( let el of list ) { result = [ ... result , ... filterIntoArray ( el , s => { const match = ignoreCase ? s . toLowerCase ( ) : s ; return match . startsWith ( query ) ; } ) ] ; } result = result . sort ( ( a , b ) => a . length - b . length ) ; if ( settings . wordInsertionMode === WordInsertionMode . IGNORE_CASE_APPEND ) { result = result . map ( s => query + s . substring ( query . length , s . length ) ) ; } return result ; } } function filterIntoArray < T > ( iterable , predicate ) { const result = [ ] ; for ( let val of iterable ) { if ( ! predicate ( val ) ) continue ; result . push ( val ) ; } return result ; }