Skip to content

Commit db43080

Browse files
bors[bot]matklad
andcommitted
Merge #794
794: fix regression in self-referential completion r=flodiebold a=matklad r? @flodiebold Co-authored-by: Aleksey Kladov <[email protected]>
2 parents db6d214 + 8ef8008 commit db43080

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

crates/ra_ide_api/src/completion/complete_path.rs

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use join_to_string::join;
2-
32
use hir::{Docs, Resolution};
3+
use ra_syntax::AstNode;
4+
use test_utils::tested_by;
45

5-
use crate::{
6-
completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
7-
};
6+
use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext};
87

98
pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
109
let path = match &ctx.path_prefix {
@@ -19,6 +18,17 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
1918
hir::ModuleDef::Module(module) => {
2019
let module_scope = module.scope(ctx.db);
2120
for (name, res) in module_scope.entries() {
21+
if Some(module) == ctx.module {
22+
if let Some(import) = res.import {
23+
let path = module.import_source(ctx.db, import);
24+
if path.syntax().range().contains_inclusive(ctx.offset) {
25+
// for `use self::foo<|>`, don't suggest `foo` as a completion
26+
tested_by!(dont_complete_current_use);
27+
continue;
28+
}
29+
}
30+
}
31+
2232
CompletionItem::new(
2333
CompletionKind::Reference,
2434
ctx.source_range(),
@@ -54,22 +64,22 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
5464

5565
#[cfg(test)]
5666
mod tests {
57-
use crate::completion::CompletionKind;
58-
use crate::completion::completion_item::check_completion;
67+
use crate::completion::{
68+
CompletionKind,
69+
completion_item::{check_completion, do_completion},
70+
};
71+
72+
use test_utils::covers;
5973

6074
fn check_reference_completion(code: &str, expected_completions: &str) {
6175
check_completion(code, expected_completions, CompletionKind::Reference);
6276
}
6377

6478
#[test]
65-
#[ignore] // should not complete foo, which currently doesn't work
6679
fn dont_complete_current_use() {
67-
check_reference_completion(
68-
"dont_complete_current_use",
69-
r"
70-
use self::foo<|>;
71-
",
72-
);
80+
covers!(dont_complete_current_use);
81+
let completions = do_completion(r"use self::foo<|>;", CompletionKind::Reference);
82+
assert!(completions.is_empty());
7383
}
7484

7585
#[test]

crates/ra_ide_api/src/completion/completion_item.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,9 @@ fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Opti
306306
}
307307

308308
#[cfg(test)]
309-
pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) {
309+
pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> {
310310
use crate::mock_analysis::{single_file_with_position, analysis_and_position};
311311
use crate::completion::completions;
312-
use insta::assert_debug_snapshot_matches;
313312
let (analysis, position) = if code.contains("//-") {
314313
analysis_and_position(code)
315314
} else {
@@ -320,6 +319,13 @@ pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind
320319
let mut kind_completions: Vec<CompletionItem> =
321320
completion_items.into_iter().filter(|c| c.completion_kind == kind).collect();
322321
kind_completions.sort_by_key(|c| c.label.clone());
322+
kind_completions
323+
}
324+
325+
#[cfg(test)]
326+
pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) {
327+
use insta::assert_debug_snapshot_matches;
328+
let kind_completions = do_completion(code, kind);
323329
assert_debug_snapshot_matches!(test_name, kind_completions);
324330
}
325331

crates/ra_ide_api/src/marks.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ test_utils::marks!(
33
goto_definition_works_for_methods
44
goto_definition_works_for_fields
55
call_info_bad_offset
6+
dont_complete_current_use
67
);

0 commit comments

Comments
 (0)