Draft the new lsp handler

This commit is contained in:
Kirill Bulatov 2020-11-28 16:26:30 +02:00
parent dfd0626dbf
commit 48acd7d455
5 changed files with 52 additions and 24 deletions

View file

@ -3,11 +3,8 @@
use std::fmt;
use hir::{Documentation, ModPath, Mutability};
use ide_db::helpers::{
insert_use::{self, ImportScope, MergeBehaviour},
mod_path_to_ast,
};
use syntax::{algo, TextRange};
use ide_db::helpers::insert_use::{ImportScope, MergeBehaviour};
use syntax::TextRange;
use text_edit::TextEdit;
use crate::config::SnippetCap;
@ -65,6 +62,10 @@ pub struct CompletionItem {
/// Indicates that a reference or mutable reference to this variable is a
/// possible match.
ref_match: Option<(Mutability, CompletionScore)>,
/// The data later to be used in the `completionItem/resolve` response
/// to add the insert import edit.
import_to_add: Option<ImportToAdd>,
}
// We use custom debug for CompletionItem to make snapshot tests more readable.
@ -294,11 +295,9 @@ impl Builder {
let mut label = self.label;
let mut lookup = self.lookup;
let mut insert_text = self.insert_text;
let mut text_edits = TextEdit::builder();
if let Some(import_data) = self.import_to_add {
let import = mod_path_to_ast(&import_data.import_path);
let mut import_path_without_last_segment = import_data.import_path;
if let Some(import_to_add) = self.import_to_add.as_ref() {
let mut import_path_without_last_segment = import_to_add.import_path.to_owned();
let _ = import_path_without_last_segment.segments.pop();
if !import_path_without_last_segment.segments.is_empty() {
@ -310,32 +309,20 @@ impl Builder {
}
label = format!("{}::{}", import_path_without_last_segment, label);
}
let rewriter = insert_use::insert_use(
&import_data.import_scope,
import,
import_data.merge_behaviour,
);
if let Some(old_ast) = rewriter.rewrite_root() {
algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits);
}
}
let original_edit = match self.text_edit {
let text_edit = match self.text_edit {
Some(it) => it,
None => {
TextEdit::replace(self.source_range, insert_text.unwrap_or_else(|| label.clone()))
}
};
let mut resulting_edit = text_edits.finish();
resulting_edit.union(original_edit).expect("Failed to unite text edits");
CompletionItem {
source_range: self.source_range,
label,
insert_text_format: self.insert_text_format,
text_edit: resulting_edit,
text_edit,
detail: self.detail,
documentation: self.documentation,
lookup,
@ -345,6 +332,7 @@ impl Builder {
trigger_call_info: self.trigger_call_info.unwrap_or(false),
score: self.score,
ref_match: self.ref_match,
import_to_add: self.import_to_add,
}
}
pub(crate) fn lookup_by(mut self, lookup: impl Into<String>) -> Builder {

View file

@ -30,7 +30,7 @@ pub fn server_capabilities(client_caps: &ClientCapabilities) -> ServerCapabiliti
})),
hover_provider: Some(HoverProviderCapability::Simple(true)),
completion_provider: Some(CompletionOptions {
resolve_provider: None,
resolve_provider: Some(true),
trigger_characters: Some(vec![":".to_string(), ".".to_string()]),
work_done_progress_options: WorkDoneProgressOptions { work_done_progress: None },
}),

View file

@ -577,6 +577,16 @@ pub(crate) fn handle_completion(
Ok(Some(completion_list.into()))
}
pub(crate) fn handle_resolve_completion(
snap: GlobalStateSnapshot,
original_completion: CompletionItem,
) -> Result<CompletionItem> {
let _p = profile::span("handle_resolve_completion");
// TODO kb use the field to detect it's for autocompletion and do the insert logic
let _data = dbg!(original_completion).data;
Ok(original_completion)
}
pub(crate) fn handle_folding_range(
snap: GlobalStateSnapshot,
params: FoldingRangeParams,

View file

@ -454,6 +454,7 @@ impl GlobalState {
.on::<lsp_types::request::GotoImplementation>(handlers::handle_goto_implementation)
.on::<lsp_types::request::GotoTypeDefinition>(handlers::handle_goto_type_definition)
.on::<lsp_types::request::Completion>(handlers::handle_completion)
.on::<lsp_types::request::ResolveCompletionItem>(handlers::handle_resolve_completion)
.on::<lsp_types::request::CodeLensRequest>(handlers::handle_code_lens)
.on::<lsp_types::request::CodeLensResolve>(handlers::handle_code_lens_resolve)
.on::<lsp_types::request::FoldingRangeRequest>(handlers::handle_folding_range)

View file

@ -231,6 +231,35 @@ pub(crate) fn completion_item(
None => vec![res],
};
// TODO kb need to get this logic away and store for the later resolve request
/*
let mut label = self.label;
let mut lookup = self.lookup;
let mut insert_text = self.insert_text;
let mut text_edits = TextEdit::builder();
if let Some((import_path, import_scope, merge_behaviour)) = completion_item.import_data.as_ref() {
let import = mod_path_to_ast(&import_path);
let mut import_path_without_last_segment = import_path;
let _ = import_path_without_last_segment.segments.pop();
if !import_path_without_last_segment.segments.is_empty() {
if lookup.is_none() {
lookup = Some(label.clone());
}
if insert_text.is_none() {
insert_text = Some(label.clone());
}
label = format!("{}::{}", import_path_without_last_segment, label);
}
let rewriter = insert_use(&import_scope, import, merge_behaviour);
if let Some(old_ast) = rewriter.rewrite_root() {
algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits);
}
}
*/
for mut r in all_results.iter_mut() {
r.insert_text_format = Some(insert_text_format(completion_item.insert_text_format()));
}