@@ -113,7 +113,8 @@ impl LanguageServer for Backend {
113113
114114 let trie_lock = self . trie . lock ( ) . await ;
115115 let words = trie_lock. suggest_completions ( & prefix) ;
116- words_to_completion_items ( words, & prefix, & mut completions) ;
116+ let suffixes = get_word_suffixes ( & current_line, position. character as i32 ) ;
117+ words_to_completion_items ( words, & suffixes, & mut completions) ;
117118
118119 let file_uri = params. text_document_position . text_document . uri . to_string ( ) ;
119120 let snippets = self . suggest_snippets ( & file_uri, & prefix) . await ;
@@ -174,14 +175,27 @@ fn get_word_prefix(current_line: &str, character: i32) -> String {
174175 prefix. iter ( ) . collect ( )
175176}
176177
178+ fn get_word_suffixes ( current_line : & str , character : i32 ) -> Vec < String > {
179+ let mut suffixes: Vec < String > = Vec :: new ( ) ;
180+ let line: Vec < char > = current_line. chars ( ) . collect ( ) ;
181+ let mut i = character. min ( line. len ( ) as i32 - 1 ) ;
182+ let mut current = Vec :: new ( ) ;
183+ while ( i as usize ) < line. len ( ) && valid_token_char ( line[ i as usize ] ) {
184+ current. push ( line[ i as usize ] ) ;
185+ suffixes. push ( current. iter ( ) . collect ( ) ) ;
186+ i += 1 ;
187+ }
188+ suffixes
189+ }
190+
177191fn words_to_completion_items (
178192 words : Vec < String > ,
179- prefix : & String ,
193+ suffixes : & Vec < String > ,
180194 completions : & mut Vec < CompletionItem > ,
181195) {
182196 let mut items: Vec < CompletionItem > = words
183197 . iter ( )
184- . filter ( |& word| word != prefix )
198+ . filter ( |& word| !suffixes . contains ( word ) )
185199 . map ( |word| CompletionItem {
186200 label : word. clone ( ) ,
187201 kind : Some ( CompletionItemKind :: TEXT ) ,
@@ -316,6 +330,36 @@ mod test {
316330 assert_eq ! ( "max" , prefix) ;
317331 }
318332
333+ #[ test]
334+ fn test_get_word_suffixes ( ) {
335+ let suffixes = get_word_suffixes ( " ios::sync" , 8 ) ;
336+ assert_eq ! ( vec![ "s" , "sy" , "syn" , "sync" ] , suffixes) ;
337+
338+ let suffixes = get_word_suffixes ( " int best = numeric_limits<int>::max();" , 14 ) ;
339+ assert_eq ! (
340+ vec![
341+ "n" ,
342+ "nu" ,
343+ "num" ,
344+ "nume" ,
345+ "numer" ,
346+ "numeri" ,
347+ "numeric" ,
348+ "numeric_" ,
349+ "numeric_l" ,
350+ "numeric_li" ,
351+ "numeric_lim" ,
352+ "numeric_limi" ,
353+ "numeric_limit" ,
354+ "numeric_limits"
355+ ] ,
356+ suffixes
357+ ) ;
358+
359+ let suffixes = get_word_suffixes ( " int best = numeric_limits<int>::max();" , 35 ) ;
360+ assert_eq ! ( vec![ "m" , "ma" , "max" ] , suffixes) ;
361+ }
362+
319363 #[ test]
320364 fn test_process_token ( ) {
321365 let tokens = process_token ( " aho_corasick(root.get())" , 2 ) ;
0 commit comments