From 901c01c2796d4056266fd3479806b0d408f8a602 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Wed, 5 Jun 2024 17:28:08 -0700 Subject: [PATCH] fix: Highlight unlinked files consistently with inactive files Currently, rust-analyzer highlights the entire region when a `cfg` is inactive (e.g. `#[cfg(windows)]` on a Linux machine). However, unlinked files only highlight the first three characters of the file. This was introduced in #8444, but users have repeatedly found themselves with no rust-analyzer support for a file and unsure why (see e.g. #13226 and the intentionally prominent pop-up added in PR #14366). (Anecdotally, we see this issue bite our users regularly, particularly people new to Rust.) Instead, highlight the entire inactive file, but mark it as all as unused. This allows users to hover and run the quickfix from any line. Whilst this is marginally more prominent, it's less invasive than a pop-up, and users do want to know why they're getting no rust-analyzer support in certain files. --- .../ide-diagnostics/src/handlers/unlinked_file.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 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 b9327f85567c..15fb42e0066a 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, TextRange, + AstNode, }; use text_edit::TextEdit; @@ -26,8 +26,6 @@ pub(crate) fn unlinked_file( acc: &mut Vec, file_id: FileId, ) { - // Limit diagnostic to the first few characters in the file. This matches how VS Code - // renders it with the full span, but on other editors, and is less invasive. let fixes = fixes(ctx, file_id); // FIXME: This is a hack for the vscode extension to notice whether there is an autofix or not before having to resolve diagnostics. // This is to prevent project linking popups from appearing when there is an autofix. https://github.com/rust-lang/rust-analyzer/issues/14523 @@ -38,13 +36,6 @@ pub(crate) fn unlinked_file( }; let range = ctx.sema.db.parse(file_id).syntax_node().text_range(); - let 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); acc.push( Diagnostic::new( @@ -52,6 +43,7 @@ pub(crate) fn unlinked_file( message, FileRange { file_id, range }, ) + .with_unused(true) .with_fixes(fixes), ); }