aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/completion/src/item.rs30
-rw-r--r--crates/completion/src/lib.rs5
-rw-r--r--crates/ide/src/lib.rs2
-rw-r--r--crates/ide_db/src/helpers/insert_use.rs32
-rw-r--r--crates/rust-analyzer/src/global_state.rs6
-rw-r--r--crates/rust-analyzer/src/handlers.rs65
-rw-r--r--crates/rust-analyzer/src/main_loop.rs4
7 files changed, 37 insertions, 107 deletions
diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs
index dd25ca75c..2dadf7e5b 100644
--- a/crates/completion/src/item.rs
+++ b/crates/completion/src/item.rs
@@ -4,10 +4,10 @@ use std::fmt;
4 4
5use hir::{Documentation, ModPath, Mutability}; 5use hir::{Documentation, ModPath, Mutability};
6use ide_db::helpers::{ 6use ide_db::helpers::{
7 insert_use::{self, ImportScope, ImportScopePtr, MergeBehaviour}, 7 insert_use::{self, ImportScope, MergeBehaviour},
8 mod_path_to_ast, 8 mod_path_to_ast,
9}; 9};
10use syntax::{algo, SyntaxNode, TextRange}; 10use syntax::{algo, TextRange};
11use text_edit::TextEdit; 11use text_edit::TextEdit;
12 12
13use crate::config::SnippetCap; 13use crate::config::SnippetCap;
@@ -275,32 +275,8 @@ pub struct ImportEdit {
275 pub merge_behaviour: Option<MergeBehaviour>, 275 pub merge_behaviour: Option<MergeBehaviour>,
276} 276}
277 277
278#[derive(Debug, Clone)]
279pub struct ImportEditPtr {
280 pub import_path: ModPath,
281 pub import_scope: ImportScopePtr,
282 pub merge_behaviour: Option<MergeBehaviour>,
283}
284
285impl ImportEditPtr {
286 pub fn into_import_edit(self, root: &SyntaxNode) -> Option<ImportEdit> {
287 Some(ImportEdit {
288 import_path: self.import_path,
289 import_scope: self.import_scope.into_scope(root)?,
290 merge_behaviour: self.merge_behaviour,
291 })
292 }
293}
294
295impl ImportEdit { 278impl ImportEdit {
296 pub fn get_edit_ptr(&self) -> ImportEditPtr { 279 // TODO kb remove this at all now, since it's used only once?
297 ImportEditPtr {
298 import_path: self.import_path.clone(),
299 import_scope: self.import_scope.get_ptr(),
300 merge_behaviour: self.merge_behaviour,
301 }
302 }
303
304 /// Attempts to insert the import to the given scope, producing a text edit. 280 /// Attempts to insert the import to the given scope, producing a text edit.
305 /// May return no edit in edge cases, such as scope already containing the import. 281 /// May return no edit in edge cases, such as scope already containing the import.
306 pub fn to_text_edit(&self) -> Option<TextEdit> { 282 pub fn to_text_edit(&self) -> Option<TextEdit> {
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs
index c277cd466..c57203c80 100644
--- a/crates/completion/src/lib.rs
+++ b/crates/completion/src/lib.rs
@@ -18,10 +18,7 @@ use crate::{completions::Completions, context::CompletionContext, item::Completi
18 18
19pub use crate::{ 19pub use crate::{
20 config::{CompletionConfig, CompletionResolveCapability}, 20 config::{CompletionConfig, CompletionResolveCapability},
21 item::{ 21 item::{CompletionItem, CompletionItemKind, CompletionScore, ImportEdit, InsertTextFormat},
22 CompletionItem, CompletionItemKind, CompletionScore, ImportEdit, ImportEditPtr,
23 InsertTextFormat,
24 },
25}; 22};
26 23
27//FIXME: split the following feature into fine-grained features. 24//FIXME: split the following feature into fine-grained features.
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index c52e53d27..9e38d6506 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -81,7 +81,7 @@ pub use crate::{
81}; 81};
82pub use completion::{ 82pub use completion::{
83 CompletionConfig, CompletionItem, CompletionItemKind, CompletionResolveCapability, 83 CompletionConfig, CompletionItem, CompletionItemKind, CompletionResolveCapability,
84 CompletionScore, ImportEdit, ImportEditPtr, InsertTextFormat, 84 CompletionScore, ImportEdit, InsertTextFormat,
85}; 85};
86pub use ide_db::{ 86pub use ide_db::{
87 call_info::CallInfo, 87 call_info::CallInfo,
diff --git a/crates/ide_db/src/helpers/insert_use.rs b/crates/ide_db/src/helpers/insert_use.rs
index 0dae9a541..040843990 100644
--- a/crates/ide_db/src/helpers/insert_use.rs
+++ b/crates/ide_db/src/helpers/insert_use.rs
@@ -11,7 +11,7 @@ use syntax::{
11 edit::{AstNodeEdit, IndentLevel}, 11 edit::{AstNodeEdit, IndentLevel},
12 make, AstNode, PathSegmentKind, VisibilityOwner, 12 make, AstNode, PathSegmentKind, VisibilityOwner,
13 }, 13 },
14 AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxNodePtr, SyntaxToken, 14 AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
15}; 15};
16use test_utils::mark; 16use test_utils::mark;
17 17
@@ -22,36 +22,6 @@ pub enum ImportScope {
22} 22}
23 23
24impl ImportScope { 24impl ImportScope {
25 pub fn get_ptr(&self) -> ImportScopePtr {
26 match self {
27 ImportScope::File(file) => ImportScopePtr::File(SyntaxNodePtr::new(file.syntax())),
28 ImportScope::Module(module) => {
29 ImportScopePtr::Module(SyntaxNodePtr::new(module.syntax()))
30 }
31 }
32 }
33}
34
35#[derive(Debug, Clone)]
36pub enum ImportScopePtr {
37 File(SyntaxNodePtr),
38 Module(SyntaxNodePtr),
39}
40
41impl ImportScopePtr {
42 pub fn into_scope(self, root: &SyntaxNode) -> Option<ImportScope> {
43 Some(match self {
44 ImportScopePtr::File(file_ptr) => {
45 ImportScope::File(ast::SourceFile::cast(file_ptr.to_node(root))?)
46 }
47 ImportScopePtr::Module(module_ptr) => {
48 ImportScope::File(ast::SourceFile::cast(module_ptr.to_node(root))?)
49 }
50 })
51 }
52}
53
54impl ImportScope {
55 pub fn from(syntax: SyntaxNode) -> Option<Self> { 25 pub fn from(syntax: SyntaxNode) -> Option<Self> {
56 if let Some(module) = ast::Module::cast(syntax.clone()) { 26 if let Some(module) = ast::Module::cast(syntax.clone()) {
57 module.item_list().map(ImportScope::Module) 27 module.item_list().map(ImportScope::Module)
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs
index 0fe69b996..a27495d0d 100644
--- a/crates/rust-analyzer/src/global_state.rs
+++ b/crates/rust-analyzer/src/global_state.rs
@@ -7,7 +7,7 @@ use std::{sync::Arc, time::Instant};
7 7
8use crossbeam_channel::{unbounded, Receiver, Sender}; 8use crossbeam_channel::{unbounded, Receiver, Sender};
9use flycheck::FlycheckHandle; 9use flycheck::FlycheckHandle;
10use ide::{Analysis, AnalysisHost, Change, FileId, ImportEditPtr}; 10use ide::{Analysis, AnalysisHost, Change, FileId};
11use ide_db::base_db::{CrateId, VfsPath}; 11use ide_db::base_db::{CrateId, VfsPath};
12use lsp_types::{SemanticTokens, Url}; 12use lsp_types::{SemanticTokens, Url};
13use parking_lot::{Mutex, RwLock}; 13use parking_lot::{Mutex, RwLock};
@@ -69,7 +69,6 @@ pub(crate) struct GlobalState {
69 pub(crate) config: Config, 69 pub(crate) config: Config,
70 pub(crate) analysis_host: AnalysisHost, 70 pub(crate) analysis_host: AnalysisHost,
71 pub(crate) diagnostics: DiagnosticCollection, 71 pub(crate) diagnostics: DiagnosticCollection,
72 pub(crate) completion_resolve_data: Arc<FxHashMap<usize, ImportEditPtr>>,
73 pub(crate) mem_docs: FxHashMap<VfsPath, DocumentData>, 72 pub(crate) mem_docs: FxHashMap<VfsPath, DocumentData>,
74 pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>, 73 pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
75 pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>, 74 pub(crate) vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
@@ -91,7 +90,6 @@ pub(crate) struct GlobalStateSnapshot {
91 pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>, 90 pub(crate) semantic_tokens_cache: Arc<Mutex<FxHashMap<Url, SemanticTokens>>>,
92 vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>, 91 vfs: Arc<RwLock<(vfs::Vfs, FxHashMap<FileId, LineEndings>)>>,
93 pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>, 92 pub(crate) workspaces: Arc<Vec<ProjectWorkspace>>,
94 pub(crate) completion_resolve_data: Arc<FxHashMap<usize, ImportEditPtr>>,
95} 93}
96 94
97impl GlobalState { 95impl GlobalState {
@@ -123,7 +121,6 @@ impl GlobalState {
123 config, 121 config,
124 analysis_host, 122 analysis_host,
125 diagnostics: Default::default(), 123 diagnostics: Default::default(),
126 completion_resolve_data: Arc::new(FxHashMap::default()),
127 mem_docs: FxHashMap::default(), 124 mem_docs: FxHashMap::default(),
128 semantic_tokens_cache: Arc::new(Default::default()), 125 semantic_tokens_cache: Arc::new(Default::default()),
129 vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))), 126 vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))),
@@ -194,7 +191,6 @@ impl GlobalState {
194 check_fixes: Arc::clone(&self.diagnostics.check_fixes), 191 check_fixes: Arc::clone(&self.diagnostics.check_fixes),
195 mem_docs: self.mem_docs.clone(), 192 mem_docs: self.mem_docs.clone(),
196 semantic_tokens_cache: Arc::clone(&self.semantic_tokens_cache), 193 semantic_tokens_cache: Arc::clone(&self.semantic_tokens_cache),
197 completion_resolve_data: Arc::clone(&self.completion_resolve_data),
198 } 194 }
199 } 195 }
200 196
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index 1ea1e1f43..dacd4ec50 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -5,7 +5,6 @@
5use std::{ 5use std::{
6 io::Write as _, 6 io::Write as _,
7 process::{self, Stdio}, 7 process::{self, Stdio},
8 sync::Arc,
9}; 8};
10 9
11use ide::{ 10use ide::{
@@ -26,7 +25,6 @@ use lsp_types::{
26 SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit, 25 SymbolTag, TextDocumentIdentifier, Url, WorkspaceEdit,
27}; 26};
28use project_model::TargetKind; 27use project_model::TargetKind;
29use rustc_hash::FxHashMap;
30use serde::{Deserialize, Serialize}; 28use serde::{Deserialize, Serialize};
31use serde_json::to_value; 29use serde_json::to_value;
32use stdx::{format_to, split_once}; 30use stdx::{format_to, split_once};
@@ -539,11 +537,10 @@ pub(crate) fn handle_runnables(
539} 537}
540 538
541pub(crate) fn handle_completion( 539pub(crate) fn handle_completion(
542 global_state: &mut GlobalState, 540 snap: GlobalStateSnapshot,
543 params: lsp_types::CompletionParams, 541 params: lsp_types::CompletionParams,
544) -> Result<Option<lsp_types::CompletionResponse>> { 542) -> Result<Option<lsp_types::CompletionResponse>> {
545 let _p = profile::span("handle_completion"); 543 let _p = profile::span("handle_completion");
546 let snap = global_state.snapshot();
547 let text_document_url = params.text_document_position.text_document.uri.clone(); 544 let text_document_url = params.text_document_position.text_document.uri.clone();
548 let position = from_proto::file_position(&snap, params.text_document_position)?; 545 let position = from_proto::file_position(&snap, params.text_document_position)?;
549 let completion_triggered_after_single_colon = { 546 let completion_triggered_after_single_colon = {
@@ -574,7 +571,6 @@ pub(crate) fn handle_completion(
574 }; 571 };
575 let line_index = snap.analysis.file_line_index(position.file_id)?; 572 let line_index = snap.analysis.file_line_index(position.file_id)?;
576 let line_endings = snap.file_line_endings(position.file_id); 573 let line_endings = snap.file_line_endings(position.file_id);
577 let mut completion_resolve_data = FxHashMap::default();
578 574
579 let items: Vec<CompletionItem> = items 575 let items: Vec<CompletionItem> = items
580 .into_iter() 576 .into_iter()
@@ -584,16 +580,15 @@ pub(crate) fn handle_completion(
584 to_proto::completion_item(&line_index, line_endings, item.clone()); 580 to_proto::completion_item(&line_index, line_endings, item.clone());
585 581
586 if snap.config.completion.resolve_additional_edits_lazily() { 582 if snap.config.completion.resolve_additional_edits_lazily() {
583 // TODO kb add resolve data somehow here
587 if let Some(import_edit) = item.import_to_add() { 584 if let Some(import_edit) = item.import_to_add() {
588 completion_resolve_data.insert(item_index, import_edit.get_edit_ptr()); 585 // let data = serde_json::to_value(&CompletionData {
589 586 // document_url: text_document_url.clone(),
590 let data = serde_json::to_value(&CompletionData { 587 // import_id: item_index,
591 document_url: text_document_url.clone(), 588 // })
592 import_id: item_index, 589 // .expect(&format!("Should be able to serialize usize value {}", item_index));
593 })
594 .expect(&format!("Should be able to serialize usize value {}", item_index));
595 for new_item in &mut new_completion_items { 590 for new_item in &mut new_completion_items {
596 new_item.data = Some(data.clone()); 591 // new_item.data = Some(data.clone());
597 } 592 }
598 } 593 }
599 } 594 }
@@ -602,8 +597,6 @@ pub(crate) fn handle_completion(
602 }) 597 })
603 .collect(); 598 .collect();
604 599
605 global_state.completion_resolve_data = Arc::new(completion_resolve_data);
606
607 let completion_list = lsp_types::CompletionList { is_incomplete: true, items }; 600 let completion_list = lsp_types::CompletionList { is_incomplete: true, items };
608 Ok(Some(completion_list.into())) 601 Ok(Some(completion_list.into()))
609} 602}
@@ -624,33 +617,31 @@ pub(crate) fn handle_completion_resolve(
624 return Ok(original_completion); 617 return Ok(original_completion);
625 } 618 }
626 619
627 let (import_edit_ptr, document_url) = match original_completion 620 let resolve_data = match original_completion
628 .data 621 .data
629 .as_ref() 622 .take()
630 .map(|data| serde_json::from_value::<CompletionData>(data.clone())) 623 .map(|data| serde_json::from_value::<CompletionResolveData>(data))
631 .transpose()? 624 .transpose()?
632 .and_then(|data| { 625 {
633 let import_edit_ptr = snap.completion_resolve_data.get(&data.import_id).cloned();
634 Some((import_edit_ptr, data.document_url))
635 }) {
636 Some(data) => data, 626 Some(data) => data,
637 None => return Ok(original_completion), 627 None => return Ok(original_completion),
638 }; 628 };
639 629
640 let file_id = from_proto::file_id(&snap, &document_url)?; 630 // TODO kb get the resolve data and somehow reparse the whole ast again?
641 let root = snap.analysis.parse(file_id)?; 631 // let file_id = from_proto::file_id(&snap, &document_url)?;
642 632 // let root = snap.analysis.parse(file_id)?;
643 if let Some(import_to_add) = 633
644 import_edit_ptr.and_then(|import_edit| import_edit.into_import_edit(root.syntax())) 634 // if let Some(import_to_add) =
645 { 635 // import_edit_ptr.and_then(|import_edit| import_edit.into_import_edit(root.syntax()))
646 // FIXME actually add all additional edits here? see `to_proto::completion_item` for more 636 // {
647 append_import_edits( 637 // // FIXME actually add all additional edits here? see `to_proto::completion_item` for more
648 &mut original_completion, 638 // append_import_edits(
649 &import_to_add, 639 // &mut original_completion,
650 snap.analysis.file_line_index(file_id)?.as_ref(), 640 // &import_to_add,
651 snap.file_line_endings(file_id), 641 // snap.analysis.file_line_index(file_id)?.as_ref(),
652 ); 642 // snap.file_line_endings(file_id),
653 } 643 // );
644 // }
654 645
655 Ok(original_completion) 646 Ok(original_completion)
656} 647}
@@ -1614,7 +1605,7 @@ fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>)
1614} 1605}
1615 1606
1616#[derive(Debug, Serialize, Deserialize)] 1607#[derive(Debug, Serialize, Deserialize)]
1617struct CompletionData { 1608struct CompletionResolveData {
1618 document_url: Url, 1609 document_url: Url,
1619 import_id: usize, 1610 import_id: usize,
1620} 1611}
diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs
index aad37fde1..95be2ebd3 100644
--- a/crates/rust-analyzer/src/main_loop.rs
+++ b/crates/rust-analyzer/src/main_loop.rs
@@ -436,8 +436,6 @@ impl GlobalState {
436 handlers::handle_matching_brace(s.snapshot(), p) 436 handlers::handle_matching_brace(s.snapshot(), p)
437 })? 437 })?
438 .on_sync::<lsp_ext::MemoryUsage>(|s, p| handlers::handle_memory_usage(s, p))? 438 .on_sync::<lsp_ext::MemoryUsage>(|s, p| handlers::handle_memory_usage(s, p))?
439 .on_sync::<lsp_types::request::Completion>(handlers::handle_completion)?
440 .on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_completion_resolve)
441 .on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status) 439 .on::<lsp_ext::AnalyzerStatus>(handlers::handle_analyzer_status)
442 .on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree) 440 .on::<lsp_ext::SyntaxTree>(handlers::handle_syntax_tree)
443 .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro) 441 .on::<lsp_ext::ExpandMacro>(handlers::handle_expand_macro)
@@ -455,6 +453,8 @@ impl GlobalState {
455 .on::<lsp_types::request::GotoDefinition>(handlers::handle_goto_definition) 453 .on::<lsp_types::request::GotoDefinition>(handlers::handle_goto_definition)
456 .on::<lsp_types::request::GotoImplementation>(handlers::handle_goto_implementation) 454 .on::<lsp_types::request::GotoImplementation>(handlers::handle_goto_implementation)
457 .on::<lsp_types::request::GotoTypeDefinition>(handlers::handle_goto_type_definition) 455 .on::<lsp_types::request::GotoTypeDefinition>(handlers::handle_goto_type_definition)
456 .on::<lsp_types::request::Completion>(handlers::handle_completion)
457 .on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_completion_resolve)
458 .on::<lsp_types::request::CodeLensRequest>(handlers::handle_code_lens) 458 .on::<lsp_types::request::CodeLensRequest>(handlers::handle_code_lens)
459 .on::<lsp_types::request::CodeLensResolve>(handlers::handle_code_lens_resolve) 459 .on::<lsp_types::request::CodeLensResolve>(handlers::handle_code_lens_resolve)
460 .on::<lsp_types::request::FoldingRangeRequest>(handlers::handle_folding_range) 460 .on::<lsp_types::request::FoldingRangeRequest>(handlers::handle_folding_range)