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)| {
CompletionItem::new(label)
CompletionItem::new(CompletionKind::Magic, label)
.lookup_by(lookup)
.kind(CompletionKind::Magic)
.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 {
CompletionItem::new(kw)
.kind(Keyword)
.snippet(snippet)
.build()
CompletionItem::new(Keyword, kw).snippet(snippet).build()
}
#[cfg(test)]

View file

@ -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(())
}

View file

@ -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);
}
}

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()) {
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);
}

View file

@ -6,7 +6,9 @@ pub struct CompletionItem {
label: String,
lookup: 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,
}
@ -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<String>) -> Builder {
pub(crate) fn new(completion_kind: CompletionKind, label: impl Into<String>) -> 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<CompletionItemKind> {
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,
}
}