Skip to content

Commit 039883c

Browse files
committed
refactor(language_server): remove ServerLinterDiagnostics
1 parent 4a2477b commit 039883c

File tree

1 file changed

+12
-71
lines changed

1 file changed

+12
-71
lines changed

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 12 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,10 @@ pub struct ServerLinter {
2929
isolated_linter: Arc<Mutex<IsolatedLintHandler>>,
3030
ignore_matcher: LintIgnoreMatcher,
3131
gitignore_glob: Vec<Gitignore>,
32-
diagnostics: ServerLinterDiagnostics,
32+
diagnostics: Arc<ConcurrentHashMap<String, Option<Vec<DiagnosticReport>>>>,
3333
extended_paths: FxHashSet<PathBuf>,
3434
}
3535

36-
#[derive(Debug, Default)]
37-
struct ServerLinterDiagnostics {
38-
isolated_linter: Arc<ConcurrentHashMap<String, Option<Vec<DiagnosticReport>>>>,
39-
}
40-
41-
impl ServerLinterDiagnostics {
42-
pub fn get_diagnostics(&self, path: &str) -> Option<Vec<DiagnosticReport>> {
43-
let mut reports = Vec::new();
44-
let mut found = false;
45-
if let Some(entry) = self.isolated_linter.pin().get(path) {
46-
found = true;
47-
if let Some(diagnostics) = entry {
48-
reports.extend(diagnostics.clone());
49-
}
50-
}
51-
if found { Some(reports) } else { None }
52-
}
53-
54-
pub fn remove_diagnostics(&self, path: &str) {
55-
self.isolated_linter.pin().remove(path);
56-
}
57-
58-
pub fn get_cached_files_of_diagnostics(&self) -> Vec<String> {
59-
self.isolated_linter.pin().keys().cloned().collect::<Vec<_>>()
60-
}
61-
}
62-
6336
impl ServerLinter {
6437
/// # Panics
6538
/// Panics if the root URI cannot be converted to a file path.
@@ -153,7 +126,7 @@ impl ServerLinter {
153126
),
154127
gitignore_glob: Self::create_ignore_glob(&root_path),
155128
extended_paths,
156-
diagnostics: ServerLinterDiagnostics::default(),
129+
diagnostics: Arc::new(ConcurrentHashMap::default()),
157130
}
158131
}
159132

@@ -242,19 +215,20 @@ impl ServerLinter {
242215
}
243216

244217
pub fn remove_diagnostics(&self, uri: &Uri) {
245-
self.diagnostics.remove_diagnostics(&uri.to_string());
218+
self.diagnostics.pin().remove(&uri.to_string());
246219
}
247220

248221
pub fn get_cached_diagnostics(&self, uri: &Uri) -> Option<Vec<DiagnosticReport>> {
249-
self.diagnostics.get_diagnostics(&uri.to_string())
222+
if let Some(diagnostics) = self.diagnostics.pin().get(&uri.to_string()) {
223+
// when the uri is ignored, diagnostics is None.
224+
// We want to return Some(vec![]), so the Worker knows there are no diagnostics for this file.
225+
return Some(diagnostics.clone().unwrap_or_default());
226+
}
227+
None
250228
}
251229

252230
pub fn get_cached_files_of_diagnostics(&self) -> Vec<Uri> {
253-
self.diagnostics
254-
.get_cached_files_of_diagnostics()
255-
.into_iter()
256-
.filter_map(|s| Uri::from_str(&s).ok())
257-
.collect()
231+
self.diagnostics.pin().keys().filter_map(|s| Uri::from_str(s).ok()).collect()
258232
}
259233

260234
pub async fn revalidate_diagnostics(&self, uris: Vec<Uri>) -> Vec<(String, Vec<Diagnostic>)> {
@@ -306,7 +280,7 @@ impl ServerLinter {
306280
isolated_linter.run_single(uri, content.clone())
307281
};
308282

309-
self.diagnostics.isolated_linter.pin().insert(uri.to_string(), diagnostics.clone());
283+
self.diagnostics.pin().insert(uri.to_string(), diagnostics.clone());
310284

311285
diagnostics
312286
}
@@ -360,11 +334,9 @@ mod test {
360334
use std::path::{Path, PathBuf};
361335

362336
use crate::{
363-
ConcurrentHashMap,
364337
linter::{
365-
error_with_position::DiagnosticReport,
366338
options::{LintFixKindFlag, LintOptions, Run, UnusedDisableDirectives},
367-
server_linter::{ServerLinter, ServerLinterDiagnostics},
339+
server_linter::ServerLinter,
368340
},
369341
tester::{Tester, get_file_path},
370342
};
@@ -400,37 +372,6 @@ mod test {
400372
assert!(configs_dirs[0].ends_with("init_nested_configs"));
401373
}
402374

403-
#[test]
404-
fn test_get_diagnostics_found_and_none_entries() {
405-
let key = "file:///test.js".to_string();
406-
407-
// Case 1: Entry present, Some diagnostics
408-
let diag = DiagnosticReport::default();
409-
let diag_map = ConcurrentHashMap::default();
410-
diag_map.pin().insert(key.clone(), Some(vec![diag]));
411-
412-
let server_diag =
413-
super::ServerLinterDiagnostics { isolated_linter: std::sync::Arc::new(diag_map) };
414-
let result = server_diag.get_diagnostics(&key);
415-
assert!(result.is_some());
416-
assert_eq!(result.unwrap().len(), 1);
417-
418-
// Case 2: Entry present, but value is None
419-
let diag_map_none = ConcurrentHashMap::default();
420-
diag_map_none.pin().insert(key.clone(), None);
421-
422-
let server_diag_none =
423-
ServerLinterDiagnostics { isolated_linter: std::sync::Arc::new(diag_map_none) };
424-
let result_none = server_diag_none.get_diagnostics(&key);
425-
assert!(result_none.is_some());
426-
assert_eq!(result_none.unwrap().len(), 0);
427-
428-
// Case 3: No entry at all
429-
let server_diag_empty = ServerLinterDiagnostics::default();
430-
let result_empty = server_diag_empty.get_diagnostics(&key);
431-
assert!(result_empty.is_none());
432-
}
433-
434375
#[test]
435376
fn test_no_errors() {
436377
Tester::new("fixtures/linter/no_errors", None)

0 commit comments

Comments
 (0)