@@ -32,7 +32,7 @@ use lsp_types::{
3232} ;
3333
3434use crate :: server:: { ELSResult , RedirectableStdout , Server } ;
35- use crate :: util:: { self , NormalizedUrl } ;
35+ use crate :: util:: { self , loc_to_pos , NormalizedUrl } ;
3636
3737fn comp_item_kind ( vi : & VarInfo ) -> CompletionItemKind {
3838 match & vi. t {
@@ -51,20 +51,21 @@ fn comp_item_kind(vi: &VarInfo) -> CompletionItemKind {
5151
5252#[ derive( Debug , PartialEq , Eq ) ]
5353pub enum CompletionKind {
54+ RetriggerLocal ,
5455 Local ,
55- Space ,
5656 LParen ,
5757 Method ,
58+ RetriggerMethod ,
5859 // Colon, // :, Type ascription or private access `::`
5960}
6061
6162impl CompletionKind {
6263 pub const fn should_be_local ( & self ) -> bool {
63- matches ! ( self , Self :: Local | Self :: Space | Self :: LParen )
64+ matches ! ( self , Self :: RetriggerLocal | Self :: Local | Self :: LParen )
6465 }
6566
6667 pub const fn should_be_method ( & self ) -> bool {
67- matches ! ( self , Self :: Method )
68+ matches ! ( self , Self :: Method | Self :: RetriggerMethod )
6869 }
6970
7071 pub const fn _is_lparen ( & self ) -> bool {
@@ -493,7 +494,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
493494 self . send_log ( format ! ( "completion requested: {params:?}" ) ) ?;
494495 let uri = NormalizedUrl :: new ( params. text_document_position . text_document . uri ) ;
495496 let path = util:: uri_to_path ( & uri) ;
496- let pos = params. text_document_position . position ;
497+ let mut pos = params. text_document_position . position ;
497498 // ignore comments
498499 // TODO: multiline comments
499500 if self
@@ -510,32 +511,39 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
510511 let comp_kind = match trigger {
511512 Some ( "." ) => CompletionKind :: Method ,
512513 Some ( ":" ) => CompletionKind :: Method ,
513- Some ( " " ) => CompletionKind :: Space ,
514+ Some ( " " ) => CompletionKind :: Local ,
514515 Some ( "(" ) => CompletionKind :: LParen ,
515- _ => CompletionKind :: Local ,
516+ _ => {
517+ let offset = match self . file_cache . get_token ( & uri, pos) . map ( |tk| tk. kind ) {
518+ Some ( TokenKind :: Newline | TokenKind :: EOF ) => -2 ,
519+ _ => -1 ,
520+ } ;
521+ let prev_token = self . file_cache . get_token_relatively ( & uri, pos, offset) ;
522+ match prev_token {
523+ Some ( prev) if matches ! ( prev. kind, Dot | DblColon ) => {
524+ if let Some ( p) = loc_to_pos ( prev. loc ( ) ) {
525+ pos = p;
526+ }
527+ CompletionKind :: RetriggerMethod
528+ }
529+ _ => CompletionKind :: RetriggerLocal ,
530+ }
531+ }
516532 } ;
517533 self . send_log ( format ! ( "CompletionKind: {comp_kind:?}" ) ) ?;
518534 let mut result: Vec < CompletionItem > = vec ! [ ] ;
519535 let mut already_appeared = Set :: new ( ) ;
520536 let contexts = if comp_kind. should_be_local ( ) {
521- let prev_token = self . file_cache . get_token_relatively ( & uri, pos, -1 ) ;
522- match prev_token {
523- Some ( prev) if matches ! ( prev. kind, Dot | DblColon ) => {
524- let Some ( dot_pos) = util:: loc_to_pos ( prev. loc ( ) ) else {
525- return Ok ( None ) ;
526- } ;
527- self . get_receiver_ctxs ( & uri, dot_pos) ?
528- }
529- _ => self . get_local_ctx ( & uri, pos) ,
530- }
537+ self . get_local_ctx ( & uri, pos)
531538 } else {
532539 self . get_receiver_ctxs ( & uri, pos) ?
533540 } ;
534541 let offset = match comp_kind {
535- CompletionKind :: Local => 0 ,
542+ CompletionKind :: RetriggerLocal => 0 ,
536543 CompletionKind :: Method => -1 ,
537- CompletionKind :: Space => -1 ,
544+ CompletionKind :: Local => -1 ,
538545 CompletionKind :: LParen => 0 ,
546+ CompletionKind :: RetriggerMethod => -1 ,
539547 } ;
540548 let arg_pt = self
541549 . get_min_expr ( & uri, pos, offset)
@@ -547,7 +555,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
547555 let nth = nth + additional;
548556 sig_t. non_default_params ( ) ?. get ( nth) . cloned ( )
549557 }
550- other if comp_kind == CompletionKind :: Space => {
558+ other if comp_kind == CompletionKind :: Local => {
551559 match other. show_acc ( ) . as_deref ( ) {
552560 Some ( "import" ) => {
553561 let insert = other
0 commit comments