Split out complete_macro_in_item_position

This commit is contained in:
uHOOCCOOHu 2019-09-11 22:44:44 +08:00
parent c033d18700
commit 6353b1621f
No known key found for this signature in database
GPG key ID: CED392DE0C483D00
3 changed files with 52 additions and 37 deletions

View file

@ -12,6 +12,7 @@ mod complete_snippet;
mod complete_path;
mod complete_scope;
mod complete_postfix;
mod complete_macro_in_item_position;
use ra_db::SourceDatabase;
@ -69,5 +70,6 @@ pub(crate) fn completions(db: &db::RootDatabase, position: FilePosition) -> Opti
complete_record_pattern::complete_record_pattern(&mut acc, &ctx);
complete_pattern::complete_pattern(&mut acc, &ctx);
complete_postfix::complete_postfix(&mut acc, &ctx);
complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
Some(acc)
}

View file

@ -0,0 +1,50 @@
use crate::completion::{CompletionContext, Completions};
pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
// Show only macros in top level.
if ctx.is_new_item {
for (name, res) in ctx.analyzer.all_names(ctx.db) {
if res.get_macros().is_some() {
acc.add_resolution(ctx, name.to_string(), &res.only_macros());
}
}
}
}
#[cfg(test)]
mod tests {
use crate::completion::{do_completion, CompletionItem, CompletionKind};
use insta::assert_debug_snapshot;
fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Reference)
}
#[test]
fn completes_macros_as_item() {
assert_debug_snapshot!(
do_reference_completion(
"
//- /main.rs
macro_rules! foo {
() => {}
}
fn foo() {}
<|>
"
),
@r##"[
CompletionItem {
label: "foo",
source_range: [46; 46),
delete: [46; 46),
insert: "foo!",
kind: Macro,
detail: "macro_rules! foo",
},
]"##
);
}
}

View file

@ -6,15 +6,6 @@ use rustc_hash::FxHashMap;
use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
// Show only macros in top level.
if ctx.is_new_item {
for (name, res) in ctx.analyzer.all_names(ctx.db) {
if res.get_macros().is_some() {
acc.add_resolution(ctx, name.to_string(), &res.only_macros());
}
}
}
if !ctx.is_trivial_path {
return;
}
@ -730,34 +721,6 @@ mod tests {
kind: Function,
detail: "fn main()",
},
]"##
);
}
#[test]
fn completes_macros_as_item() {
assert_debug_snapshot!(
do_reference_completion(
"
//- /main.rs
macro_rules! foo {
() => {}
}
fn foo() {}
<|>
"
),
@r##"[
CompletionItem {
label: "foo",
source_range: [46; 46),
delete: [46; 46),
insert: "foo!",
kind: Macro,
detail: "macro_rules! foo",
},
]"##
);
}