aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/completion/src/completions/unqualified_path.rs8
-rw-r--r--crates/completion/src/config.rs22
-rw-r--r--crates/completion/src/lib.rs2
-rw-r--r--crates/ide/src/lib.rs4
-rw-r--r--crates/rust-analyzer/src/caps.rs46
-rw-r--r--crates/rust-analyzer/src/config.rs7
-rw-r--r--crates/rust-analyzer/src/handlers.rs21
7 files changed, 32 insertions, 78 deletions
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs
index 2f41a3f96..896f167ff 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -46,7 +46,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
46 acc.add_resolution(ctx, name.to_string(), &res) 46 acc.add_resolution(ctx, name.to_string(), &res)
47 }); 47 });
48 48
49 if ctx.config.enable_autoimport_completions && ctx.config.resolve_additional_edits_lazily() { 49 if ctx.config.enable_autoimport_completions {
50 fuzzy_completion(acc, ctx); 50 fuzzy_completion(acc, ctx);
51 } 51 }
52} 52}
@@ -206,11 +206,7 @@ mod tests {
206 } 206 }
207 207
208 fn fuzzy_completion_config() -> CompletionConfig { 208 fn fuzzy_completion_config() -> CompletionConfig {
209 let mut completion_config = CompletionConfig::default(); 209 CompletionConfig::default()
210 completion_config
211 .active_resolve_capabilities
212 .insert(crate::CompletionResolveCapability::AdditionalTextEdits);
213 completion_config
214 } 210 }
215 211
216 #[test] 212 #[test]
diff --git a/crates/completion/src/config.rs b/crates/completion/src/config.rs
index 30577dc11..9f82b0346 100644
--- a/crates/completion/src/config.rs
+++ b/crates/completion/src/config.rs
@@ -5,7 +5,6 @@
5//! completions if we are allowed to. 5//! completions if we are allowed to.
6 6
7use ide_db::helpers::insert_use::MergeBehavior; 7use ide_db::helpers::insert_use::MergeBehavior;
8use rustc_hash::FxHashSet;
9 8
10#[derive(Clone, Debug, PartialEq, Eq)] 9#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct CompletionConfig { 10pub struct CompletionConfig {
@@ -15,32 +14,12 @@ pub struct CompletionConfig {
15 pub add_call_argument_snippets: bool, 14 pub add_call_argument_snippets: bool,
16 pub snippet_cap: Option<SnippetCap>, 15 pub snippet_cap: Option<SnippetCap>,
17 pub merge: Option<MergeBehavior>, 16 pub merge: Option<MergeBehavior>,
18 /// A set of capabilities, enabled on the client and supported on the server.
19 pub active_resolve_capabilities: FxHashSet<CompletionResolveCapability>,
20}
21
22/// A resolve capability, supported on the server.
23/// If the client registers any completion resolve capabilities,
24/// the server is able to render completion items' corresponding fields later,
25/// not during an initial completion item request.
26/// See https://github.com/rust-analyzer/rust-analyzer/issues/6366 for more details.
27#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
28pub enum CompletionResolveCapability {
29 Documentation,
30 Detail,
31 AdditionalTextEdits,
32} 17}
33 18
34impl CompletionConfig { 19impl CompletionConfig {
35 pub fn allow_snippets(&mut self, yes: bool) { 20 pub fn allow_snippets(&mut self, yes: bool) {
36 self.snippet_cap = if yes { Some(SnippetCap { _private: () }) } else { None } 21 self.snippet_cap = if yes { Some(SnippetCap { _private: () }) } else { None }
37 } 22 }
38
39 /// Whether the completions' additional edits are calculated when sending an initional completions list
40 /// or later, in a separate resolve request.
41 pub fn resolve_additional_edits_lazily(&self) -> bool {
42 self.active_resolve_capabilities.contains(&CompletionResolveCapability::AdditionalTextEdits)
43 }
44} 23}
45 24
46#[derive(Clone, Copy, Debug, PartialEq, Eq)] 25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -57,7 +36,6 @@ impl Default for CompletionConfig {
57 add_call_argument_snippets: true, 36 add_call_argument_snippets: true,
58 snippet_cap: Some(SnippetCap { _private: () }), 37 snippet_cap: Some(SnippetCap { _private: () }),
59 merge: Some(MergeBehavior::Full), 38 merge: Some(MergeBehavior::Full),
60 active_resolve_capabilities: FxHashSet::default(),
61 } 39 }
62 } 40 }
63} 41}
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs
index c57d05bbe..366aced71 100644
--- a/crates/completion/src/lib.rs
+++ b/crates/completion/src/lib.rs
@@ -20,7 +20,7 @@ use text_edit::TextEdit;
20use crate::{completions::Completions, context::CompletionContext, item::CompletionKind}; 20use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
21 21
22pub use crate::{ 22pub use crate::{
23 config::{CompletionConfig, CompletionResolveCapability}, 23 config::CompletionConfig,
24 item::{CompletionItem, CompletionItemKind, CompletionScore, ImportEdit, InsertTextFormat}, 24 item::{CompletionItem, CompletionItemKind, CompletionScore, ImportEdit, InsertTextFormat},
25}; 25};
26 26
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index a450794f3..72c8bfd09 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -82,8 +82,8 @@ pub use crate::{
82}; 82};
83pub use assists::{Assist, AssistConfig, AssistId, AssistKind}; 83pub use assists::{Assist, AssistConfig, AssistId, AssistKind};
84pub use completion::{ 84pub use completion::{
85 CompletionConfig, CompletionItem, CompletionItemKind, CompletionResolveCapability, 85 CompletionConfig, CompletionItem, CompletionItemKind, CompletionScore, ImportEdit,
86 CompletionScore, ImportEdit, InsertTextFormat, 86 InsertTextFormat,
87}; 87};
88pub use hir::{Documentation, Semantics}; 88pub use hir::{Documentation, Semantics};
89pub use ide_db::base_db::{ 89pub use ide_db::base_db::{
diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs
index 80e46bf7f..3db0d55c5 100644
--- a/crates/rust-analyzer/src/caps.rs
+++ b/crates/rust-analyzer/src/caps.rs
@@ -1,7 +1,6 @@
1//! Advertizes the capabilities of the LSP Server. 1//! Advertizes the capabilities of the LSP Server.
2use std::env; 2use std::env;
3 3
4use ide::CompletionResolveCapability;
5use lsp_types::{ 4use lsp_types::{
6 CallHierarchyServerCapability, ClientCapabilities, CodeActionKind, CodeActionOptions, 5 CallHierarchyServerCapability, ClientCapabilities, CodeActionKind, CodeActionOptions,
7 CodeActionProviderCapability, CodeLensOptions, CompletionOptions, 6 CodeActionProviderCapability, CodeLensOptions, CompletionOptions,
@@ -14,7 +13,6 @@ use lsp_types::{
14 WorkDoneProgressOptions, WorkspaceFileOperationsServerCapabilities, 13 WorkDoneProgressOptions, WorkspaceFileOperationsServerCapabilities,
15 WorkspaceServerCapabilities, 14 WorkspaceServerCapabilities,
16}; 15};
17use rustc_hash::FxHashSet;
18use serde_json::json; 16use serde_json::json;
19 17
20use crate::semantic_tokens; 18use crate::semantic_tokens;
@@ -118,37 +116,31 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti
118} 116}
119 117
120fn completions_resolve_provider(client_caps: &ClientCapabilities) -> Option<bool> { 118fn completions_resolve_provider(client_caps: &ClientCapabilities) -> Option<bool> {
121 if enabled_completions_resolve_capabilities(client_caps)?.is_empty() { 119 if completion_item_edit_resolve(client_caps) {
120 Some(true)
121 } else {
122 log::info!("No `additionalTextEdits` completion resolve capability was found in the client capabilities, autoimport completion is disabled"); 122 log::info!("No `additionalTextEdits` completion resolve capability was found in the client capabilities, autoimport completion is disabled");
123 None 123 None
124 } else {
125 Some(true)
126 } 124 }
127} 125}
128 126
129/// Parses client capabilities and returns all completion resolve capabilities rust-analyzer supports. 127/// Parses client capabilities and returns all completion resolve capabilities rust-analyzer supports.
130pub(crate) fn enabled_completions_resolve_capabilities( 128pub(crate) fn completion_item_edit_resolve(caps: &ClientCapabilities) -> bool {
131 caps: &ClientCapabilities, 129 (|| {
132) -> Option<FxHashSet<CompletionResolveCapability>> { 130 Some(
133 Some( 131 caps.text_document
134 caps.text_document 132 .as_ref()?
135 .as_ref()? 133 .completion
136 .completion 134 .as_ref()?
137 .as_ref()? 135 .completion_item
138 .completion_item 136 .as_ref()?
139 .as_ref()? 137 .resolve_support
140 .resolve_support 138 .as_ref()?
141 .as_ref()? 139 .properties
142 .properties 140 .iter()
143 .iter() 141 .any(|cap_string| cap_string.as_str() == "additionalTextEdits"),
144 .filter_map(|cap_string| match cap_string.as_str() { 142 )
145 "additionalTextEdits" => Some(CompletionResolveCapability::AdditionalTextEdits), 143 })() == Some(true)
146 "detail" => Some(CompletionResolveCapability::Detail),
147 "documentation" => Some(CompletionResolveCapability::Documentation),
148 _unsupported => None,
149 })
150 .collect(),
151 )
152} 144}
153 145
154fn code_action_capabilities(client_caps: &ClientCapabilities) -> CodeActionProviderCapability { 146fn code_action_capabilities(client_caps: &ClientCapabilities) -> CodeActionProviderCapability {
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index 24e7936fc..ce9526315 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -20,7 +20,7 @@ use rustc_hash::FxHashSet;
20use serde::{de::DeserializeOwned, Deserialize}; 20use serde::{de::DeserializeOwned, Deserialize};
21use vfs::AbsPathBuf; 21use vfs::AbsPathBuf;
22 22
23use crate::{caps::enabled_completions_resolve_capabilities, diagnostics::DiagnosticsMapConfig}; 23use crate::{caps::completion_item_edit_resolve, diagnostics::DiagnosticsMapConfig};
24 24
25config_data! { 25config_data! {
26 struct ConfigData { 26 struct ConfigData {
@@ -536,12 +536,11 @@ impl Config {
536 pub fn completion(&self) -> CompletionConfig { 536 pub fn completion(&self) -> CompletionConfig {
537 let mut res = CompletionConfig::default(); 537 let mut res = CompletionConfig::default();
538 res.enable_postfix_completions = self.data.completion_postfix_enable; 538 res.enable_postfix_completions = self.data.completion_postfix_enable;
539 res.enable_autoimport_completions = self.data.completion_autoimport_enable; 539 res.enable_autoimport_completions =
540 self.data.completion_autoimport_enable && completion_item_edit_resolve(&self.caps);
540 res.add_call_parenthesis = self.data.completion_addCallParenthesis; 541 res.add_call_parenthesis = self.data.completion_addCallParenthesis;
541 res.add_call_argument_snippets = self.data.completion_addCallArgumentSnippets; 542 res.add_call_argument_snippets = self.data.completion_addCallArgumentSnippets;
542 res.merge = self.merge_behavior(); 543 res.merge = self.merge_behavior();
543 res.active_resolve_capabilities =
544 enabled_completions_resolve_capabilities(&self.caps).unwrap_or_default();
545 544
546 res.allow_snippets(try_or!( 545 res.allow_snippets(try_or!(
547 self.caps 546 self.caps
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs
index 33661325a..a7bf0ec6a 100644
--- a/crates/rust-analyzer/src/handlers.rs
+++ b/crates/rust-analyzer/src/handlers.rs
@@ -9,9 +9,8 @@ use std::{
9}; 9};
10 10
11use ide::{ 11use ide::{
12 CompletionResolveCapability, FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, 12 FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, LineIndex, NavigationTarget,
13 LineIndex, NavigationTarget, Query, RangeInfo, Runnable, RunnableKind, SearchScope, 13 Query, RangeInfo, Runnable, RunnableKind, SearchScope, SourceChange, SymbolKind, TextEdit,
14 SourceChange, SymbolKind, TextEdit,
15}; 14};
16use itertools::Itertools; 15use itertools::Itertools;
17use lsp_server::ErrorCode; 16use lsp_server::ErrorCode;
@@ -634,10 +633,9 @@ pub(crate) fn handle_completion(
634 let mut new_completion_items = 633 let mut new_completion_items =
635 to_proto::completion_item(&line_index, line_endings, item.clone()); 634 to_proto::completion_item(&line_index, line_endings, item.clone());
636 635
637 if completion_config.resolve_additional_edits_lazily() { 636 if completion_config.enable_autoimport_completions {
638 for new_item in &mut new_completion_items { 637 for new_item in &mut new_completion_items {
639 let _ = fill_resolve_data(&mut new_item.data, &item, &text_document_position) 638 fill_resolve_data(&mut new_item.data, &item, &text_document_position);
640 .take();
641 } 639 }
642 } 640 }
643 641
@@ -663,15 +661,6 @@ pub(crate) fn handle_completion_resolve(
663 .into()); 661 .into());
664 } 662 }
665 663
666 // FIXME resolve the other capabilities also?
667 let completion_config = &snap.config.completion();
668 if !completion_config
669 .active_resolve_capabilities
670 .contains(&CompletionResolveCapability::AdditionalTextEdits)
671 {
672 return Ok(original_completion);
673 }
674
675 let resolve_data = match original_completion 664 let resolve_data = match original_completion
676 .data 665 .data
677 .take() 666 .take()
@@ -690,7 +679,7 @@ pub(crate) fn handle_completion_resolve(
690 let additional_edits = snap 679 let additional_edits = snap
691 .analysis 680 .analysis
692 .resolve_completion_edits( 681 .resolve_completion_edits(
693 &completion_config, 682 &snap.config.completion(),
694 FilePosition { file_id, offset }, 683 FilePosition { file_id, offset },
695 &resolve_data.full_import_path, 684 &resolve_data.full_import_path,
696 resolve_data.imported_name, 685 resolve_data.imported_name,