@@ -8,8 +8,8 @@ use std::{
8
8
} ;
9
9
10
10
use ide:: {
11
- CompletionResolveCapability , FileId , FilePosition , FileRange , HoverAction , HoverGotoTypeData ,
12
- ImportEdit , LineIndex , NavigationTarget , Query , RangeInfo , Runnable , RunnableKind , SearchScope ,
11
+ CompletionConfig , CompletionResolveCapability , FileId , FilePosition , FileRange , HoverAction ,
12
+ HoverGotoTypeData , NavigationTarget , Query , RangeInfo , Runnable , RunnableKind , SearchScope ,
13
13
TextEdit ,
14
14
} ;
15
15
use itertools:: Itertools ;
@@ -22,7 +22,7 @@ use lsp_types::{
22
22
HoverContents , Location , NumberOrString , Position , PrepareRenameResponse , Range , RenameParams ,
23
23
SemanticTokensDeltaParams , SemanticTokensFullDeltaResult , SemanticTokensParams ,
24
24
SemanticTokensRangeParams , SemanticTokensRangeResult , SemanticTokensResult , SymbolInformation ,
25
- SymbolTag , TextDocumentIdentifier , Url , WorkspaceEdit ,
25
+ SymbolTag , TextDocumentIdentifier , TextDocumentPositionParams , Url , WorkspaceEdit ,
26
26
} ;
27
27
use project_model:: TargetKind ;
28
28
use serde:: { Deserialize , Serialize } ;
@@ -35,7 +35,6 @@ use crate::{
35
35
config:: RustfmtConfig ,
36
36
from_json, from_proto,
37
37
global_state:: { GlobalState , GlobalStateSnapshot } ,
38
- line_endings:: LineEndings ,
39
38
lsp_ext:: { self , InlayHint , InlayHintsParams } ,
40
39
to_proto, LspError , Result ,
41
40
} ;
@@ -541,7 +540,7 @@ pub(crate) fn handle_completion(
541
540
params : lsp_types:: CompletionParams ,
542
541
) -> Result < Option < lsp_types:: CompletionResponse > > {
543
542
let _p = profile:: span ( "handle_completion" ) ;
544
- let text_document_url = params. text_document_position . text_document . uri . clone ( ) ;
543
+ let text_document_position = params. text_document_position . clone ( ) ;
545
544
let position = from_proto:: file_position ( & snap, params. text_document_position ) ?;
546
545
let completion_triggered_after_single_colon = {
547
546
let mut res = false ;
@@ -574,23 +573,18 @@ pub(crate) fn handle_completion(
574
573
575
574
let items: Vec < CompletionItem > = items
576
575
. into_iter ( )
577
- . enumerate ( )
578
- . flat_map ( |( item_index, item) | {
576
+ . flat_map ( |item| {
579
577
let mut new_completion_items =
580
578
to_proto:: completion_item ( & line_index, line_endings, item. clone ( ) ) ;
581
579
582
- if snap. config . completion . resolve_additional_edits_lazily ( ) {
583
- // TODO kb add resolve data somehow here
584
- if let Some ( import_edit) = item. import_to_add ( ) {
585
- // let data = serde_json::to_value(&CompletionData {
586
- // document_url: text_document_url.clone(),
587
- // import_id: item_index,
588
- // })
589
- // .expect(&format!("Should be able to serialize usize value {}", item_index));
590
- for new_item in & mut new_completion_items {
591
- // new_item.data = Some(data.clone());
592
- }
593
- }
580
+ for new_item in & mut new_completion_items {
581
+ let _ = fill_resolve_data (
582
+ & mut new_item. data ,
583
+ & item,
584
+ & snap. config . completion ,
585
+ & text_document_position,
586
+ )
587
+ . take ( ) ;
594
588
}
595
589
596
590
new_completion_items
@@ -603,8 +597,8 @@ pub(crate) fn handle_completion(
603
597
604
598
pub ( crate ) fn handle_completion_resolve (
605
599
snap : GlobalStateSnapshot ,
606
- mut original_completion : lsp_types :: CompletionItem ,
607
- ) -> Result < lsp_types :: CompletionItem > {
600
+ mut original_completion : CompletionItem ,
601
+ ) -> Result < CompletionItem > {
608
602
let _p = profile:: span ( "handle_resolve_completion" ) ;
609
603
610
604
// FIXME resolve the other capabilities also?
@@ -627,21 +621,30 @@ pub(crate) fn handle_completion_resolve(
627
621
None => return Ok ( original_completion) ,
628
622
} ;
629
623
630
- // TODO kb get the resolve data and somehow reparse the whole ast again?
631
- // let file_id = from_proto::file_id(&snap, &document_url)?;
632
- // let root = snap.analysis.parse(file_id)?;
633
-
634
- // if let Some(import_to_add) =
635
- // import_edit_ptr.and_then(|import_edit| import_edit.into_import_edit(root.syntax()))
636
- // {
637
- // // FIXME actually add all additional edits here? see `to_proto::completion_item` for more
638
- // append_import_edits(
639
- // &mut original_completion,
640
- // &import_to_add,
641
- // snap.analysis.file_line_index(file_id)?.as_ref(),
642
- // snap.file_line_endings(file_id),
643
- // );
644
- // }
624
+ let file_id = from_proto:: file_id ( & snap, & resolve_data. position . text_document . uri ) ?;
625
+ let line_index = snap. analysis . file_line_index ( file_id) ?;
626
+ let line_endings = snap. file_line_endings ( file_id) ;
627
+ let offset = from_proto:: offset ( & line_index, resolve_data. position . position ) ;
628
+
629
+ let mut additional_edits = snap
630
+ . analysis
631
+ . resolve_completion_edits (
632
+ & snap. config . completion ,
633
+ FilePosition { file_id, offset } ,
634
+ & resolve_data. full_import_path ,
635
+ & resolve_data. imported_name ,
636
+ ) ?
637
+ . into_iter ( )
638
+ . flat_map ( |edit| {
639
+ edit. into_iter ( ) . map ( |indel| to_proto:: text_edit ( & line_index, line_endings, indel) )
640
+ } )
641
+ . collect_vec ( ) ;
642
+
643
+ if let Some ( original_additional_edits) = original_completion. additional_text_edits . as_mut ( ) {
644
+ original_additional_edits. extend ( additional_edits. drain ( ..) )
645
+ } else {
646
+ original_completion. additional_text_edits = Some ( additional_edits) ;
647
+ }
645
648
646
649
Ok ( original_completion)
647
650
}
@@ -1606,27 +1609,30 @@ fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>)
1606
1609
1607
1610
#[ derive( Debug , Serialize , Deserialize ) ]
1608
1611
struct CompletionResolveData {
1609
- document_url : Url ,
1610
- import_id : usize ,
1612
+ position : lsp_types:: TextDocumentPositionParams ,
1613
+ full_import_path : String ,
1614
+ imported_name : String ,
1611
1615
}
1612
1616
1613
- fn append_import_edits (
1614
- completion : & mut lsp_types:: CompletionItem ,
1615
- import_to_add : & ImportEdit ,
1616
- line_index : & LineIndex ,
1617
- line_endings : LineEndings ,
1618
- ) {
1619
- let import_edits = import_to_add. to_text_edit ( ) . map ( |import_edit| {
1620
- import_edit
1621
- . into_iter ( )
1622
- . map ( |indel| to_proto:: text_edit ( line_index, line_endings, indel) )
1623
- . collect_vec ( )
1624
- } ) ;
1625
- if let Some ( original_additional_edits) = completion. additional_text_edits . as_mut ( ) {
1626
- if let Some ( mut new_edits) = import_edits {
1627
- original_additional_edits. extend ( new_edits. drain ( ..) )
1628
- }
1629
- } else {
1630
- completion. additional_text_edits = import_edits;
1617
+ fn fill_resolve_data (
1618
+ resolve_data : & mut Option < serde_json:: Value > ,
1619
+ item : & ide:: CompletionItem ,
1620
+ completion_config : & CompletionConfig ,
1621
+ position : & TextDocumentPositionParams ,
1622
+ ) -> Option < ( ) > {
1623
+ if completion_config. resolve_additional_edits_lazily ( ) {
1624
+ let import_edit = item. import_to_add ( ) ?;
1625
+ let full_import_path = import_edit. import_path . to_string ( ) ;
1626
+ let imported_name = import_edit. import_path . segments . clone ( ) . pop ( ) ?. to_string ( ) ;
1627
+
1628
+ * resolve_data = Some (
1629
+ serde_json:: to_value ( CompletionResolveData {
1630
+ position : position. to_owned ( ) ,
1631
+ full_import_path,
1632
+ imported_name,
1633
+ } )
1634
+ . expect ( "Failed to serialize a regular struct with derives" ) ,
1635
+ )
1631
1636
}
1637
+ Some ( ( ) )
1632
1638
}
0 commit comments