about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-06-19 07:58:53 +0000
committerbors <bors@rust-lang.org>2024-06-19 07:58:53 +0000
commitcf50b298bfbc90f9645abc0f6ef7d363aac9f41e (patch)
tree50980f7cb96fb9508989aa97cc8a4027ee647e35
parent2ad31327f95a67e975b3492cdaadf91438bee8f2 (diff)
parente11ea3cffc6f98d4948bc6c917b4f1374922bace (diff)
downloadrust-cf50b298bfbc90f9645abc0f6ef7d363aac9f41e.tar.gz
rust-cf50b298bfbc90f9645abc0f6ef7d363aac9f41e.zip
Auto merge of #17415 - Wilfred:unlinked_diagnostic_span, r=Veykril
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 file. This autofix heuristic fixes the diagnostic being overwhelming during startup.
-rw-r--r--src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/unlinked_file.rs26
1 files 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 15fb42e0066..cbf50d13f58 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),
     );
 }