Remove redundant code
This commit is contained in:
@@ -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(),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user