mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Remove redundant code
This commit is contained in:
parent
50e06ee95a
commit
68a747efe0
8 changed files with 79 additions and 102 deletions
|
@ -15,12 +15,12 @@ pub struct CompletionConfig {
|
|||
pub add_call_argument_snippets: bool,
|
||||
pub snippet_cap: Option<SnippetCap>,
|
||||
pub merge: Option<MergeBehaviour>,
|
||||
/// A set of capabilities, enabled on the cliend and supported on the server.
|
||||
pub resolve_capabilities: FxHashSet<CompletionResolveCapability>,
|
||||
/// A set of capabilities, enabled on the client and supported on the server.
|
||||
pub active_resolve_capabilities: FxHashSet<CompletionResolveCapability>,
|
||||
}
|
||||
|
||||
/// A resolve capability, supported on a server.
|
||||
/// If the client registers any of those in its completion resolve capabilities,
|
||||
/// A resolve capability, supported on the server.
|
||||
/// If the client registers any completion resolve capabilities,
|
||||
/// the server is able to render completion items' corresponding fields later,
|
||||
/// not during an initial completion item request.
|
||||
/// See https://github.com/rust-analyzer/rust-analyzer/issues/6366 for more details.
|
||||
|
@ -37,8 +37,11 @@ impl CompletionConfig {
|
|||
}
|
||||
|
||||
/// Whether the completions' additional edits are calculated later, during a resolve request or not.
|
||||
pub fn should_resolve_additional_edits_immediately(&self) -> bool {
|
||||
!self.resolve_capabilities.contains(&CompletionResolveCapability::AdditionalTextEdits)
|
||||
/// See `CompletionResolveCapability` for the details.
|
||||
pub fn resolve_edits_immediately(&self) -> bool {
|
||||
!self
|
||||
.active_resolve_capabilities
|
||||
.contains(&CompletionResolveCapability::AdditionalTextEdits)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,7 +59,7 @@ impl Default for CompletionConfig {
|
|||
add_call_argument_snippets: true,
|
||||
snippet_cap: Some(SnippetCap { _private: () }),
|
||||
merge: Some(MergeBehaviour::Full),
|
||||
resolve_capabilities: FxHashSet::default(),
|
||||
active_resolve_capabilities: FxHashSet::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -194,10 +194,7 @@ impl<'a> Render<'a> {
|
|||
local_name,
|
||||
)
|
||||
.kind(CompletionItemKind::UnresolvedReference)
|
||||
.add_import(
|
||||
import_to_add,
|
||||
self.ctx.completion.config.should_resolve_additional_edits_immediately(),
|
||||
)
|
||||
.add_import(import_to_add, self.ctx.completion.config.resolve_edits_immediately())
|
||||
.build();
|
||||
return Some(item);
|
||||
}
|
||||
|
@ -252,10 +249,7 @@ impl<'a> Render<'a> {
|
|||
|
||||
let item = item
|
||||
.kind(kind)
|
||||
.add_import(
|
||||
import_to_add,
|
||||
self.ctx.completion.config.should_resolve_additional_edits_immediately(),
|
||||
)
|
||||
.add_import(import_to_add, self.ctx.completion.config.resolve_edits_immediately())
|
||||
.set_documentation(docs)
|
||||
.set_ref_match(ref_match)
|
||||
.build();
|
||||
|
|
|
@ -71,10 +71,7 @@ impl<'a> EnumVariantRender<'a> {
|
|||
.kind(CompletionItemKind::EnumVariant)
|
||||
.set_documentation(self.variant.docs(self.ctx.db()))
|
||||
.set_deprecated(self.ctx.is_deprecated(self.variant))
|
||||
.add_import(
|
||||
import_to_add,
|
||||
self.ctx.completion.config.should_resolve_additional_edits_immediately(),
|
||||
)
|
||||
.add_import(import_to_add, self.ctx.completion.config.resolve_edits_immediately())
|
||||
.detail(self.detail());
|
||||
|
||||
if self.variant_kind == StructKind::Tuple {
|
||||
|
|
|
@ -47,10 +47,7 @@ impl<'a> FunctionRender<'a> {
|
|||
.set_deprecated(self.ctx.is_deprecated(self.func))
|
||||
.detail(self.detail())
|
||||
.add_call_parens(self.ctx.completion, self.name, params)
|
||||
.add_import(
|
||||
import_to_add,
|
||||
self.ctx.completion.config.should_resolve_additional_edits_immediately(),
|
||||
)
|
||||
.add_import(import_to_add, self.ctx.completion.config.resolve_edits_immediately())
|
||||
.build()
|
||||
}
|
||||
|
||||
|
|
|
@ -50,10 +50,7 @@ impl<'a> MacroRender<'a> {
|
|||
.kind(CompletionItemKind::Macro)
|
||||
.set_documentation(self.docs.clone())
|
||||
.set_deprecated(self.ctx.is_deprecated(self.macro_))
|
||||
.add_import(
|
||||
import_to_add,
|
||||
self.ctx.completion.config.should_resolve_additional_edits_immediately(),
|
||||
)
|
||||
.add_import(import_to_add, self.ctx.completion.config.resolve_edits_immediately())
|
||||
.detail(self.detail());
|
||||
|
||||
let needs_bang = self.needs_bang();
|
||||
|
|
|
@ -388,7 +388,7 @@ impl Config {
|
|||
}
|
||||
|
||||
self.completion.allow_snippets(false);
|
||||
self.completion.resolve_capabilities =
|
||||
self.completion.active_resolve_capabilities =
|
||||
enabled_completions_resolve_capabilities(caps).unwrap_or_default();
|
||||
if let Some(completion) = &doc_caps.completion {
|
||||
if let Some(completion_item) = &completion.completion_item {
|
||||
|
|
|
@ -8,9 +8,10 @@ use std::{
|
|||
};
|
||||
|
||||
use ide::{
|
||||
FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, NavigationTarget, Query,
|
||||
RangeInfo, Runnable, RunnableKind, SearchScope, TextEdit,
|
||||
FileId, FilePosition, FileRange, HoverAction, HoverGotoTypeData, ImportToAdd, LineIndex,
|
||||
NavigationTarget, Query, RangeInfo, Runnable, RunnableKind, SearchScope, TextEdit,
|
||||
};
|
||||
use ide_db::helpers::{insert_use, mod_path_to_ast};
|
||||
use itertools::Itertools;
|
||||
use lsp_server::ErrorCode;
|
||||
use lsp_types::{
|
||||
|
@ -35,9 +36,9 @@ use crate::{
|
|||
config::RustfmtConfig,
|
||||
from_json, from_proto,
|
||||
global_state::{CompletionResolveData, GlobalState, GlobalStateSnapshot},
|
||||
line_endings::LineEndings,
|
||||
lsp_ext::{self, InlayHint, InlayHintsParams},
|
||||
to_proto::{self, append_import_edits},
|
||||
LspError, Result,
|
||||
to_proto, LspError, Result,
|
||||
};
|
||||
|
||||
pub(crate) fn handle_analyzer_status(
|
||||
|
@ -577,20 +578,19 @@ pub(crate) fn handle_completion(
|
|||
.into_iter()
|
||||
.enumerate()
|
||||
.flat_map(|(item_index, item)| {
|
||||
let mut new_completion_items = to_proto::completion_item(
|
||||
&line_index,
|
||||
line_endings,
|
||||
item.clone(),
|
||||
snap.config.completion.should_resolve_additional_edits_immediately(),
|
||||
);
|
||||
let mut new_completion_items =
|
||||
to_proto::completion_item(&line_index, line_endings, item.clone());
|
||||
|
||||
let item_id = serde_json::to_value(&item_index)
|
||||
.expect(&format!("Should be able to serialize usize value {}", item_index));
|
||||
completion_resolve_data
|
||||
.insert(item_index, CompletionResolveData { file_id: position.file_id, item });
|
||||
for new_item in &mut new_completion_items {
|
||||
new_item.data = Some(item_id.clone());
|
||||
if !snap.config.completion.active_resolve_capabilities.is_empty() {
|
||||
let item_id = serde_json::to_value(&item_index)
|
||||
.expect(&format!("Should be able to serialize usize value {}", item_index));
|
||||
completion_resolve_data
|
||||
.insert(item_index, CompletionResolveData { file_id: position.file_id, item });
|
||||
for new_item in &mut new_completion_items {
|
||||
new_item.data = Some(item_id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
new_completion_items
|
||||
})
|
||||
.collect();
|
||||
|
@ -620,7 +620,7 @@ pub(crate) fn handle_resolve_completion(
|
|||
};
|
||||
|
||||
let snap = &global_state.snapshot();
|
||||
for supported_completion_resolve_cap in &snap.config.completion.resolve_capabilities {
|
||||
for supported_completion_resolve_cap in &snap.config.completion.active_resolve_capabilities {
|
||||
match supported_completion_resolve_cap {
|
||||
ide::CompletionResolveCapability::AdditionalTextEdits => {
|
||||
// FIXME actually add all additional edits here?
|
||||
|
@ -1598,3 +1598,44 @@ fn should_skip_target(runnable: &Runnable, cargo_spec: Option<&CargoTargetSpec>)
|
|||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn append_import_edits(
|
||||
completion: &mut lsp_types::CompletionItem,
|
||||
import_to_add: &ImportToAdd,
|
||||
line_index: &LineIndex,
|
||||
line_endings: LineEndings,
|
||||
) {
|
||||
let new_edits = import_into_edits(import_to_add, line_index, line_endings);
|
||||
if let Some(original_additional_edits) = completion.additional_text_edits.as_mut() {
|
||||
if let Some(mut new_edits) = new_edits {
|
||||
original_additional_edits.extend(new_edits.drain(..))
|
||||
}
|
||||
} else {
|
||||
completion.additional_text_edits = new_edits;
|
||||
}
|
||||
}
|
||||
|
||||
fn import_into_edits(
|
||||
import_to_add: &ImportToAdd,
|
||||
line_index: &LineIndex,
|
||||
line_endings: LineEndings,
|
||||
) -> Option<Vec<lsp_types::TextEdit>> {
|
||||
let _p = profile::span("add_import_edits");
|
||||
|
||||
let rewriter = insert_use::insert_use(
|
||||
&import_to_add.import_scope,
|
||||
mod_path_to_ast(&import_to_add.import_path),
|
||||
import_to_add.merge_behaviour,
|
||||
);
|
||||
let old_ast = rewriter.rewrite_root()?;
|
||||
let mut import_insert = TextEdit::builder();
|
||||
algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut import_insert);
|
||||
let import_edit = import_insert.finish();
|
||||
|
||||
Some(
|
||||
import_edit
|
||||
.into_iter()
|
||||
.map(|indel| to_proto::text_edit(line_index, line_endings, indel))
|
||||
.collect_vec(),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -7,16 +7,12 @@ use std::{
|
|||
use ide::{
|
||||
Assist, AssistKind, CallInfo, CompletionItem, CompletionItemKind, Documentation,
|
||||
FileSystemEdit, Fold, FoldKind, Highlight, HighlightModifier, HighlightTag, HighlightedRange,
|
||||
ImportToAdd, Indel, InlayHint, InlayKind, InsertTextFormat, LineIndex, Markup,
|
||||
NavigationTarget, ReferenceAccess, ResolvedAssist, Runnable, Severity, SourceChange,
|
||||
SourceFileEdit, TextEdit,
|
||||
};
|
||||
use ide_db::{
|
||||
base_db::{FileId, FileRange},
|
||||
helpers::{insert_use, mod_path_to_ast},
|
||||
Indel, InlayHint, InlayKind, InsertTextFormat, LineIndex, Markup, NavigationTarget,
|
||||
ReferenceAccess, ResolvedAssist, Runnable, Severity, SourceChange, SourceFileEdit, TextEdit,
|
||||
};
|
||||
use ide_db::base_db::{FileId, FileRange};
|
||||
use itertools::Itertools;
|
||||
use syntax::{algo, SyntaxKind, TextRange, TextSize};
|
||||
use syntax::{SyntaxKind, TextRange, TextSize};
|
||||
|
||||
use crate::{
|
||||
cargo_target_spec::CargoTargetSpec, global_state::GlobalStateSnapshot,
|
||||
|
@ -162,7 +158,6 @@ pub(crate) fn completion_item(
|
|||
line_index: &LineIndex,
|
||||
line_endings: LineEndings,
|
||||
completion_item: CompletionItem,
|
||||
should_resolve_additional_edits_immediately: bool,
|
||||
) -> Vec<lsp_types::CompletionItem> {
|
||||
fn set_score(res: &mut lsp_types::CompletionItem, label: &str) {
|
||||
res.preselect = Some(true);
|
||||
|
@ -238,13 +233,7 @@ pub(crate) fn completion_item(
|
|||
|
||||
for mut r in all_results.iter_mut() {
|
||||
r.insert_text_format = Some(insert_text_format(completion_item.insert_text_format()));
|
||||
if !should_resolve_additional_edits_immediately {
|
||||
if let Some(unapplied_import_data) = completion_item.import_to_add() {
|
||||
append_import_edits(r, unapplied_import_data, line_index, line_endings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
all_results
|
||||
}
|
||||
|
||||
|
@ -828,47 +817,6 @@ pub(crate) fn markup_content(markup: Markup) -> lsp_types::MarkupContent {
|
|||
lsp_types::MarkupContent { kind: lsp_types::MarkupKind::Markdown, value }
|
||||
}
|
||||
|
||||
pub(crate) fn import_into_edits(
|
||||
import_to_add: &ImportToAdd,
|
||||
line_index: &LineIndex,
|
||||
line_endings: LineEndings,
|
||||
) -> Option<Vec<lsp_types::TextEdit>> {
|
||||
let _p = profile::span("add_import_edits");
|
||||
|
||||
let rewriter = insert_use::insert_use(
|
||||
&import_to_add.import_scope,
|
||||
mod_path_to_ast(&import_to_add.import_path),
|
||||
import_to_add.merge_behaviour,
|
||||
);
|
||||
let old_ast = rewriter.rewrite_root()?;
|
||||
let mut import_insert = TextEdit::builder();
|
||||
algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut import_insert);
|
||||
let import_edit = import_insert.finish();
|
||||
|
||||
Some(
|
||||
import_edit
|
||||
.into_iter()
|
||||
.map(|indel| text_edit(line_index, line_endings, indel))
|
||||
.collect_vec(),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn append_import_edits(
|
||||
completion: &mut lsp_types::CompletionItem,
|
||||
import_to_add: &ImportToAdd,
|
||||
line_index: &LineIndex,
|
||||
line_endings: LineEndings,
|
||||
) {
|
||||
let new_edits = import_into_edits(import_to_add, line_index, line_endings);
|
||||
if let Some(original_additional_edits) = completion.additional_text_edits.as_mut() {
|
||||
if let Some(mut new_edits) = new_edits {
|
||||
original_additional_edits.extend(new_edits.drain(..))
|
||||
}
|
||||
} else {
|
||||
completion.additional_text_edits = new_edits;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ide::Analysis;
|
||||
|
@ -897,7 +845,7 @@ mod tests {
|
|||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|c| c.label().ends_with("arg"))
|
||||
.map(|c| completion_item(&line_index, LineEndings::Unix, c, true))
|
||||
.map(|c| completion_item(&line_index, LineEndings::Unix, c))
|
||||
.flat_map(|comps| comps.into_iter().map(|c| (c.label, c.sort_text)))
|
||||
.collect();
|
||||
expect_test::expect![[r#"
|
||||
|
|
Loading…
Reference in a new issue