@@ -5,11 +5,11 @@ use ide_db::FxHashMap;
55use syntax:: {
66 algo,
77 ast:: { self , HasModuleItem } ,
8- match_ast, AstNode , Direction , SyntaxKind , TextRange ,
8+ match_ast, AstNode , Direction , SyntaxKind , TextRange , TextSize ,
99} ;
1010
1111use crate :: {
12- context:: { ParamKind , PatternContext } ,
12+ context:: { ParamContext , ParamKind , PatternContext } ,
1313 CompletionContext , CompletionItem , CompletionItemKind , Completions ,
1414} ;
1515
@@ -24,7 +24,7 @@ pub(crate) fn complete_fn_param(
2424 ctx : & CompletionContext ,
2525 pattern_ctx : & PatternContext ,
2626) -> Option < ( ) > {
27- let ( ( param_list, _ , param_kind ) , impl_) = match pattern_ctx {
27+ let ( ParamContext { param_list, kind , .. } , impl_) = match pattern_ctx {
2828 PatternContext { param_ctx : Some ( kind) , impl_, .. } => ( kind, impl_) ,
2929 _ => return None ,
3030 } ;
@@ -43,7 +43,7 @@ pub(crate) fn complete_fn_param(
4343 item. add_to ( acc)
4444 } ;
4545
46- match param_kind {
46+ match kind {
4747 ParamKind :: Function ( function) => {
4848 fill_fn_params ( ctx, function, param_list, impl_, add_new_item_to_acc) ;
4949 }
@@ -105,7 +105,7 @@ fn fill_fn_params(
105105 }
106106 remove_duplicated ( & mut file_params, param_list. params ( ) ) ;
107107 let self_completion_items = [ "self" , "&self" , "mut self" , "&mut self" ] ;
108- if should_add_self_completions ( param_list, impl_) {
108+ if should_add_self_completions ( ctx . token . text_range ( ) . start ( ) , param_list, impl_) {
109109 self_completion_items. into_iter ( ) . for_each ( |self_item| add_new_item_to_acc ( self_item) ) ;
110110 }
111111
@@ -156,10 +156,18 @@ fn remove_duplicated(
156156 } )
157157}
158158
159- fn should_add_self_completions ( param_list : & ast:: ParamList , impl_ : & Option < ast:: Impl > ) -> bool {
160- let no_params = param_list. params ( ) . next ( ) . is_none ( ) && param_list. self_param ( ) . is_none ( ) ;
161-
162- impl_. is_some ( ) && no_params
159+ fn should_add_self_completions (
160+ cursor : TextSize ,
161+ param_list : & ast:: ParamList ,
162+ impl_ : & Option < ast:: Impl > ,
163+ ) -> bool {
164+ if impl_. is_none ( ) || param_list. self_param ( ) . is_some ( ) {
165+ return false ;
166+ }
167+ match param_list. params ( ) . next ( ) {
168+ Some ( first) => first. pat ( ) . map_or ( false , |pat| pat. syntax ( ) . text_range ( ) . contains ( cursor) ) ,
169+ None => true ,
170+ }
163171}
164172
165173fn comma_wrapper ( ctx : & CompletionContext ) -> Option < ( impl Fn ( & str ) -> String , TextRange ) > {
0 commit comments