From e11ea3cffc6f98d4948bc6c917b4f1374922bace Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Thu, 13 Jun 2024 11:52:34 -0700 Subject: [PATCH] fix: Only show unlinked-file diagnostic on first line during startup This partially reverts #17350, based on the feedback in #17397. If we don't have an autofix, it's more annoying to highlight the whole line. This heuristic fixes the diagnostic overwhelming the user during startup. --- .../src/handlers/unlinked_file.rs | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unlinked_file.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unlinked_file.rs index 15fb42e0066a4..cbf50d13f5838 100644 --- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unlinked_file.rs +++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unlinked_file.rs @@ -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; @@ -35,7 +35,27 @@ 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( @@ -43,7 +63,7 @@ pub(crate) fn unlinked_file( message, FileRange { file_id, range }, ) - .with_unused(true) + .with_unused(unused) .with_fixes(fixes), ); }