introduce ComletionItemKind

This commit is contained in:
Aleksey Kladov 2018-12-22 01:27:07 +03:00
parent ebb584ce66
commit 25dda42f37
6 changed files with 28 additions and 34 deletions

View file

@ -34,9 +34,8 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
} }
}) })
.for_each(|(label, lookup)| { .for_each(|(label, lookup)| {
CompletionItem::new(label) CompletionItem::new(CompletionKind::Magic, label)
.lookup_by(lookup) .lookup_by(lookup)
.kind(CompletionKind::Magic)
.add_to(acc) .add_to(acc)
}); });

View file

@ -61,10 +61,7 @@ fn complete_return(fn_def: ast::FnDef, is_stmt: bool) -> Option<CompletionItem>
} }
fn keyword(kw: &str, snippet: &str) -> CompletionItem { fn keyword(kw: &str, snippet: &str) -> CompletionItem {
CompletionItem::new(kw) CompletionItem::new(Keyword, kw).snippet(snippet).build()
.kind(Keyword)
.snippet(snippet)
.build()
} }
#[cfg(test)] #[cfg(test)]

View file

@ -17,11 +17,9 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C
_ => return Ok(()), _ => return Ok(()),
}; };
let module_scope = target_module.scope(ctx.db)?; let module_scope = target_module.scope(ctx.db)?;
module_scope.entries().for_each(|(name, _res)| { module_scope
CompletionItem::new(name.to_string()) .entries()
.kind(Reference) .for_each(|(name, _res)| CompletionItem::new(Reference, name.to_string()).add_to(acc));
.add_to(acc)
});
Ok(()) Ok(())
} }

View file

@ -29,11 +29,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) ->
} }
} }
}) })
.for_each(|(name, _res)| { .for_each(|(name, _res)| CompletionItem::new(Reference, name.to_string()).add_to(acc));
CompletionItem::new(name.to_string())
.kind(Reference)
.add_to(acc)
});
} }
Ok(()) Ok(())
@ -45,13 +41,9 @@ fn complete_fn(acc: &mut Completions, scopes: &hir::FnScopes, offset: TextUnit)
.scope_chain_for_offset(offset) .scope_chain_for_offset(offset)
.flat_map(|scope| scopes.entries(scope).iter()) .flat_map(|scope| scopes.entries(scope).iter())
.filter(|entry| shadowed.insert(entry.name())) .filter(|entry| shadowed.insert(entry.name()))
.for_each(|entry| { .for_each(|entry| CompletionItem::new(Reference, entry.name().to_string()).add_to(acc));
CompletionItem::new(entry.name().to_string())
.kind(Reference)
.add_to(acc)
});
if scopes.self_param.is_some() { if scopes.self_param.is_some() {
CompletionItem::new("self").kind(Reference).add_to(acc); CompletionItem::new(Reference, "self").add_to(acc);
} }
} }

View file

@ -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()) { if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) {
return; return;
} }
CompletionItem::new("pd") CompletionItem::new(Snippet, "pd")
.snippet("eprintln!(\"$0 = {:?}\", $0);") .snippet("eprintln!(\"$0 = {:?}\", $0);")
.kind(Snippet)
.add_to(acc); .add_to(acc);
CompletionItem::new("ppd") CompletionItem::new(Snippet, "ppd")
.snippet("eprintln!(\"$0 = {:#?}\", $0);") .snippet("eprintln!(\"$0 = {:#?}\", $0);")
.kind(Snippet)
.add_to(acc); .add_to(acc);
} }
@ -18,7 +16,7 @@ pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionConte
if !ctx.is_new_item { if !ctx.is_new_item {
return; return;
} }
CompletionItem::new("Test function") CompletionItem::new(Snippet, "Test function")
.lookup_by("tfn") .lookup_by("tfn")
.snippet( .snippet(
"\ "\
@ -27,11 +25,9 @@ fn ${1:feature}() {
$0 $0
}", }",
) )
.kind(Snippet)
.add_to(acc); .add_to(acc);
CompletionItem::new("pub(crate)") CompletionItem::new(Snippet, "pub(crate)")
.snippet("pub(crate) $0") .snippet("pub(crate) $0")
.kind(Snippet)
.add_to(acc); .add_to(acc);
} }

View file

@ -6,7 +6,9 @@ pub struct CompletionItem {
label: String, label: String,
lookup: Option<String>, lookup: Option<String>,
snippet: Option<String>, snippet: Option<String>,
/// Used only internally in test, to check only specific kind of completion. kind: Option<CompletionItemKind>,
/// Used only internally in tests, to check only specific kind of
/// completion.
completion_kind: CompletionKind, completion_kind: CompletionKind,
} }
@ -15,6 +17,12 @@ pub enum InsertText {
Snippet { text: String }, Snippet { text: String },
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompletionItemKind {
Snippet,
Keyword,
}
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub(crate) enum CompletionKind { pub(crate) enum CompletionKind {
/// Parser-based keyword completion. /// Parser-based keyword completion.
@ -24,17 +32,16 @@ pub(crate) enum CompletionKind {
/// "Secret sauce" completions. /// "Secret sauce" completions.
Magic, Magic,
Snippet, Snippet,
Unspecified,
} }
impl CompletionItem { impl CompletionItem {
pub(crate) fn new(label: impl Into<String>) -> Builder { pub(crate) fn new(completion_kind: CompletionKind, label: impl Into<String>) -> Builder {
let label = label.into(); let label = label.into();
Builder { Builder {
label, label,
lookup: None, lookup: None,
snippet: None, snippet: None,
completion_kind: CompletionKind::Unspecified, completion_kind,
} }
} }
/// What user sees in pop-up in the UI. /// What user sees in pop-up in the UI.
@ -57,6 +64,10 @@ impl CompletionItem {
Some(it) => InsertText::Snippet { text: it.clone() }, Some(it) => InsertText::Snippet { text: it.clone() },
} }
} }
pub fn kind(&self) -> Option<CompletionItemKind> {
self.kind
}
} }
/// A helper to make `CompletionItem`s. /// A helper to make `CompletionItem`s.
@ -78,6 +89,7 @@ impl Builder {
label: self.label, label: self.label,
lookup: self.lookup, lookup: self.lookup,
snippet: self.snippet, snippet: self.snippet,
kind: None,
completion_kind: self.completion_kind, completion_kind: self.completion_kind,
} }
} }