Skip to content

Commit

Permalink
Auto merge of rust-lang#17415 - Wilfred:unlinked_diagnostic_span, r=V…
Browse files Browse the repository at this point in the history
…eykril

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

This partially reverts rust-lang#17350, based on the feedback in rust-lang#17397.

If we don't have an autofix, it's more annoying to highlight the whole file. This autofix heuristic fixes the diagnostic being overwhelming during startup.
  • Loading branch information
bors committed Jun 19, 2024
2 parents 2ad3132 + e11ea3c commit cf50b29
Showing 1 changed file with 23 additions and 3 deletions.
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

0 comments on commit cf50b29

Please sign in to comment.