Skip to content

Commit

Permalink
fix: Highlight unlinked files consistently with inactive files
Browse files Browse the repository at this point in the history
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 rust-lang#8444, but users have repeatedly found
themselves with no rust-analyzer support for a file and unsure
why (see e.g. rust-lang#13226 and the intentionally prominent pop-up added in
PR rust-lang#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.
  • Loading branch information
Wilfred committed Jun 6, 2024
1 parent 48bbdd6 commit fcecbc5
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 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, TextRange,
AstNode,
};
use text_edit::TextEdit;

Expand All @@ -26,8 +26,6 @@ pub(crate) fn unlinked_file(
acc: &mut Vec<Diagnostic>,
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
Expand All @@ -38,20 +36,14 @@ 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(
DiagnosticCode::Ra("unlinked-file", Severity::WeakWarning),
message,
FileRange { file_id, range },
)
.with_unused(true)
.with_fixes(fixes),
);
}
Expand Down

0 comments on commit fcecbc5

Please sign in to comment.