Skip to content

fix: Only show unlinked-file diagnostic on first line during startup #17415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions crates/ide-diagnostics/src/handlers/unlinked_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ide_db::{
use paths::Utf8Component;
use syntax::{
ast::{self, edit::IndentLevel, HasModuleItem, HasName},
AstNode,
AstNode, TextRange,
};
use text_edit::TextEdit;

Expand All @@ -35,15 +35,35 @@ pub(crate) fn unlinked_file(
"file not included in module tree"
};

let range = ctx.sema.db.parse(file_id).syntax_node().text_range();
let mut range = ctx.sema.db.parse(file_id).syntax_node().text_range();
let mut unused = true;

if fixes.is_none() {
// If we don't have a fix, the unlinked-file diagnostic is not
// actionable. This generally means that rust-analyzer hasn't
// finished startup, or we couldn't find the Cargo.toml.
//
// Only show this diagnostic on the first three characters of
// the file, to avoid overwhelming the user during startup.
range = FileLoader::file_text(ctx.sema.db, file_id)
.char_indices()
.take(3)
.last()
.map(|(i, _)| i)
.map(|i| TextRange::up_to(i.try_into().unwrap()))
.unwrap_or(range);
// Prefer a diagnostic underline over graying out the text,
// since we're only highlighting a small region.
unused = false;
}

acc.push(
Diagnostic::new(
DiagnosticCode::Ra("unlinked-file", Severity::WeakWarning),
message,
FileRange { file_id, range },
)
.with_unused(true)
.with_unused(unused)
.with_fixes(fixes),
);
}
Expand Down