refactor completions to use TextEdit instead of InsertText

This commit is contained in:
gfreezy
2019-01-19 22:02:50 +08:00
parent fa43ef30f4
commit d08e81cdd8
54 changed files with 2320 additions and 313 deletions

View File

@@ -1,6 +1,5 @@
use rustc_hash::FxHashSet;
use ra_syntax::{AstNode, TextUnit};
use ra_syntax::ast::AstNode;
use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext};
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
@@ -13,7 +12,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
};
if let Some(function) = &ctx.function {
let scopes = function.scopes(ctx.db);
complete_fn(acc, &scopes, ctx.offset);
complete_fn(acc, &scopes, ctx);
}
let module_scope = module.scope(ctx.db);
@@ -30,20 +29,24 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
}
})
.for_each(|(name, res)| {
CompletionItem::new(CompletionKind::Reference, name.to_string())
CompletionItem::new(CompletionKind::Reference, ctx, name.to_string())
.from_resolution(ctx, res)
.add_to(acc)
});
}
fn complete_fn(acc: &mut Completions, scopes: &hir::ScopesWithSyntaxMapping, offset: TextUnit) {
fn complete_fn(
acc: &mut Completions,
scopes: &hir::ScopesWithSyntaxMapping,
ctx: &CompletionContext,
) {
let mut shadowed = FxHashSet::default();
scopes
.scope_chain_for_offset(offset)
.scope_chain_for_offset(ctx.offset)
.flat_map(|scope| scopes.scopes.entries(scope).iter())
.filter(|entry| shadowed.insert(entry.name()))
.for_each(|entry| {
CompletionItem::new(CompletionKind::Reference, entry.name().to_string())
CompletionItem::new(CompletionKind::Reference, ctx, entry.name().to_string())
.kind(CompletionItemKind::Binding)
.add_to(acc)
});
@@ -51,15 +54,17 @@ fn complete_fn(acc: &mut Completions, scopes: &hir::ScopesWithSyntaxMapping, off
#[cfg(test)]
mod tests {
use crate::completion::{CompletionKind, check_completion};
use crate::completion::CompletionKind;
use crate::completion::completion_item::check_completion;
fn check_reference_completion(code: &str, expected_completions: &str) {
check_completion(code, expected_completions, CompletionKind::Reference);
fn check_reference_completion(name: &str, code: &str) {
check_completion(name, code, CompletionKind::Reference);
}
#[test]
fn completes_bindings_from_let() {
check_reference_completion(
"bindings_from_let",
r"
fn quux(x: i32) {
let y = 92;
@@ -67,13 +72,13 @@ mod tests {
let z = ();
}
",
r#"y;x;quux "quux($0)""#,
);
}
#[test]
fn completes_bindings_from_if_let() {
check_reference_completion(
"bindings_from_if_let",
r"
fn quux() {
if let Some(x) = foo() {
@@ -85,13 +90,13 @@ mod tests {
}
}
",
r#"b;a;quux "quux()$0""#,
);
}
#[test]
fn completes_bindings_from_for() {
check_reference_completion(
"bindings_from_for",
r"
fn quux() {
for x in &[1, 2, 3] {
@@ -99,13 +104,13 @@ mod tests {
}
}
",
r#"x;quux "quux()$0""#,
);
}
#[test]
fn completes_module_items() {
check_reference_completion(
"module_items",
r"
struct Foo;
enum Baz {}
@@ -113,13 +118,13 @@ mod tests {
<|>
}
",
r#"quux "quux()$0";Foo;Baz"#,
);
}
#[test]
fn completes_module_items_in_nested_modules() {
check_reference_completion(
"module_items_in_nested_modules",
r"
struct Foo;
mod m {
@@ -127,24 +132,24 @@ mod tests {
fn quux() { <|> }
}
",
r#"quux "quux()$0";Bar"#,
);
}
#[test]
fn completes_return_type() {
check_reference_completion(
"return_type",
r"
struct Foo;
fn x() -> <|>
",
r#"Foo;x "x()$0""#,
)
}
#[test]
fn dont_show_both_completions_for_shadowing() {
check_reference_completion(
"dont_show_both_completions_for_shadowing",
r"
fn foo() -> {
let bar = 92;
@@ -154,32 +159,29 @@ mod tests {
}
}
",
r#"bar;foo "foo()$0""#,
)
}
#[test]
fn completes_self_in_methods() {
check_reference_completion(r"impl S { fn foo(&self) { <|> } }", "self")
check_reference_completion("self_in_methods", r"impl S { fn foo(&self) { <|> } }")
}
#[test]
fn inserts_parens_for_function_calls() {
check_reference_completion(
"inserts_parens_for_function_calls1",
r"
fn no_args() {}
fn main() { no_<|> }
",
r#"no_args "no_args()$0"
main "main()$0""#,
);
check_reference_completion(
"inserts_parens_for_function_calls2",
r"
fn with_args(x: i32, y: String) {}
fn main() { with_<|> }
",
r#"main "main()$0"
with_args "with_args($0)""#,
);
}
}