2019-09-30 11:58:53 +03:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2020-02-07 23:35:34 +02:00
|
|
|
use ra_assists::insert_use_statement;
|
2019-07-04 23:05:17 +03:00
|
|
|
use ra_syntax::{ast, AstNode, SmolStr};
|
|
|
|
use ra_text_edit::TextEditBuilder;
|
|
|
|
use rustc_hash::FxHashMap;
|
2019-04-22 15:56:28 +03:00
|
|
|
|
2019-07-04 23:05:17 +03:00
|
|
|
use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};
|
2020-02-02 14:06:51 +02:00
|
|
|
use hir::{ModPath, PathKind};
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-01-15 21:09:51 +03:00
|
|
|
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
|
2019-09-06 21:57:11 +03:00
|
|
|
if !ctx.is_trivial_path {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-12 23:35:53 +03:00
|
|
|
ctx.analyzer.process_all_names(ctx.db, &mut |name, res| {
|
|
|
|
acc.add_resolution(ctx, name.to_string(), &res)
|
|
|
|
});
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-09-06 21:57:11 +03:00
|
|
|
// auto-import
|
|
|
|
// We fetch ident from the original file, because we need to pre-filter auto-imports
|
|
|
|
if ast::NameRef::cast(ctx.token.parent()).is_some() {
|
|
|
|
let import_resolver = ImportResolver::new();
|
|
|
|
let import_names = import_resolver.all_names(ctx.token.text());
|
|
|
|
import_names.into_iter().for_each(|(name, path)| {
|
|
|
|
let edit = {
|
|
|
|
let mut builder = TextEditBuilder::default();
|
|
|
|
builder.replace(ctx.source_range(), name.to_string());
|
2020-02-07 23:35:34 +02:00
|
|
|
insert_use_statement(&ctx.token.parent(), &ctx.token.parent(), &path, &mut builder);
|
2019-09-06 21:57:11 +03:00
|
|
|
builder.finish()
|
|
|
|
};
|
2019-04-21 23:55:47 +02:00
|
|
|
|
2019-09-06 21:57:11 +03:00
|
|
|
// Hack: copied this check form conv.rs beacause auto import can produce edits
|
|
|
|
// that invalidate assert in conv_with.
|
|
|
|
if edit
|
|
|
|
.as_atoms()
|
|
|
|
.iter()
|
|
|
|
.filter(|atom| !ctx.source_range().is_subrange(&atom.delete))
|
|
|
|
.all(|atom| ctx.source_range().intersection(&atom.delete).is_none())
|
|
|
|
{
|
|
|
|
CompletionItem::new(
|
|
|
|
CompletionKind::Reference,
|
|
|
|
ctx.source_range(),
|
|
|
|
build_import_label(&name, &path),
|
|
|
|
)
|
|
|
|
.text_edit(edit)
|
|
|
|
.add_to(acc);
|
|
|
|
}
|
|
|
|
});
|
2019-04-15 16:11:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:06:51 +02:00
|
|
|
fn build_import_label(name: &str, path: &ModPath) -> String {
|
2019-04-15 16:11:32 +02:00
|
|
|
let mut buf = String::with_capacity(64);
|
|
|
|
buf.push_str(name);
|
|
|
|
buf.push_str(" (");
|
2020-02-02 14:06:51 +02:00
|
|
|
buf.push_str(&path.to_string());
|
2019-04-15 16:11:32 +02:00
|
|
|
buf.push_str(")");
|
|
|
|
buf
|
|
|
|
}
|
|
|
|
|
2019-04-22 15:56:28 +03:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub(crate) struct ImportResolver {
|
|
|
|
// todo: use fst crate or something like that
|
2020-02-02 14:06:51 +02:00
|
|
|
dummy_names: Vec<(SmolStr, ModPath)>,
|
2019-04-22 15:56:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ImportResolver {
|
|
|
|
pub(crate) fn new() -> Self {
|
2020-02-02 14:27:52 +02:00
|
|
|
use hir::name;
|
|
|
|
|
2019-04-22 15:56:28 +03:00
|
|
|
let dummy_names = vec![
|
2020-02-02 14:06:51 +02:00
|
|
|
(
|
|
|
|
SmolStr::new("fmt"),
|
2020-02-02 14:27:52 +02:00
|
|
|
ModPath { kind: PathKind::Plain, segments: vec![name![std], name![fmt]] },
|
2020-02-02 14:06:51 +02:00
|
|
|
),
|
|
|
|
(
|
|
|
|
SmolStr::new("io"),
|
2020-02-02 14:27:52 +02:00
|
|
|
ModPath { kind: PathKind::Plain, segments: vec![name![std], name![io]] },
|
2020-02-02 14:06:51 +02:00
|
|
|
),
|
|
|
|
(
|
|
|
|
SmolStr::new("iter"),
|
2020-02-02 14:27:52 +02:00
|
|
|
ModPath { kind: PathKind::Plain, segments: vec![name![std], name![iter]] },
|
2020-02-02 14:06:51 +02:00
|
|
|
),
|
|
|
|
(
|
|
|
|
SmolStr::new("hash"),
|
2020-02-02 14:27:52 +02:00
|
|
|
ModPath { kind: PathKind::Plain, segments: vec![name![std], name![hash]] },
|
2020-02-02 14:06:51 +02:00
|
|
|
),
|
2019-04-22 15:56:28 +03:00
|
|
|
(
|
|
|
|
SmolStr::new("Debug"),
|
2020-02-02 14:06:51 +02:00
|
|
|
ModPath {
|
|
|
|
kind: PathKind::Plain,
|
2020-02-02 14:27:52 +02:00
|
|
|
segments: vec![name![std], name![fmt], name![Debug]],
|
2020-02-02 14:06:51 +02:00
|
|
|
},
|
2019-04-22 15:56:28 +03:00
|
|
|
),
|
|
|
|
(
|
|
|
|
SmolStr::new("Display"),
|
2020-02-02 14:06:51 +02:00
|
|
|
ModPath {
|
|
|
|
kind: PathKind::Plain,
|
2020-02-02 14:27:52 +02:00
|
|
|
segments: vec![name![std], name![fmt], name![Display]],
|
2020-02-02 14:06:51 +02:00
|
|
|
},
|
2019-04-22 15:56:28 +03:00
|
|
|
),
|
|
|
|
(
|
|
|
|
SmolStr::new("Hash"),
|
2020-02-02 14:06:51 +02:00
|
|
|
ModPath {
|
|
|
|
kind: PathKind::Plain,
|
2020-02-02 14:27:52 +02:00
|
|
|
segments: vec![name![std], name![hash], name![Hash]],
|
2020-02-02 14:06:51 +02:00
|
|
|
},
|
2019-04-22 15:56:28 +03:00
|
|
|
),
|
|
|
|
(
|
|
|
|
SmolStr::new("Hasher"),
|
2020-02-02 14:06:51 +02:00
|
|
|
ModPath {
|
|
|
|
kind: PathKind::Plain,
|
2020-02-02 14:27:52 +02:00
|
|
|
segments: vec![name![std], name![hash], name![Hasher]],
|
2020-02-02 14:06:51 +02:00
|
|
|
},
|
2019-04-22 15:56:28 +03:00
|
|
|
),
|
|
|
|
(
|
|
|
|
SmolStr::new("Iterator"),
|
2020-02-02 14:06:51 +02:00
|
|
|
ModPath {
|
|
|
|
kind: PathKind::Plain,
|
2020-02-02 14:27:52 +02:00
|
|
|
segments: vec![name![std], name![iter], name![Iterator]],
|
2020-02-02 14:06:51 +02:00
|
|
|
},
|
2019-04-22 15:56:28 +03:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
ImportResolver { dummy_names }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a map of importable items filtered by name.
|
|
|
|
// The map associates item name with its full path.
|
|
|
|
// todo: should return Resolutions
|
2020-02-02 14:06:51 +02:00
|
|
|
pub(crate) fn all_names(&self, name: &str) -> FxHashMap<SmolStr, ModPath> {
|
2019-04-22 15:56:28 +03:00
|
|
|
if name.len() > 1 {
|
|
|
|
self.dummy_names.iter().filter(|(n, _)| n.contains(name)).cloned().collect()
|
|
|
|
} else {
|
|
|
|
FxHashMap::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-07-28 11:24:29 +01:00
|
|
|
use crate::completion::{do_completion, CompletionItem, CompletionKind};
|
2019-08-29 16:49:10 +03:00
|
|
|
use insta::assert_debug_snapshot;
|
2019-01-08 22:33:36 +03:00
|
|
|
|
2019-07-28 11:24:29 +01:00
|
|
|
fn do_reference_completion(code: &str) -> Vec<CompletionItem> {
|
|
|
|
do_completion(code, CompletionKind::Reference)
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_bindings_from_let() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
fn quux(x: i32) {
|
|
|
|
let y = 92;
|
|
|
|
1 + <|>;
|
|
|
|
let z = ();
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "quux(…)",
|
|
|
|
source_range: [91; 91),
|
|
|
|
delete: [91; 91),
|
|
|
|
insert: "quux($0)",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "quux",
|
|
|
|
detail: "fn quux(x: i32)",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "x",
|
|
|
|
source_range: [91; 91),
|
|
|
|
delete: [91; 91),
|
|
|
|
insert: "x",
|
|
|
|
kind: Binding,
|
|
|
|
detail: "i32",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "y",
|
|
|
|
source_range: [91; 91),
|
|
|
|
delete: [91; 91),
|
|
|
|
insert: "y",
|
|
|
|
kind: Binding,
|
|
|
|
detail: "i32",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_bindings_from_if_let() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
fn quux() {
|
|
|
|
if let Some(x) = foo() {
|
|
|
|
let y = 92;
|
|
|
|
};
|
|
|
|
if let Some(a) = bar() {
|
|
|
|
let b = 62;
|
|
|
|
1 + <|>
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-07-28 11:24:29 +01:00
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "a",
|
|
|
|
source_range: [242; 242),
|
|
|
|
delete: [242; 242),
|
|
|
|
insert: "a",
|
|
|
|
kind: Binding,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "b",
|
|
|
|
source_range: [242; 242),
|
|
|
|
delete: [242; 242),
|
|
|
|
insert: "b",
|
|
|
|
kind: Binding,
|
|
|
|
detail: "i32",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "quux()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [242; 242),
|
|
|
|
delete: [242; 242),
|
|
|
|
insert: "quux()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "quux",
|
|
|
|
detail: "fn quux()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_bindings_from_for() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
fn quux() {
|
|
|
|
for x in &[1, 2, 3] {
|
|
|
|
<|>
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-07-28 11:24:29 +01:00
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "quux()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [95; 95),
|
|
|
|
delete: [95; 95),
|
|
|
|
insert: "quux()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "quux",
|
|
|
|
detail: "fn quux()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "x",
|
|
|
|
source_range: [95; 95),
|
|
|
|
delete: [95; 95),
|
|
|
|
insert: "x",
|
|
|
|
kind: Binding,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
2019-02-01 23:06:57 +01:00
|
|
|
#[test]
|
|
|
|
fn completes_generic_params() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
fn quux<T>() {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "T",
|
|
|
|
source_range: [52; 52),
|
|
|
|
delete: [52; 52),
|
|
|
|
insert: "T",
|
|
|
|
kind: TypeParam,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "quux()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [52; 52),
|
|
|
|
delete: [52; 52),
|
|
|
|
insert: "quux()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "quux",
|
|
|
|
detail: "fn quux<T>()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-02-01 23:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_generic_params_in_struct() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
struct X<T> {
|
|
|
|
x: <|>
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-10-08 21:14:52 +03:00
|
|
|
@r###"
|
|
|
|
[
|
2019-11-20 21:55:33 +03:00
|
|
|
CompletionItem {
|
|
|
|
label: "Self",
|
|
|
|
source_range: [54; 54),
|
|
|
|
delete: [54; 54),
|
|
|
|
insert: "Self",
|
|
|
|
kind: TypeParam,
|
|
|
|
},
|
2019-10-08 21:14:52 +03:00
|
|
|
CompletionItem {
|
|
|
|
label: "T",
|
|
|
|
source_range: [54; 54),
|
|
|
|
delete: [54; 54),
|
|
|
|
insert: "T",
|
|
|
|
kind: TypeParam,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 13:03:20 +03:00
|
|
|
label: "X<…>",
|
2019-10-08 21:14:52 +03:00
|
|
|
source_range: [54; 54),
|
|
|
|
delete: [54; 54),
|
|
|
|
insert: "X<$0>",
|
|
|
|
kind: Struct,
|
2019-10-10 13:03:20 +03:00
|
|
|
lookup: "X",
|
2019-10-08 21:14:52 +03:00
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-02-01 23:06:57 +01:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:33:36 +03:00
|
|
|
#[test]
|
2019-10-08 07:25:37 -04:00
|
|
|
fn completes_self_in_enum() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_reference_completion(
|
|
|
|
r"
|
|
|
|
enum X {
|
|
|
|
Y(<|>)
|
|
|
|
}
|
|
|
|
"
|
|
|
|
),
|
2019-10-08 21:14:52 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Self",
|
|
|
|
source_range: [48; 48),
|
|
|
|
delete: [48; 48),
|
|
|
|
insert: "Self",
|
|
|
|
kind: TypeParam,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "X",
|
|
|
|
source_range: [48; 48),
|
|
|
|
delete: [48; 48),
|
|
|
|
insert: "X",
|
|
|
|
kind: Enum,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-10-08 07:25:37 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-01-08 22:33:36 +03:00
|
|
|
fn completes_module_items() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:45:01 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
struct Foo;
|
|
|
|
enum Baz {}
|
|
|
|
fn quux() {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:45:01 +01:00
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Baz",
|
|
|
|
source_range: [105; 105),
|
|
|
|
delete: [105; 105),
|
|
|
|
insert: "Baz",
|
|
|
|
kind: Enum,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "Foo",
|
|
|
|
source_range: [105; 105),
|
|
|
|
delete: [105; 105),
|
|
|
|
insert: "Foo",
|
|
|
|
kind: Struct,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "quux()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [105; 105),
|
|
|
|
delete: [105; 105),
|
|
|
|
insert: "quux()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "quux",
|
|
|
|
detail: "fn quux()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:45:01 +01:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-02-04 22:09:56 +01:00
|
|
|
fn completes_extern_prelude() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
//- /lib.rs
|
|
|
|
use <|>;
|
2019-02-04 22:09:56 +01:00
|
|
|
|
2019-07-28 11:24:29 +01:00
|
|
|
//- /other_crate/lib.rs
|
|
|
|
// nothing here
|
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "other_crate",
|
|
|
|
source_range: [4; 4),
|
|
|
|
delete: [4; 4),
|
|
|
|
insert: "other_crate",
|
|
|
|
kind: Module,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-02-04 22:09:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-01-08 22:33:36 +03:00
|
|
|
fn completes_module_items_in_nested_modules() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
struct Foo;
|
|
|
|
mod m {
|
|
|
|
struct Bar;
|
|
|
|
fn quux() { <|> }
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Bar",
|
|
|
|
source_range: [117; 117),
|
|
|
|
delete: [117; 117),
|
|
|
|
insert: "Bar",
|
|
|
|
kind: Struct,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "quux()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [117; 117),
|
|
|
|
delete: [117; 117),
|
|
|
|
insert: "quux()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "quux",
|
|
|
|
detail: "fn quux()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_return_type() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
struct Foo;
|
|
|
|
fn x() -> <|>
|
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-10-08 21:14:52 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Foo",
|
|
|
|
source_range: [55; 55),
|
|
|
|
delete: [55; 55),
|
|
|
|
insert: "Foo",
|
|
|
|
kind: Struct,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "x()",
|
2019-10-08 21:14:52 +03:00
|
|
|
source_range: [55; 55),
|
|
|
|
delete: [55; 55),
|
|
|
|
insert: "x()$0",
|
|
|
|
kind: Function,
|
2019-10-10 13:03:20 +03:00
|
|
|
lookup: "x",
|
2019-10-08 21:14:52 +03:00
|
|
|
detail: "fn x()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_show_both_completions_for_shadowing() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
r"
|
2019-07-28 11:24:29 +01:00
|
|
|
fn foo() {
|
|
|
|
let bar = 92;
|
|
|
|
{
|
|
|
|
let bar = 62;
|
|
|
|
<|>
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-07-28 11:24:29 +01:00
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "bar",
|
|
|
|
source_range: [146; 146),
|
|
|
|
delete: [146; 146),
|
|
|
|
insert: "bar",
|
|
|
|
kind: Binding,
|
|
|
|
detail: "i32",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "foo()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [146; 146),
|
|
|
|
delete: [146; 146),
|
|
|
|
insert: "foo()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "foo",
|
|
|
|
detail: "fn foo()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_self_in_methods() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(r"impl S { fn foo(&self) { <|> } }"),
|
2019-11-15 12:56:24 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Self",
|
|
|
|
source_range: [25; 25),
|
|
|
|
delete: [25; 25),
|
|
|
|
insert: "Self",
|
|
|
|
kind: TypeParam,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "self",
|
|
|
|
source_range: [25; 25),
|
|
|
|
delete: [25; 25),
|
|
|
|
insert: "self",
|
|
|
|
kind: Binding,
|
|
|
|
detail: "&{unknown}",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|
2019-02-13 20:53:42 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_prelude() {
|
2019-08-29 16:49:10 +03:00
|
|
|
assert_debug_snapshot!(
|
2019-07-28 12:28:14 +01:00
|
|
|
do_reference_completion(
|
|
|
|
"
|
2019-07-28 11:24:29 +01:00
|
|
|
//- /main.rs
|
|
|
|
fn foo() { let x: <|> }
|
2019-02-13 20:53:42 +01:00
|
|
|
|
2019-07-28 11:24:29 +01:00
|
|
|
//- /std/lib.rs
|
|
|
|
#[prelude_import]
|
|
|
|
use prelude::*;
|
2019-02-13 20:53:42 +01:00
|
|
|
|
2019-07-28 11:24:29 +01:00
|
|
|
mod prelude {
|
|
|
|
struct Option;
|
|
|
|
}
|
|
|
|
"
|
2019-07-28 12:28:14 +01:00
|
|
|
),
|
2019-10-08 21:14:52 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "Option",
|
|
|
|
source_range: [18; 18),
|
|
|
|
delete: [18; 18),
|
|
|
|
insert: "Option",
|
|
|
|
kind: Struct,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "foo()",
|
2019-10-08 21:14:52 +03:00
|
|
|
source_range: [18; 18),
|
|
|
|
delete: [18; 18),
|
|
|
|
insert: "foo()$0",
|
|
|
|
kind: Function,
|
2019-10-10 13:03:20 +03:00
|
|
|
lookup: "foo",
|
2019-10-08 21:14:52 +03:00
|
|
|
detail: "fn foo()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "std",
|
|
|
|
source_range: [18; 18),
|
|
|
|
delete: [18; 18),
|
|
|
|
insert: "std",
|
|
|
|
kind: Module,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-07-28 12:28:14 +01:00
|
|
|
);
|
2019-02-13 20:53:42 +01:00
|
|
|
}
|
2019-09-10 13:32:47 +08:00
|
|
|
|
2019-11-09 17:38:08 -08:00
|
|
|
#[test]
|
|
|
|
fn completes_std_prelude_if_core_is_defined() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_reference_completion(
|
|
|
|
"
|
|
|
|
//- /main.rs
|
|
|
|
fn foo() { let x: <|> }
|
|
|
|
|
|
|
|
//- /core/lib.rs
|
|
|
|
#[prelude_import]
|
|
|
|
use prelude::*;
|
|
|
|
|
|
|
|
mod prelude {
|
|
|
|
struct Option;
|
|
|
|
}
|
|
|
|
|
|
|
|
//- /std/lib.rs
|
|
|
|
#[prelude_import]
|
|
|
|
use prelude::*;
|
|
|
|
|
|
|
|
mod prelude {
|
|
|
|
struct String;
|
|
|
|
}
|
|
|
|
"
|
|
|
|
),
|
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "String",
|
|
|
|
source_range: [18; 18),
|
|
|
|
delete: [18; 18),
|
|
|
|
insert: "String",
|
|
|
|
kind: Struct,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "core",
|
|
|
|
source_range: [18; 18),
|
|
|
|
delete: [18; 18),
|
|
|
|
insert: "core",
|
|
|
|
kind: Module,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "foo()",
|
|
|
|
source_range: [18; 18),
|
|
|
|
delete: [18; 18),
|
|
|
|
insert: "foo()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "foo",
|
|
|
|
detail: "fn foo()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "std",
|
|
|
|
source_range: [18; 18),
|
|
|
|
delete: [18; 18),
|
|
|
|
insert: "std",
|
|
|
|
kind: Module,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-10 13:32:47 +08:00
|
|
|
#[test]
|
|
|
|
fn completes_macros_as_value() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_reference_completion(
|
|
|
|
"
|
|
|
|
//- /main.rs
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod m1 {
|
|
|
|
macro_rules! bar {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod m2 {
|
|
|
|
macro_rules! nope {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! baz {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let v = <|>
|
|
|
|
}
|
|
|
|
"
|
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "bar!",
|
|
|
|
source_range: [252; 252),
|
|
|
|
delete: [252; 252),
|
|
|
|
insert: "bar!($0)",
|
|
|
|
kind: Macro,
|
|
|
|
detail: "macro_rules! bar",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "baz!",
|
|
|
|
source_range: [252; 252),
|
|
|
|
delete: [252; 252),
|
|
|
|
insert: "baz!($0)",
|
|
|
|
kind: Macro,
|
|
|
|
detail: "#[macro_export]\nmacro_rules! baz",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "foo!",
|
|
|
|
source_range: [252; 252),
|
|
|
|
delete: [252; 252),
|
|
|
|
insert: "foo!($0)",
|
|
|
|
kind: Macro,
|
|
|
|
detail: "macro_rules! foo",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "m1",
|
|
|
|
source_range: [252; 252),
|
|
|
|
delete: [252; 252),
|
|
|
|
insert: "m1",
|
|
|
|
kind: Module,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "m2",
|
|
|
|
source_range: [252; 252),
|
|
|
|
delete: [252; 252),
|
|
|
|
insert: "m2",
|
|
|
|
kind: Module,
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "main()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [252; 252),
|
|
|
|
delete: [252; 252),
|
|
|
|
insert: "main()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "main",
|
|
|
|
detail: "fn main()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-09-10 13:32:47 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_both_macro_and_value() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_reference_completion(
|
|
|
|
"
|
|
|
|
//- /main.rs
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
"
|
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "foo!",
|
|
|
|
source_range: [49; 49),
|
|
|
|
delete: [49; 49),
|
|
|
|
insert: "foo!($0)",
|
|
|
|
kind: Macro,
|
|
|
|
detail: "macro_rules! foo",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "foo()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [49; 49),
|
|
|
|
delete: [49; 49),
|
|
|
|
insert: "foo()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "foo",
|
|
|
|
detail: "fn foo()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-09-10 13:32:47 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_macros_as_type() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_reference_completion(
|
|
|
|
"
|
|
|
|
//- /main.rs
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let x: <|>
|
|
|
|
}
|
|
|
|
"
|
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "foo!",
|
|
|
|
source_range: [57; 57),
|
|
|
|
delete: [57; 57),
|
|
|
|
insert: "foo!($0)",
|
|
|
|
kind: Macro,
|
|
|
|
detail: "macro_rules! foo",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "main()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [57; 57),
|
|
|
|
delete: [57; 57),
|
|
|
|
insert: "main()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "main",
|
|
|
|
detail: "fn main()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-09-10 13:32:47 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_macros_as_stmt() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_reference_completion(
|
|
|
|
"
|
|
|
|
//- /main.rs
|
|
|
|
macro_rules! foo {
|
|
|
|
() => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
"
|
|
|
|
),
|
2019-10-10 13:03:20 +03:00
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "foo!",
|
|
|
|
source_range: [50; 50),
|
|
|
|
delete: [50; 50),
|
|
|
|
insert: "foo!($0)",
|
|
|
|
kind: Macro,
|
|
|
|
detail: "macro_rules! foo",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
2019-10-10 16:37:56 +03:00
|
|
|
label: "main()",
|
2019-10-10 13:03:20 +03:00
|
|
|
source_range: [50; 50),
|
|
|
|
delete: [50; 50),
|
|
|
|
insert: "main()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "main",
|
|
|
|
detail: "fn main()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
2019-09-10 13:32:47 +08:00
|
|
|
);
|
|
|
|
}
|
2019-12-22 20:12:23 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn completes_local_item() {
|
|
|
|
assert_debug_snapshot!(
|
|
|
|
do_reference_completion(
|
|
|
|
"
|
|
|
|
//- /main.rs
|
|
|
|
fn main() {
|
|
|
|
return f<|>;
|
|
|
|
fn frobnicate() {}
|
|
|
|
}
|
|
|
|
"
|
|
|
|
),
|
|
|
|
@r###"
|
|
|
|
[
|
|
|
|
CompletionItem {
|
|
|
|
label: "frobnicate()",
|
|
|
|
source_range: [23; 24),
|
|
|
|
delete: [23; 24),
|
|
|
|
insert: "frobnicate()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "frobnicate",
|
|
|
|
detail: "fn frobnicate()",
|
|
|
|
},
|
|
|
|
CompletionItem {
|
|
|
|
label: "main()",
|
|
|
|
source_range: [23; 24),
|
|
|
|
delete: [23; 24),
|
|
|
|
insert: "main()$0",
|
|
|
|
kind: Function,
|
|
|
|
lookup: "main",
|
|
|
|
detail: "fn main()",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
"###
|
|
|
|
)
|
|
|
|
}
|
2019-01-08 22:33:36 +03:00
|
|
|
}
|