This commit is contained in:
Aleksey Kladov 2018-12-22 01:34:22 +03:00
parent 25dda42f37
commit 284e894069
4 changed files with 34 additions and 29 deletions

View File

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

View File

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

View File

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

View File

@ -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)]