Skip to content

Commit 161da30

Browse files
committed
refactor(language-server): move generate_inverted_diagnostics to error_with_position
1 parent 90fd46f commit 161da30

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

crates/oxc_language_server/src/linter/error_with_position.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,43 @@ pub fn message_with_position_to_lsp_diagnostic_report(
146146
},
147147
}
148148
}
149+
150+
pub fn generate_inverted_diagnostics(
151+
diagnostics: &[DiagnosticReport],
152+
uri: &Uri,
153+
) -> Vec<DiagnosticReport> {
154+
let mut inverted_diagnostics = vec![];
155+
for d in diagnostics {
156+
let Some(related_info) = &d.diagnostic.related_information else {
157+
continue;
158+
};
159+
let related_information = Some(vec![DiagnosticRelatedInformation {
160+
location: lsp_types::Location { uri: uri.clone(), range: d.diagnostic.range },
161+
message: "original diagnostic".to_string(),
162+
}]);
163+
for r in related_info {
164+
if r.location.range == d.diagnostic.range {
165+
continue;
166+
}
167+
if r.message.is_empty() {
168+
continue;
169+
}
170+
inverted_diagnostics.push(DiagnosticReport {
171+
diagnostic: lsp_types::Diagnostic {
172+
range: r.location.range,
173+
severity: Some(lsp_types::DiagnosticSeverity::HINT),
174+
code: None,
175+
message: r.message.clone(),
176+
source: d.diagnostic.source.clone(),
177+
code_description: None,
178+
related_information: related_information.clone(),
179+
tags: None,
180+
data: None,
181+
},
182+
fixed_content: PossibleFixContent::None,
183+
rule_name: None,
184+
});
185+
}
186+
}
187+
inverted_diagnostics
188+
}

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use std::{
55

66
use log::debug;
77
use rustc_hash::FxHashSet;
8-
use tower_lsp_server::{
9-
UriExt,
10-
lsp_types::{self, DiagnosticRelatedInformation, DiagnosticSeverity, Uri},
11-
};
8+
use tower_lsp_server::{UriExt, lsp_types::Uri};
129

1310
use oxc_allocator::Allocator;
1411
use oxc_linter::{
@@ -18,7 +15,7 @@ use oxc_linter::{
1815
use oxc_linter::{RuntimeFileSystem, read_to_string};
1916

2017
use super::error_with_position::{
21-
DiagnosticReport, PossibleFixContent, message_with_position_to_lsp_diagnostic_report,
18+
DiagnosticReport, generate_inverted_diagnostics, message_with_position_to_lsp_diagnostic_report,
2219
};
2320

2421
/// smaller subset of LintServiceOptions, which is used by IsolatedLintHandler
@@ -102,41 +99,7 @@ impl IsolatedLintHandler {
10299
let mut diagnostics: Vec<DiagnosticReport> =
103100
errors.iter().map(|e| message_with_position_to_lsp_diagnostic_report(e, uri)).collect();
104101

105-
// a diagnostics connected from related_info to original diagnostic
106-
let mut inverted_diagnostics = vec![];
107-
for d in &diagnostics {
108-
let Some(related_info) = &d.diagnostic.related_information else {
109-
continue;
110-
};
111-
let related_information = Some(vec![DiagnosticRelatedInformation {
112-
location: lsp_types::Location { uri: uri.clone(), range: d.diagnostic.range },
113-
message: "original diagnostic".to_string(),
114-
}]);
115-
for r in related_info {
116-
if r.location.range == d.diagnostic.range {
117-
continue;
118-
}
119-
// If there is no message content for this span, then don't produce an additional diagnostic
120-
// which also has no content. This prevents issues where editors expect diagnostics to have messages.
121-
if r.message.is_empty() {
122-
continue;
123-
}
124-
inverted_diagnostics.push(DiagnosticReport {
125-
diagnostic: lsp_types::Diagnostic {
126-
range: r.location.range,
127-
severity: Some(DiagnosticSeverity::HINT),
128-
code: None,
129-
message: r.message.clone(),
130-
source: d.diagnostic.source.clone(),
131-
code_description: None,
132-
related_information: related_information.clone(),
133-
tags: None,
134-
data: None,
135-
},
136-
fixed_content: PossibleFixContent::None,
137-
});
138-
}
139-
}
102+
let mut inverted_diagnostics = generate_inverted_diagnostics(&diagnostics, uri);
140103
diagnostics.append(&mut inverted_diagnostics);
141104
Some(diagnostics)
142105
}

crates/oxc_language_server/src/linter/tsgo_linter.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hash::FxHashSet;
1111
use tower_lsp_server::{UriExt, lsp_types::Uri};
1212

1313
use crate::linter::error_with_position::{
14-
DiagnosticReport, message_with_position_to_lsp_diagnostic_report,
14+
DiagnosticReport, generate_inverted_diagnostics, message_with_position_to_lsp_diagnostic_report,
1515
};
1616

1717
pub struct TsgoLinter {
@@ -35,12 +35,15 @@ impl TsgoLinter {
3535

3636
let messages = self.state.lint_source(&Arc::from(path.as_os_str()), source_text).ok()?;
3737

38-
Some(
39-
messages
40-
.iter()
41-
.map(|e| message_with_position_to_lsp_diagnostic_report(e, uri))
42-
.collect(),
43-
)
38+
let mut diagnostics: Vec<DiagnosticReport> = messages
39+
.iter()
40+
.map(|e| message_with_position_to_lsp_diagnostic_report(e, uri))
41+
.collect();
42+
43+
let mut inverted_diagnostics = generate_inverted_diagnostics(&diagnostics, uri);
44+
diagnostics.append(&mut inverted_diagnostics);
45+
46+
Some(diagnostics)
4447
}
4548

4649
fn should_lint_path(path: &Path) -> bool {

0 commit comments

Comments
 (0)