From ebb584ce669d04f109d5b21a08aca9d4e9acecc8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 01:14:15 +0300 Subject: rename completion kind --- crates/ra_analysis/src/completion/completion_item.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index d5d751759..30581c8a5 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -7,7 +7,7 @@ pub struct CompletionItem { lookup: Option, snippet: Option, /// Used only internally in test, to check only specific kind of completion. - kind: CompletionKind, + completion_kind: CompletionKind, } pub enum InsertText { @@ -34,7 +34,7 @@ impl CompletionItem { label, lookup: None, snippet: None, - kind: CompletionKind::Unspecified, + completion_kind: CompletionKind::Unspecified, } } /// What user sees in pop-up in the UI. @@ -65,7 +65,7 @@ pub(crate) struct Builder { label: String, lookup: Option, snippet: Option, - kind: CompletionKind, + completion_kind: CompletionKind, } impl Builder { @@ -78,7 +78,7 @@ impl Builder { label: self.label, lookup: self.lookup, snippet: self.snippet, - kind: self.kind, + completion_kind: self.completion_kind, } } pub(crate) fn lookup_by(mut self, lookup: impl Into) -> Builder { @@ -90,7 +90,7 @@ impl Builder { self } pub(crate) fn kind(mut self, kind: CompletionKind) -> Builder { - self.kind = kind; + self.completion_kind = kind; self } } @@ -154,7 +154,7 @@ impl Completions { fn debug_render(&self, kind: CompletionKind) -> String { let mut res = String::new(); for c in self.buf.iter() { - if c.kind == kind { + if c.completion_kind == kind { if let Some(lookup) = &c.lookup { res.push_str(lookup); res.push_str(&format!(" {:?}", c.label)); -- cgit v1.2.3 From 25dda42f3773b1d002a5809c0182c2adc6c47027 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 01:27:07 +0300 Subject: introduce ComletionItemKind --- .../ra_analysis/src/completion/complete_fn_param.rs | 3 +-- .../ra_analysis/src/completion/complete_keyword.rs | 5 +---- crates/ra_analysis/src/completion/complete_path.rs | 8 +++----- crates/ra_analysis/src/completion/complete_scope.rs | 14 +++----------- .../ra_analysis/src/completion/complete_snippet.rs | 12 ++++-------- crates/ra_analysis/src/completion/completion_item.rs | 20 ++++++++++++++++---- 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/crates/ra_analysis/src/completion/complete_fn_param.rs b/crates/ra_analysis/src/completion/complete_fn_param.rs index 6a6213e67..bb5fdfda0 100644 --- a/crates/ra_analysis/src/completion/complete_fn_param.rs +++ b/crates/ra_analysis/src/completion/complete_fn_param.rs @@ -34,9 +34,8 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) } }) .for_each(|(label, lookup)| { - CompletionItem::new(label) + CompletionItem::new(CompletionKind::Magic, label) .lookup_by(lookup) - .kind(CompletionKind::Magic) .add_to(acc) }); diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index dead15bb6..1e6d7008d 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -61,10 +61,7 @@ fn complete_return(fn_def: ast::FnDef, is_stmt: bool) -> Option } fn keyword(kw: &str, snippet: &str) -> CompletionItem { - CompletionItem::new(kw) - .kind(Keyword) - .snippet(snippet) - .build() + CompletionItem::new(Keyword, kw).snippet(snippet).build() } #[cfg(test)] diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs index 5fc24af72..692502bb2 100644 --- a/crates/ra_analysis/src/completion/complete_path.rs +++ b/crates/ra_analysis/src/completion/complete_path.rs @@ -17,11 +17,9 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C _ => return Ok(()), }; let module_scope = target_module.scope(ctx.db)?; - module_scope.entries().for_each(|(name, _res)| { - CompletionItem::new(name.to_string()) - .kind(Reference) - .add_to(acc) - }); + module_scope + .entries() + .for_each(|(name, _res)| CompletionItem::new(Reference, name.to_string()).add_to(acc)); Ok(()) } diff --git a/crates/ra_analysis/src/completion/complete_scope.rs b/crates/ra_analysis/src/completion/complete_scope.rs index d07c0e46d..f39b98d62 100644 --- a/crates/ra_analysis/src/completion/complete_scope.rs +++ b/crates/ra_analysis/src/completion/complete_scope.rs @@ -29,11 +29,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> } } }) - .for_each(|(name, _res)| { - CompletionItem::new(name.to_string()) - .kind(Reference) - .add_to(acc) - }); + .for_each(|(name, _res)| CompletionItem::new(Reference, name.to_string()).add_to(acc)); } Ok(()) @@ -45,13 +41,9 @@ fn complete_fn(acc: &mut Completions, scopes: &hir::FnScopes, offset: TextUnit) .scope_chain_for_offset(offset) .flat_map(|scope| scopes.entries(scope).iter()) .filter(|entry| shadowed.insert(entry.name())) - .for_each(|entry| { - CompletionItem::new(entry.name().to_string()) - .kind(Reference) - .add_to(acc) - }); + .for_each(|entry| CompletionItem::new(Reference, entry.name().to_string()).add_to(acc)); if scopes.self_param.is_some() { - CompletionItem::new("self").kind(Reference).add_to(acc); + CompletionItem::new(Reference, "self").add_to(acc); } } diff --git a/crates/ra_analysis/src/completion/complete_snippet.rs b/crates/ra_analysis/src/completion/complete_snippet.rs index ccd68832b..f0ad45fec 100644 --- a/crates/ra_analysis/src/completion/complete_snippet.rs +++ b/crates/ra_analysis/src/completion/complete_snippet.rs @@ -4,13 +4,11 @@ pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) { return; } - CompletionItem::new("pd") + CompletionItem::new(Snippet, "pd") .snippet("eprintln!(\"$0 = {:?}\", $0);") - .kind(Snippet) .add_to(acc); - CompletionItem::new("ppd") + CompletionItem::new(Snippet, "ppd") .snippet("eprintln!(\"$0 = {:#?}\", $0);") - .kind(Snippet) .add_to(acc); } @@ -18,7 +16,7 @@ pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionConte if !ctx.is_new_item { return; } - CompletionItem::new("Test function") + CompletionItem::new(Snippet, "Test function") .lookup_by("tfn") .snippet( "\ @@ -27,11 +25,9 @@ fn ${1:feature}() { $0 }", ) - .kind(Snippet) .add_to(acc); - CompletionItem::new("pub(crate)") + CompletionItem::new(Snippet, "pub(crate)") .snippet("pub(crate) $0") - .kind(Snippet) .add_to(acc); } diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index 30581c8a5..423d5a48b 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -6,7 +6,9 @@ pub struct CompletionItem { label: String, lookup: Option, snippet: Option, - /// Used only internally in test, to check only specific kind of completion. + kind: Option, + /// Used only internally in tests, to check only specific kind of + /// completion. completion_kind: CompletionKind, } @@ -15,6 +17,12 @@ pub enum InsertText { Snippet { text: String }, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CompletionItemKind { + Snippet, + Keyword, +} + #[derive(Debug, PartialEq, Eq)] pub(crate) enum CompletionKind { /// Parser-based keyword completion. @@ -24,17 +32,16 @@ pub(crate) enum CompletionKind { /// "Secret sauce" completions. Magic, Snippet, - Unspecified, } impl CompletionItem { - pub(crate) fn new(label: impl Into) -> Builder { + pub(crate) fn new(completion_kind: CompletionKind, label: impl Into) -> Builder { let label = label.into(); Builder { label, lookup: None, snippet: None, - completion_kind: CompletionKind::Unspecified, + completion_kind, } } /// What user sees in pop-up in the UI. @@ -57,6 +64,10 @@ impl CompletionItem { Some(it) => InsertText::Snippet { text: it.clone() }, } } + + pub fn kind(&self) -> Option { + self.kind + } } /// A helper to make `CompletionItem`s. @@ -78,6 +89,7 @@ impl Builder { label: self.label, lookup: self.lookup, snippet: self.snippet, + kind: None, completion_kind: self.completion_kind, } } -- cgit v1.2.3 From 284e89406906b97eec8e0f91bce2955a5e94b880 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 01:34:22 +0300 Subject: cleanup --- .../ra_analysis/src/completion/complete_keyword.rs | 12 +++++---- crates/ra_analysis/src/completion/complete_path.rs | 8 +++--- .../ra_analysis/src/completion/complete_scope.rs | 12 ++++++--- .../ra_analysis/src/completion/complete_snippet.rs | 31 +++++++++++----------- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index 1e6d7008d..2c7891920 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -5,7 +5,13 @@ use ra_syntax::{ SyntaxKind::*, SyntaxNodeRef, }; -use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind::*}; +use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind}; + +fn keyword(kw: &str, snippet: &str) -> CompletionItem { + CompletionItem::new(CompletionKind::Keyword, kw) + .snippet(snippet) + .build() +} pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) { if !ctx.is_trivial_path { @@ -60,10 +66,6 @@ fn complete_return(fn_def: ast::FnDef, is_stmt: bool) -> Option Some(keyword("return", snip)) } -fn keyword(kw: &str, snippet: &str) -> CompletionItem { - CompletionItem::new(Keyword, kw).snippet(snippet).build() -} - #[cfg(test)] mod tests { use crate::completion::{CompletionKind, check_completion}; diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs index 692502bb2..802f4abe4 100644 --- a/crates/ra_analysis/src/completion/complete_path.rs +++ b/crates/ra_analysis/src/completion/complete_path.rs @@ -1,6 +1,6 @@ use crate::{ Cancelable, - completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext}, + completion::{CompletionItem, Completions, CompletionKind, CompletionContext}, }; pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { @@ -17,9 +17,9 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C _ => return Ok(()), }; let module_scope = target_module.scope(ctx.db)?; - module_scope - .entries() - .for_each(|(name, _res)| CompletionItem::new(Reference, name.to_string()).add_to(acc)); + module_scope.entries().for_each(|(name, _res)| { + CompletionItem::new(CompletionKind::Reference, name.to_string()).add_to(acc) + }); Ok(()) } diff --git a/crates/ra_analysis/src/completion/complete_scope.rs b/crates/ra_analysis/src/completion/complete_scope.rs index f39b98d62..fb87be4b1 100644 --- a/crates/ra_analysis/src/completion/complete_scope.rs +++ b/crates/ra_analysis/src/completion/complete_scope.rs @@ -3,7 +3,7 @@ use ra_syntax::TextUnit; use crate::{ Cancelable, - completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext}, + completion::{CompletionItem, Completions, CompletionKind, CompletionContext}, }; pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { @@ -29,7 +29,9 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> } } }) - .for_each(|(name, _res)| CompletionItem::new(Reference, name.to_string()).add_to(acc)); + .for_each(|(name, _res)| { + CompletionItem::new(CompletionKind::Reference, name.to_string()).add_to(acc) + }); } Ok(()) @@ -41,9 +43,11 @@ fn complete_fn(acc: &mut Completions, scopes: &hir::FnScopes, offset: TextUnit) .scope_chain_for_offset(offset) .flat_map(|scope| scopes.entries(scope).iter()) .filter(|entry| shadowed.insert(entry.name())) - .for_each(|entry| CompletionItem::new(Reference, entry.name().to_string()).add_to(acc)); + .for_each(|entry| { + CompletionItem::new(CompletionKind::Reference, entry.name().to_string()).add_to(acc) + }); if scopes.self_param.is_some() { - CompletionItem::new(Reference, "self").add_to(acc); + CompletionItem::new(CompletionKind::Reference, "self").add_to(acc); } } diff --git a/crates/ra_analysis/src/completion/complete_snippet.rs b/crates/ra_analysis/src/completion/complete_snippet.rs index f0ad45fec..1feb332b9 100644 --- a/crates/ra_analysis/src/completion/complete_snippet.rs +++ b/crates/ra_analysis/src/completion/complete_snippet.rs @@ -1,34 +1,33 @@ -use crate::completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext}; +use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext, completion_item::Builder}; + +fn snippet(label: &str, snippet: &str) -> Builder { + CompletionItem::new(CompletionKind::Snippet, label).snippet(snippet) +} pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) { if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) { return; } - CompletionItem::new(Snippet, "pd") - .snippet("eprintln!(\"$0 = {:?}\", $0);") - .add_to(acc); - CompletionItem::new(Snippet, "ppd") - .snippet("eprintln!(\"$0 = {:#?}\", $0);") - .add_to(acc); + snippet("pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc); + snippet("ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc); } pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) { if !ctx.is_new_item { return; } - CompletionItem::new(Snippet, "Test function") - .lookup_by("tfn") - .snippet( - "\ + snippet( + "Test function", + "\ #[test] fn ${1:feature}() { $0 }", - ) - .add_to(acc); - CompletionItem::new(Snippet, "pub(crate)") - .snippet("pub(crate) $0") - .add_to(acc); + ) + .lookup_by("tfn") + .add_to(acc); + + snippet("pub(crate)", "pub(crate) $0").add_to(acc); } #[cfg(test)] -- cgit v1.2.3 From 328d123f5baeab8ff9a1f63a6744f6eec89818ab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 01:37:11 +0300 Subject: specify completion item kind --- crates/ra_analysis/src/completion.rs | 2 +- crates/ra_analysis/src/completion/complete_keyword.rs | 3 ++- crates/ra_analysis/src/completion/complete_snippet.rs | 6 ++++-- crates/ra_analysis/src/completion/completion_item.rs | 18 ++++++++++-------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index 2d61a3aef..d742d6295 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs @@ -18,7 +18,7 @@ use crate::{ }, }; -pub use crate::completion::completion_item::{CompletionItem, InsertText}; +pub use crate::completion::completion_item::{CompletionItem, InsertText, CompletionItemKind}; /// Main entry point for copmletion. We run comletion as a two-phase process. /// diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index 2c7891920..5427fcb11 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -5,10 +5,11 @@ use ra_syntax::{ SyntaxKind::*, SyntaxNodeRef, }; -use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind}; +use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind, CompletionItemKind}; fn keyword(kw: &str, snippet: &str) -> CompletionItem { CompletionItem::new(CompletionKind::Keyword, kw) + .kind(CompletionItemKind::Keyword) .snippet(snippet) .build() } diff --git a/crates/ra_analysis/src/completion/complete_snippet.rs b/crates/ra_analysis/src/completion/complete_snippet.rs index 1feb332b9..91de8ca37 100644 --- a/crates/ra_analysis/src/completion/complete_snippet.rs +++ b/crates/ra_analysis/src/completion/complete_snippet.rs @@ -1,7 +1,9 @@ -use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext, completion_item::Builder}; +use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionItemKind, CompletionContext, completion_item::Builder}; fn snippet(label: &str, snippet: &str) -> Builder { - CompletionItem::new(CompletionKind::Snippet, label).snippet(snippet) + CompletionItem::new(CompletionKind::Snippet, label) + .snippet(snippet) + .kind(CompletionItemKind::Keyword) } pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) { diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index 423d5a48b..89fbe62d8 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -3,13 +3,13 @@ /// `CompletionItem`, use `new` method and the `Builder` struct. #[derive(Debug)] pub struct CompletionItem { + /// Used only internally in tests, to check only specific kind of + /// completion. + completion_kind: CompletionKind, label: String, lookup: Option, snippet: Option, kind: Option, - /// Used only internally in tests, to check only specific kind of - /// completion. - completion_kind: CompletionKind, } pub enum InsertText { @@ -38,10 +38,11 @@ impl CompletionItem { pub(crate) fn new(completion_kind: CompletionKind, label: impl Into) -> Builder { let label = label.into(); Builder { + completion_kind, label, lookup: None, snippet: None, - completion_kind, + kind: None, } } /// What user sees in pop-up in the UI. @@ -73,10 +74,11 @@ impl CompletionItem { /// A helper to make `CompletionItem`s. #[must_use] pub(crate) struct Builder { + completion_kind: CompletionKind, label: String, lookup: Option, snippet: Option, - completion_kind: CompletionKind, + kind: Option, } impl Builder { @@ -89,7 +91,7 @@ impl Builder { label: self.label, lookup: self.lookup, snippet: self.snippet, - kind: None, + kind: self.kind, completion_kind: self.completion_kind, } } @@ -101,8 +103,8 @@ impl Builder { self.snippet = Some(snippet.into()); self } - pub(crate) fn kind(mut self, kind: CompletionKind) -> Builder { - self.completion_kind = kind; + pub(crate) fn kind(mut self, kind: CompletionItemKind) -> Builder { + self.kind = Some(kind); self } } -- cgit v1.2.3 From f1f2804c71ee997e36904dea72911104b2e2375b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 01:59:32 +0300 Subject: move completion items to conv --- crates/gen_lsp_server/Cargo.toml | 2 +- crates/gen_lsp_server/src/msg.rs | 2 +- crates/ra_hir/src/function/scope.rs | 2 +- crates/ra_lsp_server/src/conv.rs | 28 +++++++++++++++++++++++-- crates/ra_lsp_server/src/main_loop/handlers.rs | 29 ++++---------------------- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/crates/gen_lsp_server/Cargo.toml b/crates/gen_lsp_server/Cargo.toml index 5f90d39d6..9776a82e3 100644 --- a/crates/gen_lsp_server/Cargo.toml +++ b/crates/gen_lsp_server/Cargo.toml @@ -12,5 +12,5 @@ languageserver-types = "0.53.0" log = "0.4.3" failure = "0.1.2" serde_json = "1.0.24" -serde = "1.0.71" +serde = { version = "1.0.71", features = ["derive"] } crossbeam-channel = "0.2.4" diff --git a/crates/gen_lsp_server/src/msg.rs b/crates/gen_lsp_server/src/msg.rs index ef6358cb1..af901d0d2 100644 --- a/crates/gen_lsp_server/src/msg.rs +++ b/crates/gen_lsp_server/src/msg.rs @@ -7,7 +7,7 @@ use failure::{bail, format_err}; use crate::Result; -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Serialize, Deserialize, Debug, Clone)] #[serde(untagged)] pub enum RawMessage { Request(RawRequest), diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/function/scope.rs index 9f1aa1ef2..d12f1781e 100644 --- a/crates/ra_hir/src/function/scope.rs +++ b/crates/ra_hir/src/function/scope.rs @@ -95,7 +95,7 @@ impl FnScopes { r1.start().cmp(&r2.start()) } }) - .map(|(ptr, scope)| *scope) + .map(|(_ptr, scope)| *scope) .unwrap_or(original_scope) } diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 218ded4ee..973e0915e 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -1,8 +1,8 @@ use languageserver_types::{ self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, - TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, + TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, }; -use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition}; +use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, InsertText}; use ra_editor::{LineCol, LineIndex}; use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; @@ -45,6 +45,30 @@ impl Conv for SyntaxKind { } } +impl Conv for CompletionItem { + type Output = ::languageserver_types::CompletionItem; + + fn conv(self) -> ::Output { + let mut res = ::languageserver_types::CompletionItem { + label: self.label().to_string(), + filter_text: Some(self.lookup().to_string()), + ..Default::default() + }; + match self.insert_text() { + InsertText::PlainText { text } => { + res.insert_text = Some(text); + res.insert_text_format = Some(InsertTextFormat::PlainText); + } + InsertText::Snippet { text } => { + res.insert_text = Some(text); + res.insert_text_format = Some(InsertTextFormat::Snippet); + res.kind = Some(languageserver_types::CompletionItemKind::Keyword); + } + } + res + } +} + impl ConvWith for Position { type Ctx = LineIndex; type Output = TextUnit; diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 2dfeb061a..252d1ba3e 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -2,13 +2,13 @@ use std::collections::HashMap; use gen_lsp_server::ErrorCode; use languageserver_types::{ - CodeActionResponse, Command, CompletionItem, CompletionItemKind, Diagnostic, + CodeActionResponse, Command, Diagnostic, DiagnosticSeverity, DocumentSymbol, Documentation, FoldingRange, FoldingRangeKind, - FoldingRangeParams, InsertTextFormat, Location, MarkupContent, MarkupKind, MarkedString, Position, + FoldingRangeParams, Location, MarkupContent, MarkupKind, MarkedString, Position, PrepareRenameResponse, RenameParams, SymbolInformation, TextDocumentIdentifier, TextEdit, WorkspaceEdit, ParameterInformation, ParameterLabel, SignatureInformation, Hover, HoverContents, }; -use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition, InsertText}; +use ra_analysis::{FileId, FoldKind, Query, RunnableKind, FilePosition}; use ra_syntax::{TextUnit, text_utils::intersect}; use ra_text_edit::text_utils::contains_offset_nonstrict; use rustc_hash::FxHashMap; @@ -419,28 +419,7 @@ pub fn handle_completion( None => return Ok(None), Some(items) => items, }; - let items = items - .into_iter() - .map(|item| { - let mut res = CompletionItem { - label: item.label().to_string(), - filter_text: Some(item.lookup().to_string()), - ..Default::default() - }; - match item.insert_text() { - InsertText::PlainText { text } => { - res.insert_text = Some(text); - res.insert_text_format = Some(InsertTextFormat::PlainText); - } - InsertText::Snippet { text } => { - res.insert_text = Some(text); - res.insert_text_format = Some(InsertTextFormat::Snippet); - res.kind = Some(CompletionItemKind::Keyword); - } - } - res - }) - .collect(); + let items = items.into_iter().map(|item| item.conv()).collect(); Ok(Some(req::CompletionResponse::Array(items))) } -- cgit v1.2.3 From 238b52358d0d6109c89d1288b12bfa0aa71790de Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 02:02:47 +0300 Subject: more icons --- crates/ra_analysis/src/lib.rs | 2 +- crates/ra_lsp_server/src/conv.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index b2f4cd228..6fd157880 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -30,7 +30,7 @@ use crate::{ }; pub use crate::{ - completion::{CompletionItem, InsertText}, + completion::{CompletionItem, CompletionItemKind, InsertText}, }; pub use ra_editor::{ FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 973e0915e..6f770ec69 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -2,7 +2,7 @@ use languageserver_types::{ self, Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url, VersionedTextDocumentIdentifier, InsertTextFormat, }; -use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, InsertText}; +use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition, CompletionItem, CompletionItemKind, InsertText}; use ra_editor::{LineCol, LineIndex}; use ra_text_edit::{AtomTextEdit, TextEdit}; use ra_syntax::{SyntaxKind, TextRange, TextUnit}; @@ -45,6 +45,18 @@ impl Conv for SyntaxKind { } } +impl Conv for CompletionItemKind { + type Output = ::languageserver_types::CompletionItemKind; + + fn conv(self) -> ::Output { + use ::languageserver_types::CompletionItemKind::*; + match self { + CompletionItemKind::Keyword => Keyword, + CompletionItemKind::Snippet => Snippet, + } + } +} + impl Conv for CompletionItem { type Output = ::languageserver_types::CompletionItem; @@ -52,6 +64,7 @@ impl Conv for CompletionItem { let mut res = ::languageserver_types::CompletionItem { label: self.label().to_string(), filter_text: Some(self.lookup().to_string()), + kind: self.kind().map(|it| it.conv()), ..Default::default() }; match self.insert_text() { -- cgit v1.2.3 From 97cb463c9be3c21931d0e284603238ce3cb02b2a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 02:20:14 +0300 Subject: moar icons --- crates/ra_analysis/src/completion/complete_path.rs | 6 ++++-- crates/ra_analysis/src/completion/complete_scope.rs | 6 ++++-- .../ra_analysis/src/completion/completion_item.rs | 21 +++++++++++++++++++++ crates/ra_hir/src/lib.rs | 2 +- crates/ra_hir/src/module.rs | 2 +- crates/ra_hir/src/module/nameres.rs | 2 +- crates/ra_lsp_server/src/conv.rs | 2 ++ 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs index 802f4abe4..ad4d68a33 100644 --- a/crates/ra_analysis/src/completion/complete_path.rs +++ b/crates/ra_analysis/src/completion/complete_path.rs @@ -17,8 +17,10 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C _ => return Ok(()), }; let module_scope = target_module.scope(ctx.db)?; - module_scope.entries().for_each(|(name, _res)| { - CompletionItem::new(CompletionKind::Reference, name.to_string()).add_to(acc) + module_scope.entries().for_each(|(name, res)| { + CompletionItem::new(CompletionKind::Reference, name.to_string()) + .from_resolution(ctx.db, res) + .add_to(acc) }); Ok(()) } diff --git a/crates/ra_analysis/src/completion/complete_scope.rs b/crates/ra_analysis/src/completion/complete_scope.rs index fb87be4b1..4f27ad9eb 100644 --- a/crates/ra_analysis/src/completion/complete_scope.rs +++ b/crates/ra_analysis/src/completion/complete_scope.rs @@ -29,8 +29,10 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> } } }) - .for_each(|(name, _res)| { - CompletionItem::new(CompletionKind::Reference, name.to_string()).add_to(acc) + .for_each(|(name, res)| { + CompletionItem::new(CompletionKind::Reference, name.to_string()) + .from_resolution(ctx.db, res) + .add_to(acc) }); } diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index 89fbe62d8..cbd42a44e 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -1,3 +1,5 @@ +use crate::db; + /// `CompletionItem` describes a single completion variant in the editor pop-up. /// It is basically a POD with various properties. To construct a /// `CompletionItem`, use `new` method and the `Builder` struct. @@ -21,6 +23,8 @@ pub enum InsertText { pub enum CompletionItemKind { Snippet, Keyword, + Module, + Function, } #[derive(Debug, PartialEq, Eq)] @@ -107,6 +111,23 @@ impl Builder { self.kind = Some(kind); self } + pub(crate) fn from_resolution( + mut self, + db: &db::RootDatabase, + resolution: &hir::Resolution, + ) -> Builder { + if let Some(def_id) = resolution.def_id { + if let Ok(def) = def_id.resolve(db) { + let kind = match def { + hir::Def::Module(..) => CompletionItemKind::Module, + hir::Def::Function(..) => CompletionItemKind::Function, + _ => return self, + }; + self.kind = Some(kind); + } + } + self + } } impl Into for Builder { diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 5941a9ea3..f56214b47 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -39,7 +39,7 @@ use crate::{ pub use self::{ path::{Path, PathKind}, krate::Crate, - module::{Module, ModuleId, Problem, nameres::ItemMap}, + module::{Module, ModuleId, Problem, nameres::ItemMap, ModuleScope, Resolution}, function::{Function, FnScopes}, }; diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs index d5866f6ef..cd31e8cfe 100644 --- a/crates/ra_hir/src/module.rs +++ b/crates/ra_hir/src/module.rs @@ -16,7 +16,7 @@ use crate::{ arena::{Arena, Id}, }; -pub use self::nameres::ModuleScope; +pub use self::nameres::{ModuleScope, Resolution}; /// `Module` is API entry point to get all the information /// about a particular module. diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index f44abc730..39e891cda 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs @@ -49,7 +49,7 @@ pub struct ModuleScope { } impl ModuleScope { - pub fn entries<'a>(&'a self) -> impl Iterator + 'a { + pub fn entries<'a>(&'a self) -> impl Iterator + 'a { self.items.iter() } pub fn get(&self, name: &SmolStr) -> Option<&Resolution> { diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 6f770ec69..9320f147a 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -53,6 +53,8 @@ impl Conv for CompletionItemKind { match self { CompletionItemKind::Keyword => Keyword, CompletionItemKind::Snippet => Snippet, + CompletionItemKind::Module => Module, + CompletionItemKind::Function => Function, } } } -- cgit v1.2.3 From 8e056ef0428190a1abf363a2409e40751a02ee5b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 02:23:25 +0300 Subject: debug pring --- crates/ra_hir/src/function/scope.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/ra_hir/src/function/scope.rs b/crates/ra_hir/src/function/scope.rs index d12f1781e..77be25f1a 100644 --- a/crates/ra_hir/src/function/scope.rs +++ b/crates/ra_hir/src/function/scope.rs @@ -209,7 +209,6 @@ fn compute_block_scopes(block: ast::Block, scopes: &mut FnScopes, mut scope: Sco } } if let Some(expr) = block.expr() { - eprintln!("{:?}", expr); scopes.set_scope(expr.syntax(), scope); compute_expr_scopes(expr, scopes, scope); } -- cgit v1.2.3 From 498e3a6846602b9f2cd40c093007071b4df35817 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 02:24:59 +0300 Subject: binding icon --- crates/ra_analysis/src/completion/complete_scope.rs | 6 ++++-- crates/ra_analysis/src/completion/completion_item.rs | 1 + crates/ra_lsp_server/src/conv.rs | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/ra_analysis/src/completion/complete_scope.rs b/crates/ra_analysis/src/completion/complete_scope.rs index 4f27ad9eb..82610d63f 100644 --- a/crates/ra_analysis/src/completion/complete_scope.rs +++ b/crates/ra_analysis/src/completion/complete_scope.rs @@ -3,7 +3,7 @@ use ra_syntax::TextUnit; use crate::{ Cancelable, - completion::{CompletionItem, Completions, CompletionKind, CompletionContext}, + completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}, }; pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { @@ -46,7 +46,9 @@ fn complete_fn(acc: &mut Completions, scopes: &hir::FnScopes, offset: TextUnit) .flat_map(|scope| scopes.entries(scope).iter()) .filter(|entry| shadowed.insert(entry.name())) .for_each(|entry| { - CompletionItem::new(CompletionKind::Reference, entry.name().to_string()).add_to(acc) + CompletionItem::new(CompletionKind::Reference, entry.name().to_string()) + .kind(CompletionItemKind::Binding) + .add_to(acc) }); if scopes.self_param.is_some() { CompletionItem::new(CompletionKind::Reference, "self").add_to(acc); diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index cbd42a44e..911f08468 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -25,6 +25,7 @@ pub enum CompletionItemKind { Keyword, Module, Function, + Binding, } #[derive(Debug, PartialEq, Eq)] diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 9320f147a..051f1f995 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -55,6 +55,7 @@ impl Conv for CompletionItemKind { CompletionItemKind::Snippet => Snippet, CompletionItemKind::Module => Module, CompletionItemKind::Function => Function, + CompletionItemKind::Binding => Variable, } } } -- cgit v1.2.3 From ea763c73b8d89edbf716805c62cf31b00e2e1a4f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 02:28:09 +0300 Subject: fix snippet icon --- crates/ra_analysis/src/completion/complete_snippet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_analysis/src/completion/complete_snippet.rs b/crates/ra_analysis/src/completion/complete_snippet.rs index 91de8ca37..fb9da0a4f 100644 --- a/crates/ra_analysis/src/completion/complete_snippet.rs +++ b/crates/ra_analysis/src/completion/complete_snippet.rs @@ -3,7 +3,7 @@ use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionI fn snippet(label: &str, snippet: &str) -> Builder { CompletionItem::new(CompletionKind::Snippet, label) .snippet(snippet) - .kind(CompletionItemKind::Keyword) + .kind(CompletionItemKind::Snippet) } pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) { -- cgit v1.2.3