Skip to content

Commit 81ba6af

Browse files
committed
Auto merge of #14538 - Veykril:project-link-fix, r=Veykril
fix: Fix project linking popup appearing for modules that can be linked to a crate should fix #14523
2 parents 01120f1 + 9b60063 commit 81ba6af

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

crates/ide-diagnostics/src/handlers/unlinked_file.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ide_db::{
1010
};
1111
use syntax::{
1212
ast::{self, edit::IndentLevel, HasModuleItem, HasName},
13-
AstNode, TextRange, TextSize,
13+
AstNode, TextRange,
1414
};
1515
use text_edit::TextEdit;
1616

@@ -27,14 +27,28 @@ pub(crate) fn unlinked_file(
2727
) {
2828
// Limit diagnostic to the first few characters in the file. This matches how VS Code
2929
// renders it with the full span, but on other editors, and is less invasive.
30+
let fixes = fixes(ctx, file_id);
31+
// FIXME: This is a hack for the vscode extension to notice whether there is an autofix or not before having to resolve diagnostics.
32+
// This is to prevent project linking popups from appearing when there is an autofix. https://github.com/rust-lang/rust-analyzer/issues/14523
33+
let message = if fixes.is_none() {
34+
"file not included in crate hierarchy"
35+
} else {
36+
"file not included in module tree"
37+
};
38+
3039
let range = ctx.sema.db.parse(file_id).syntax_node().text_range();
31-
// FIXME: This is wrong if one of the first three characters is not ascii: `//Ы`.
32-
let range = range.intersect(TextRange::up_to(TextSize::of("..."))).unwrap_or(range);
40+
let range = FileLoader::file_text(ctx.sema.db, file_id)
41+
.char_indices()
42+
.take(3)
43+
.last()
44+
.map(|(i, _)| i)
45+
.map(|i| TextRange::up_to(i.try_into().unwrap()))
46+
.unwrap_or(range);
3347

3448
acc.push(
35-
Diagnostic::new("unlinked-file", "file not included in module tree", range)
49+
Diagnostic::new("unlinked-file", message, range)
3650
.severity(Severity::WeakWarning)
37-
.with_fixes(fixes(ctx, file_id)),
51+
.with_fixes(fixes),
3852
);
3953
}
4054

editors/code/src/client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ export async function createClient(
125125
typeof diag.code === "string" || typeof diag.code === "number"
126126
? diag.code
127127
: diag.code?.value;
128-
if (value === "unlinked-file" && !unlinkedFiles.includes(uri)) {
128+
if (
129+
value === "unlinked-file" &&
130+
!unlinkedFiles.includes(uri) &&
131+
diag.message != "file not included in module tree"
132+
) {
129133
const config = vscode.workspace.getConfiguration("rust-analyzer");
130134
if (config.get("showUnlinkedFileNotification")) {
131135
unlinkedFiles.push(uri);

0 commit comments

Comments
 (0)