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, 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) { pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_trivial_path { 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)) Some(keyword("return", snip))
} }
fn keyword(kw: &str, snippet: &str) -> CompletionItem {
CompletionItem::new(Keyword, kw).snippet(snippet).build()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::completion::{CompletionKind, check_completion}; use crate::completion::{CompletionKind, check_completion};

View file

@ -1,6 +1,6 @@
use crate::{ use crate::{
Cancelable, Cancelable,
completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext}, completion::{CompletionItem, Completions, CompletionKind, CompletionContext},
}; };
pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { 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(()), _ => return Ok(()),
}; };
let module_scope = target_module.scope(ctx.db)?; let module_scope = target_module.scope(ctx.db)?;
module_scope module_scope.entries().for_each(|(name, _res)| {
.entries() CompletionItem::new(CompletionKind::Reference, name.to_string()).add_to(acc)
.for_each(|(name, _res)| CompletionItem::new(Reference, name.to_string()).add_to(acc)); });
Ok(()) Ok(())
} }

View file

@ -3,7 +3,7 @@ use ra_syntax::TextUnit;
use crate::{ use crate::{
Cancelable, Cancelable,
completion::{CompletionItem, Completions, CompletionKind::*, CompletionContext}, completion::{CompletionItem, Completions, CompletionKind, CompletionContext},
}; };
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> Cancelable<()> { 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(()) Ok(())
@ -41,9 +43,11 @@ 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| 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() { 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) { pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) { if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) {
return; return;
} }
CompletionItem::new(Snippet, "pd") snippet("pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
.snippet("eprintln!(\"$0 = {:?}\", $0);") snippet("ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
.add_to(acc);
CompletionItem::new(Snippet, "ppd")
.snippet("eprintln!(\"$0 = {:#?}\", $0);")
.add_to(acc);
} }
pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) { pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_new_item { if !ctx.is_new_item {
return; return;
} }
CompletionItem::new(Snippet, "Test function") snippet(
.lookup_by("tfn") "Test function",
.snippet( "\
"\
#[test] #[test]
fn ${1:feature}() { fn ${1:feature}() {
$0 $0
}", }",
) )
.add_to(acc); .lookup_by("tfn")
CompletionItem::new(Snippet, "pub(crate)") .add_to(acc);
.snippet("pub(crate) $0")
.add_to(acc); snippet("pub(crate)", "pub(crate) $0").add_to(acc);
} }
#[cfg(test)] #[cfg(test)]