Skip to content

Commit 5ad7764

Browse files
committed
fix(oxc_linter): Make linter file paths clickable within JetBrains terminals
JetBrains terminals don't work reliably with relative paths to hyperlink to the file. Outputting the full file URL instead works consistently.
1 parent ff7111c commit 5ad7764

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ tokio = { version = "1.45.1", default-features = false }
228228
tower-lsp-server = "0.21.1"
229229
tracing-subscriber = "0.3.19"
230230
ureq = { version = "3.0.11", default-features = false }
231+
url = "2.5.4"
231232
walkdir = "2.5.0"
232233

233234
[workspace.metadata.cargo-shear]

crates/oxc_diagnostics/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ doctest = false
2121
[dependencies]
2222
cow-utils = { workspace = true }
2323
miette = { workspace = true }
24+
url = { workspace = true }

crates/oxc_diagnostics/src/service.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66

77
use cow_utils::CowUtils;
88
use miette::LabeledSpan;
9+
use url::Url;
910

1011
use crate::{
1112
Error, NamedSource, OxcDiagnostic, Severity,
@@ -128,20 +129,38 @@ impl DiagnosticService {
128129
/// Wrap [diagnostics] with the source code and path, converting them into [Error]s.
129130
///
130131
/// [diagnostics]: OxcDiagnostic
131-
pub fn wrap_diagnostics<P: AsRef<Path>>(
132+
pub fn wrap_diagnostics<C: AsRef<Path>, P: AsRef<Path>>(
133+
cwd: C,
132134
path: P,
133135
source_text: &str,
134136
source_start: u32,
135137
diagnostics: Vec<OxcDiagnostic>,
136-
) -> (PathBuf, Vec<Error>) {
137-
let path = path.as_ref();
138-
let path_display = path.to_string_lossy();
138+
) -> Vec<Error> {
139+
#[cfg(test)]
140+
let is_jetbrains_terminal = false;
141+
#[cfg(not(test))]
142+
let is_jetbrains_terminal =
143+
std::env::var("TERMINAL_EMULATOR").is_ok_and(|x| x.eq("JetBrains-JediTerm"));
144+
139145
// replace windows \ path separator with posix style one
140146
// reflects what eslint is outputting
141-
let path_display = path_display.cow_replace('\\', "/");
147+
let default_path = path
148+
.as_ref()
149+
.strip_prefix(cwd)
150+
.unwrap_or(path.as_ref())
151+
.to_string_lossy()
152+
.cow_replace('\\', "/")
153+
.to_string();
154+
155+
let file_url = Url::from_file_path(path.as_ref());
156+
let path_display = if is_jetbrains_terminal {
157+
file_url.map(|x| x.to_string()).unwrap_or(default_path)
158+
} else {
159+
default_path
160+
};
142161

143162
let source = Arc::new(NamedSource::new(path_display, source_text.to_owned()));
144-
let diagnostics = diagnostics
163+
diagnostics
145164
.into_iter()
146165
.map(|diagnostic| {
147166
if source_start == 0 {
@@ -166,8 +185,7 @@ impl DiagnosticService {
166185
}
167186
}
168187
})
169-
.collect();
170-
(path.to_path_buf(), diagnostics)
188+
.collect()
171189
}
172190

173191
/// # Panics

crates/oxc_linter/src/service/runtime.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,14 @@ impl<'l> Runtime<'l> {
522522

523523
if !messages.is_empty() {
524524
let errors = messages.into_iter().map(Into::into).collect();
525-
let path = path.strip_prefix(&me.cwd).unwrap_or(path);
526525
let diagnostics = DiagnosticService::wrap_diagnostics(
526+
&me.cwd,
527527
path,
528528
&owner.source_text,
529529
section.source.start,
530530
errors,
531531
);
532-
tx_error.send(Some(diagnostics)).unwrap();
532+
tx_error.send(Some((path.to_path_buf(), diagnostics))).unwrap();
533533
}
534534
}
535535
// If the new source text is owned, that means it was modified,

0 commit comments

Comments
 (0)