Skip to content

Commit 1547df2

Browse files
committed
refactor(language_server): remove ServerLinterDiagnostics
1 parent f211d1e commit 1547df2

File tree

1 file changed

+7
-71
lines changed

1 file changed

+7
-71
lines changed

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 7 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,15 @@ 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+
self.diagnostics.pin().get(&uri.to_string()).and_then(std::clone::Clone::clone)
250223
}
251224

252225
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()
226+
self.diagnostics.pin().keys().filter_map(|s| Uri::from_str(s).ok()).collect()
258227
}
259228

260229
pub async fn revalidate_diagnostics(&self, uris: Vec<Uri>) -> Vec<(String, Vec<Diagnostic>)> {
@@ -306,7 +275,7 @@ impl ServerLinter {
306275
isolated_linter.run_single(uri, content.clone())
307276
};
308277

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

311280
diagnostics
312281
}
@@ -360,11 +329,9 @@ mod test {
360329
use std::path::{Path, PathBuf};
361330

362331
use crate::{
363-
ConcurrentHashMap,
364332
linter::{
365-
error_with_position::DiagnosticReport,
366333
options::{LintFixKindFlag, LintOptions, Run, UnusedDisableDirectives},
367-
server_linter::{ServerLinter, ServerLinterDiagnostics},
334+
server_linter::ServerLinter,
368335
},
369336
tester::{Tester, get_file_path},
370337
};
@@ -400,37 +367,6 @@ mod test {
400367
assert!(configs_dirs[0].ends_with("init_nested_configs"));
401368
}
402369

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-
434370
#[test]
435371
fn test_no_errors() {
436372
Tester::new("fixtures/linter/no_errors", None)

0 commit comments

Comments
 (0)