Unify naming

This commit is contained in:
Aleksey Kladov 2021-03-12 12:12:32 +03:00
parent c01ef6eaba
commit 7e217a42e1
13 changed files with 90 additions and 103 deletions

View file

@ -168,20 +168,20 @@ fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, derive_input:
); );
let lookup = components.join(", "); let lookup = components.join(", ");
let label = components.iter().rev().join(", "); let label = components.iter().rev().join(", ");
let mut builder = let mut item =
CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label); CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
builder.lookup_by(lookup).kind(CompletionItemKind::Attribute); item.lookup_by(lookup).kind(CompletionItemKind::Attribute);
builder.add_to(acc); item.add_to(acc);
} }
for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) { for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) {
let mut builder = CompletionItem::new( let mut item = CompletionItem::new(
CompletionKind::Attribute, CompletionKind::Attribute,
ctx.source_range(), ctx.source_range(),
custom_derive_name, custom_derive_name,
); );
builder.kind(CompletionItemKind::Attribute); item.kind(CompletionItemKind::Attribute);
builder.add_to(acc); item.add_to(acc);
} }
} }
} }
@ -197,13 +197,13 @@ fn complete_lint(
.into_iter() .into_iter()
.filter(|completion| !existing_lints.contains(completion.label)) .filter(|completion| !existing_lints.contains(completion.label))
{ {
let mut builder = CompletionItem::new( let mut item = CompletionItem::new(
CompletionKind::Attribute, CompletionKind::Attribute,
ctx.source_range(), ctx.source_range(),
lint_completion.label, lint_completion.label,
); );
builder.kind(CompletionItemKind::Attribute).detail(lint_completion.description); item.kind(CompletionItemKind::Attribute).detail(lint_completion.description);
builder.add_to(acc) item.add_to(acc)
} }
} }
} }

View file

@ -54,9 +54,9 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
} }
params.into_iter().for_each(|(label, lookup)| { params.into_iter().for_each(|(label, lookup)| {
let mut builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label); let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
builder.kind(CompletionItemKind::Binding).lookup_by(lookup); item.kind(CompletionItemKind::Binding).lookup_by(lookup);
builder.add_to(acc) item.add_to(acc)
}); });
} }

View file

@ -12,21 +12,19 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
if ctx.use_item_syntax.is_some() { if ctx.use_item_syntax.is_some() {
if ctx.path_qual.is_none() { if ctx.path_qual.is_none() {
let mut crate_builder = let mut item = CompletionItem::new(CompletionKind::Keyword, source_range, "crate::");
CompletionItem::new(CompletionKind::Keyword, source_range, "crate::"); item.kind(CompletionItemKind::Keyword).insert_text("crate::");
crate_builder.kind(CompletionItemKind::Keyword).insert_text("crate::"); item.add_to(acc);
crate_builder.add_to(acc);
} }
let mut self_builder = CompletionItem::new(CompletionKind::Keyword, source_range, "self"); let mut item = CompletionItem::new(CompletionKind::Keyword, source_range, "self");
self_builder.kind(CompletionItemKind::Keyword); item.kind(CompletionItemKind::Keyword);
self_builder.add_to(acc); item.add_to(acc);
if iter::successors(ctx.path_qual.clone(), |p| p.qualifier()) if iter::successors(ctx.path_qual.clone(), |p| p.qualifier())
.all(|p| p.segment().and_then(|s| s.super_token()).is_some()) .all(|p| p.segment().and_then(|s| s.super_token()).is_some())
{ {
let mut super_builder = let mut item = CompletionItem::new(CompletionKind::Keyword, source_range, "super::");
CompletionItem::new(CompletionKind::Keyword, source_range, "super::"); item.kind(CompletionItemKind::Keyword).insert_text("super::");
super_builder.kind(CompletionItemKind::Keyword).insert_text("super::"); item.add_to(acc);
super_builder.add_to(acc);
} }
} }
@ -34,10 +32,10 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
if let Some(receiver) = &ctx.dot_receiver { if let Some(receiver) = &ctx.dot_receiver {
if let Some(ty) = ctx.sema.type_of_expr(receiver) { if let Some(ty) = ctx.sema.type_of_expr(receiver) {
if ty.impls_future(ctx.db) { if ty.impls_future(ctx.db) {
let mut builder = let mut item =
CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await"); CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await");
builder.kind(CompletionItemKind::Keyword).detail("expr.await").insert_text("await"); item.kind(CompletionItemKind::Keyword).detail("expr.await").insert_text("await");
builder.add_to(acc); item.add_to(acc);
} }
}; };
} }
@ -164,8 +162,8 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
} }
fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) { fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) {
let mut builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw); let mut item = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw);
builder.kind(CompletionItemKind::Keyword); item.kind(CompletionItemKind::Keyword);
match ctx.config.snippet_cap { match ctx.config.snippet_cap {
Some(cap) => { Some(cap) => {
@ -177,13 +175,13 @@ fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet
} else { } else {
snippet snippet
}; };
builder.insert_snippet(cap, snippet); item.insert_snippet(cap, snippet);
} }
None => { None => {
builder.insert_text(if snippet.contains('$') { kw } else { snippet }); item.insert_text(if snippet.contains('$') { kw } else { snippet });
} }
}; };
acc.add(builder.build()); item.add_to(acc);
} }
#[cfg(test)] #[cfg(test)]

View file

@ -80,10 +80,9 @@ pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
if mod_under_caret.semicolon_token().is_none() { if mod_under_caret.semicolon_token().is_none() {
label.push(';'); label.push(';');
} }
let mut builder = let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label);
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label); item.kind(SymbolKind::Module);
builder.kind(SymbolKind::Module); item.add_to(acc)
builder.add_to(acc)
}); });
Some(()) Some(())

View file

@ -297,11 +297,9 @@ fn postfix_snippet(
let delete_range = TextRange::new(receiver_range.start(), ctx.source_range().end()); let delete_range = TextRange::new(receiver_range.start(), ctx.source_range().end());
TextEdit::replace(delete_range, snippet.to_string()) TextEdit::replace(delete_range, snippet.to_string())
}; };
CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label) let mut item = CompletionItem::new(CompletionKind::Postfix, ctx.source_range(), label);
.detail(detail) item.detail(detail).kind(CompletionItemKind::Snippet).snippet_edit(cap, edit);
.kind(CompletionItemKind::Snippet) item
.snippet_edit(cap, edit)
.clone()
} }
#[cfg(test)] #[cfg(test)]

View file

@ -22,13 +22,13 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
let completion_text = completion_text let completion_text = completion_text
.strip_prefix(ctx.token.to_string().as_str()) .strip_prefix(ctx.token.to_string().as_str())
.unwrap_or(completion_text); .unwrap_or(completion_text);
let mut builder = CompletionItem::new( let mut item = CompletionItem::new(
CompletionKind::Snippet, CompletionKind::Snippet,
ctx.source_range(), ctx.source_range(),
"..Default::default()", "..Default::default()",
); );
builder.insert_text(completion_text).kind(SymbolKind::Field); item.insert_text(completion_text).kind(SymbolKind::Field);
acc.add(builder.build()); item.add_to(acc);
} }
missing_fields missing_fields

View file

@ -8,8 +8,9 @@ use crate::{
}; };
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder { fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
let mut builder = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label); let mut item = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label);
builder.insert_snippet(cap, snippet).kind(CompletionItemKind::Snippet).clone() item.insert_snippet(cap, snippet).kind(CompletionItemKind::Snippet);
item
} }
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) { pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
@ -34,7 +35,7 @@ pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionConte
None => return, None => return,
}; };
let mut test_module_builder = snippet( let mut item = snippet(
ctx, ctx,
cap, cap,
"tmod (Test module)", "tmod (Test module)",
@ -49,10 +50,10 @@ mod tests {
} }
}", }",
); );
test_module_builder.lookup_by("tmod"); item.lookup_by("tmod");
test_module_builder.add_to(acc); item.add_to(acc);
let mut test_function_builder = snippet( let mut item = snippet(
ctx, ctx,
cap, cap,
"tfn (Test function)", "tfn (Test function)",
@ -62,12 +63,11 @@ fn ${1:feature}() {
$0 $0
}", }",
); );
test_function_builder.lookup_by("tfn"); item.lookup_by("tfn");
test_function_builder.add_to(acc); item.add_to(acc);
let macro_rules_builder = let item = snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}");
snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}"); item.add_to(acc);
macro_rules_builder.add_to(acc);
} }
#[cfg(test)] #[cfg(test)]

View file

@ -145,8 +145,8 @@ fn add_function_impl(
format!("fn {}(..)", fn_name) format!("fn {}(..)", fn_name)
}; };
let mut builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label); let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
builder.lookup_by(fn_name).set_documentation(func.docs(ctx.db)); item.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
let completion_kind = if func.self_param(ctx.db).is_some() { let completion_kind = if func.self_param(ctx.db).is_some() {
CompletionItemKind::Method CompletionItemKind::Method
@ -160,15 +160,15 @@ fn add_function_impl(
match ctx.config.snippet_cap { match ctx.config.snippet_cap {
Some(cap) => { Some(cap) => {
let snippet = format!("{} {{\n $0\n}}", function_decl); let snippet = format!("{} {{\n $0\n}}", function_decl);
builder.snippet_edit(cap, TextEdit::replace(range, snippet)); item.snippet_edit(cap, TextEdit::replace(range, snippet));
} }
None => { None => {
let header = format!("{} {{", function_decl); let header = format!("{} {{", function_decl);
builder.text_edit(TextEdit::replace(range, header)); item.text_edit(TextEdit::replace(range, header));
} }
}; };
builder.kind(completion_kind); item.kind(completion_kind);
builder.add_to(acc); item.add_to(acc);
} }
} }
@ -184,14 +184,12 @@ fn add_type_alias_impl(
let range = TextRange::new(type_def_node.text_range().start(), ctx.source_range().end()); let range = TextRange::new(type_def_node.text_range().start(), ctx.source_range().end());
let mut builder = let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()); item.text_edit(TextEdit::replace(range, snippet))
builder
.text_edit(TextEdit::replace(range, snippet))
.lookup_by(alias_name) .lookup_by(alias_name)
.kind(SymbolKind::TypeAlias) .kind(SymbolKind::TypeAlias)
.set_documentation(type_alias.docs(ctx.db)); .set_documentation(type_alias.docs(ctx.db));
builder.add_to(acc); item.add_to(acc);
} }
fn add_const_impl( fn add_const_impl(
@ -209,14 +207,13 @@ fn add_const_impl(
let range = let range =
TextRange::new(const_def_node.text_range().start(), ctx.source_range().end()); TextRange::new(const_def_node.text_range().start(), ctx.source_range().end());
let mut builder = let mut item =
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone()); CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
builder item.text_edit(TextEdit::replace(range, snippet))
.text_edit(TextEdit::replace(range, snippet))
.lookup_by(const_name) .lookup_by(const_name)
.kind(SymbolKind::Const) .kind(SymbolKind::Const)
.set_documentation(const_.docs(ctx.db)); .set_documentation(const_.docs(ctx.db));
builder.add_to(acc); item.add_to(acc);
} }
} }
} }

View file

@ -145,34 +145,33 @@ impl<'a> Render<'a> {
fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem { fn add_field(&mut self, field: hir::Field, ty: &Type) -> CompletionItem {
let is_deprecated = self.ctx.is_deprecated(field); let is_deprecated = self.ctx.is_deprecated(field);
let name = field.name(self.ctx.db()); let name = field.name(self.ctx.db());
let mut builder = CompletionItem::new( let mut item = CompletionItem::new(
CompletionKind::Reference, CompletionKind::Reference,
self.ctx.source_range(), self.ctx.source_range(),
name.to_string(), name.to_string(),
); );
builder item.kind(SymbolKind::Field)
.kind(SymbolKind::Field)
.detail(ty.display(self.ctx.db()).to_string()) .detail(ty.display(self.ctx.db()).to_string())
.set_documentation(field.docs(self.ctx.db())) .set_documentation(field.docs(self.ctx.db()))
.set_deprecated(is_deprecated); .set_deprecated(is_deprecated);
if let Some(relevance) = compute_relevance(&self.ctx, &ty, &name.to_string()) { if let Some(relevance) = compute_relevance(&self.ctx, &ty, &name.to_string()) {
builder.set_relevance(relevance); item.set_relevance(relevance);
} }
builder.build() item.build()
} }
fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem { fn add_tuple_field(&mut self, field: usize, ty: &Type) -> CompletionItem {
let mut builder = CompletionItem::new( let mut item = CompletionItem::new(
CompletionKind::Reference, CompletionKind::Reference,
self.ctx.source_range(), self.ctx.source_range(),
field.to_string(), field.to_string(),
); );
builder.kind(SymbolKind::Field).detail(ty.display(self.ctx.db()).to_string()); item.kind(SymbolKind::Field).detail(ty.display(self.ctx.db()).to_string());
builder.build() item.build()
} }
fn render_resolution( fn render_resolution(

View file

@ -55,13 +55,12 @@ impl<'a> EnumRender<'a> {
} }
fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem { fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
let mut builder = CompletionItem::new( let mut item = CompletionItem::new(
CompletionKind::Reference, CompletionKind::Reference,
self.ctx.source_range(), self.ctx.source_range(),
self.qualified_name.clone(), self.qualified_name.clone(),
); );
builder item.kind(SymbolKind::Variant)
.kind(SymbolKind::Variant)
.set_documentation(self.variant.docs(self.ctx.db())) .set_documentation(self.variant.docs(self.ctx.db()))
.set_deprecated(self.ctx.is_deprecated(self.variant)) .set_deprecated(self.ctx.is_deprecated(self.variant))
.add_import(import_to_add) .add_import(import_to_add)
@ -70,12 +69,12 @@ impl<'a> EnumRender<'a> {
if self.variant_kind == StructKind::Tuple { if self.variant_kind == StructKind::Tuple {
cov_mark::hit!(inserts_parens_for_tuple_enums); cov_mark::hit!(inserts_parens_for_tuple_enums);
let params = Params::Anonymous(self.variant.fields(self.ctx.db()).len()); let params = Params::Anonymous(self.variant.fields(self.ctx.db()).len());
builder.add_call_parens(self.ctx.completion, self.short_qualified_name, params); item.add_call_parens(self.ctx.completion, self.short_qualified_name, params);
} else if self.path.is_some() { } else if self.path.is_some() {
builder.lookup_by(self.short_qualified_name); item.lookup_by(self.short_qualified_name);
} }
builder.build() item.build()
} }
fn detail(&self) -> String { fn detail(&self) -> String {

View file

@ -41,13 +41,12 @@ impl<'a> FunctionRender<'a> {
fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem { fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
let params = self.params(); let params = self.params();
let mut builder = CompletionItem::new( let mut item = CompletionItem::new(
CompletionKind::Reference, CompletionKind::Reference,
self.ctx.source_range(), self.ctx.source_range(),
self.name.clone(), self.name.clone(),
); );
builder item.kind(self.kind())
.kind(self.kind())
.set_documentation(self.ctx.docs(self.func)) .set_documentation(self.ctx.docs(self.func))
.set_deprecated( .set_deprecated(
self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func), self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func),
@ -56,7 +55,7 @@ impl<'a> FunctionRender<'a> {
.add_call_parens(self.ctx.completion, self.name, params) .add_call_parens(self.ctx.completion, self.name, params)
.add_import(import_to_add); .add_import(import_to_add);
builder.build() item.build()
} }
fn detail(&self) -> String { fn detail(&self) -> String {

View file

@ -39,10 +39,9 @@ impl<'a> MacroRender<'a> {
} }
fn render(&self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> { fn render(&self, import_to_add: Option<ImportEdit>) -> Option<CompletionItem> {
let mut builder = let mut item =
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label()); CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), &self.label());
builder item.kind(SymbolKind::Macro)
.kind(SymbolKind::Macro)
.set_documentation(self.docs.clone()) .set_documentation(self.docs.clone())
.set_deprecated(self.ctx.is_deprecated(self.macro_)) .set_deprecated(self.ctx.is_deprecated(self.macro_))
.add_import(import_to_add) .add_import(import_to_add)
@ -53,18 +52,18 @@ impl<'a> MacroRender<'a> {
Some(cap) if needs_bang => { Some(cap) if needs_bang => {
let snippet = self.snippet(); let snippet = self.snippet();
let lookup = self.lookup(); let lookup = self.lookup();
builder.insert_snippet(cap, snippet).lookup_by(lookup); item.insert_snippet(cap, snippet).lookup_by(lookup);
} }
None if needs_bang => { None if needs_bang => {
builder.insert_text(self.banged_name()); item.insert_text(self.banged_name());
} }
_ => { _ => {
cov_mark::hit!(dont_insert_macro_call_parens_unncessary); cov_mark::hit!(dont_insert_macro_call_parens_unncessary);
builder.insert_text(&self.name); item.insert_text(&self.name);
} }
}; };
Some(builder.build()) Some(item.build())
} }
fn needs_bang(&self) -> bool { fn needs_bang(&self) -> bool {

View file

@ -69,20 +69,19 @@ fn build_completion(
ctx: RenderContext<'_>, ctx: RenderContext<'_>,
name: String, name: String,
pat: String, pat: String,
item: impl HasAttrs + Copy, def: impl HasAttrs + Copy,
) -> CompletionItem { ) -> CompletionItem {
let mut completion = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name); let mut item = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), name);
completion item.kind(CompletionItemKind::Binding)
.kind(CompletionItemKind::Binding) .set_documentation(ctx.docs(def))
.set_documentation(ctx.docs(item)) .set_deprecated(ctx.is_deprecated(def))
.set_deprecated(ctx.is_deprecated(item))
.detail(&pat); .detail(&pat);
if let Some(snippet_cap) = ctx.snippet_cap() { if let Some(snippet_cap) = ctx.snippet_cap() {
completion.insert_snippet(snippet_cap, pat); item.insert_snippet(snippet_cap, pat);
} else { } else {
completion.insert_text(pat); item.insert_text(pat);
}; };
completion.build() item.build()
} }
fn render_pat( fn render_pat(