11use async_lsp:: lsp_types:: {
2- CompletionItem , CompletionItemKind , CompletionItemLabelDetails ,
3- InsertTextFormat , InsertTextMode , Position ,
2+ CompletionContext , CompletionItem , CompletionItemKind ,
3+ CompletionItemLabelDetails , CompletionTriggerKind , InsertTextFormat ,
4+ InsertTextMode , Position ,
45} ;
56use itertools:: Itertools ;
67
@@ -76,6 +77,7 @@ const CONDITION_SUGGESTIONS: [(&str, Option<&str>); 16] = [
7677pub fn completion (
7778 document : & Document ,
7879 pos : Position ,
80+ context : Option < CompletionContext > ,
7981) -> Option < Vec < CompletionItem > > {
8082 let cst = & document. cst ;
8183 // Get the token before cursor. There might be no token at cursor when the
@@ -84,13 +86,29 @@ pub fn completion(
8486 . and_then ( |token| token. prev_token ( ) )
8587 . or_else ( || cst. root ( ) . last_token ( ) ) ?;
8688
89+ // Trigger characters are: `.`, `!`, `$`, `@`, `#`.
90+ let is_trigger_character = context. is_some_and ( |ctx| {
91+ ctx. trigger_kind == CompletionTriggerKind :: TRIGGER_CHARACTER
92+ } ) ;
93+
8794 // If the token is a direct child of `SOURCE_FILE`, return top-level suggestions.
88- if non_error_parent ( & token) ?. kind ( ) == SyntaxKind :: SOURCE_FILE {
95+ if !is_trigger_character
96+ && non_error_parent ( & token) ?. kind ( ) == SyntaxKind :: SOURCE_FILE
97+ {
8998 return Some ( source_file_suggestions ( ) ) ;
9099 }
91100
92101 let prev_token = prev_non_trivia_token ( & token) ?;
93102
103+ if prev_token. ancestors ( ) . any ( |n| n. kind ( ) == SyntaxKind :: CONDITION_BLK ) {
104+ return condition_suggestions ( cst, token) ;
105+ }
106+
107+ // Trigger characters are recognized in the condition block only.
108+ if is_trigger_character {
109+ return Some ( vec ! [ ] ) ;
110+ }
111+
94112 if prev_token. kind ( ) == SyntaxKind :: IMPORT_KW {
95113 #[ cfg( feature = "full-compiler" ) ]
96114 return Some ( import_suggestions ( ) ) ;
@@ -104,10 +122,6 @@ pub fn completion(
104122 return Some ( pattern_modifier_suggestions ( pattern_def) ) ;
105123 }
106124
107- if prev_token. ancestors ( ) . any ( |n| n. kind ( ) == SyntaxKind :: CONDITION_BLK ) {
108- return condition_suggestions ( cst, token) ;
109- }
110-
111125 if prev_token. ancestors ( ) . any ( |n| n. kind ( ) == SyntaxKind :: RULE_DECL ) {
112126 return Some ( rule_suggestions ( ) ) ;
113127 }
@@ -192,6 +206,8 @@ fn condition_suggestions(
192206 }
193207 }
194208 }
209+ // Do not propose keywords for condition block after a dot.
210+ SyntaxKind :: DOT => { }
195211 _ => {
196212 CONDITION_SUGGESTIONS . iter ( ) . for_each ( |( kw, insert) | {
197213 result. push ( CompletionItem {
0 commit comments