Skip to content

Commit 9ec13f6

Browse files
committed
refactor(language_server): move gitignore_glob to ServerLinter (#10762)
1 parent 6de5a43 commit 9ec13f6

File tree

2 files changed

+31
-39
lines changed

2 files changed

+31
-39
lines changed

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33

44
use globset::Glob;
55
use ignore::gitignore::Gitignore;
6-
use log::warn;
6+
use log::{debug, warn};
77
use rustc_hash::{FxBuildHasher, FxHashMap};
88
use tower_lsp_server::lsp_types::Uri;
99

@@ -18,9 +18,9 @@ use crate::{ConcurrentHashMap, Options};
1818

1919
use super::config_walker::ConfigWalker;
2020

21-
#[derive(Clone)]
2221
pub struct ServerLinter {
2322
isolated_linter: Arc<IsolatedLintHandler>,
23+
gitignore_glob: Vec<Gitignore>,
2424
}
2525

2626
impl ServerLinter {
@@ -67,7 +67,7 @@ impl ServerLinter {
6767
nested_configs
6868
}
6969

70-
pub fn create_ignore_glob(root_uri: &Uri, oxlintrc: &Oxlintrc) -> Vec<Gitignore> {
70+
fn create_ignore_glob(root_uri: &Uri, oxlintrc: &Oxlintrc) -> Vec<Gitignore> {
7171
let mut builder = globset::GlobSetBuilder::new();
7272
// Collecting all ignore files
7373
builder.add(Glob::new("**/.eslintignore").unwrap());
@@ -119,7 +119,7 @@ impl ServerLinter {
119119
root_uri: &Uri,
120120
options: &Options,
121121
nested_configs: &ConcurrentHashMap<PathBuf, ConfigStore>,
122-
) -> (Self, Oxlintrc) {
122+
) -> Self {
123123
let root_path = root_uri.to_file_path().unwrap();
124124
let relative_config_path = options.config_path.clone();
125125
let oxlintrc = if relative_config_path.is_some() {
@@ -171,21 +171,37 @@ impl ServerLinter {
171171
Linter::new(lint_options, config_store)
172172
};
173173

174-
let server_linter = ServerLinter::new_with_linter(
174+
let isolated_linter = IsolatedLintHandler::new(
175175
linter,
176176
IsolatedLintHandlerOptions { use_cross_module, root_path: root_path.to_path_buf() },
177177
);
178178

179-
(server_linter, oxlintrc)
179+
Self {
180+
isolated_linter: Arc::new(isolated_linter),
181+
gitignore_glob: Self::create_ignore_glob(root_uri, &oxlintrc),
182+
}
180183
}
181184

182-
fn new_with_linter(linter: Linter, options: IsolatedLintHandlerOptions) -> Self {
183-
let isolated_linter = Arc::new(IsolatedLintHandler::new(linter, options));
184-
185-
Self { isolated_linter }
185+
fn is_ignored(&self, uri: &Uri) -> bool {
186+
for gitignore in &self.gitignore_glob {
187+
if let Some(uri_path) = uri.to_file_path() {
188+
if !uri_path.starts_with(gitignore.path()) {
189+
continue;
190+
}
191+
if gitignore.matched_path_or_any_parents(&uri_path, uri_path.is_dir()).is_ignore() {
192+
debug!("ignored: {uri:?}");
193+
return true;
194+
}
195+
}
196+
}
197+
false
186198
}
187199

188200
pub fn run_single(&self, uri: &Uri, content: Option<String>) -> Option<Vec<DiagnosticReport>> {
201+
if self.is_ignored(uri) {
202+
return None;
203+
}
204+
189205
self.isolated_linter.run_single(uri, content)
190206
}
191207
}

crates/oxc_language_server/src/worker.rs

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{path::PathBuf, str::FromStr, vec};
22

3-
use ignore::gitignore::Gitignore;
43
use log::{debug, info};
54
use oxc_linter::{ConfigStore, ConfigStoreBuilder, Oxlintrc};
65
use rustc_hash::FxBuildHasher;
@@ -24,7 +23,6 @@ pub struct WorkspaceWorker {
2423
server_linter: RwLock<ServerLinter>,
2524
diagnostics_report_map: RwLock<ConcurrentHashMap<String, Vec<DiagnosticReport>>>,
2625
options: Mutex<Options>,
27-
gitignore_glob: Mutex<Vec<Gitignore>>,
2826
nested_configs: RwLock<ConcurrentHashMap<PathBuf, ConfigStore>>,
2927
}
3028

@@ -34,14 +32,12 @@ impl WorkspaceWorker {
3432
root_uri_cell.set(root_uri.clone()).unwrap();
3533

3634
let nested_configs = ServerLinter::create_nested_configs(root_uri, &options);
37-
let (server_linter, oxlintrc) =
38-
ServerLinter::create_server_linter(root_uri, &options, &nested_configs);
35+
let server_linter = ServerLinter::create_server_linter(root_uri, &options, &nested_configs);
3936
Self {
4037
root_uri: root_uri_cell,
4138
server_linter: RwLock::new(server_linter),
4239
diagnostics_report_map: RwLock::new(ConcurrentHashMap::default()),
4340
options: Mutex::new(options),
44-
gitignore_glob: Mutex::new(ServerLinter::create_ignore_glob(root_uri, &oxlintrc)),
4541
nested_configs: RwLock::const_new(nested_configs),
4642
}
4743
}
@@ -71,10 +67,10 @@ impl WorkspaceWorker {
7167
*self.nested_configs.write().await = nested_configs;
7268
}
7369

74-
async fn refresh_linter_config(&self) {
70+
async fn refresh_server_linter(&self) {
7571
let options = self.options.lock().await;
7672
let nested_configs = self.nested_configs.read().await;
77-
let (server_linter, _) = ServerLinter::create_server_linter(
73+
let server_linter = ServerLinter::create_server_linter(
7874
self.root_uri.get().unwrap(),
7975
&options,
8076
&nested_configs,
@@ -114,10 +110,6 @@ impl WorkspaceWorker {
114110
uri: &Uri,
115111
content: Option<String>,
116112
) -> Option<Vec<DiagnosticReport>> {
117-
if self.is_ignored(uri).await {
118-
return None;
119-
}
120-
121113
self.server_linter.read().await.run_single(uri, content)
122114
}
123115

@@ -274,7 +266,7 @@ impl WorkspaceWorker {
274266
}
275267
}
276268

277-
self.refresh_linter_config().await;
269+
self.refresh_server_linter().await;
278270
Some(self.revalidate_diagnostics().await)
279271
}
280272

@@ -301,28 +293,12 @@ impl WorkspaceWorker {
301293
}
302294

303295
if Self::needs_linter_restart(current_option, &changed_options) {
304-
self.refresh_linter_config().await;
296+
self.refresh_server_linter().await;
305297
return Some(self.revalidate_diagnostics().await);
306298
}
307299

308300
None
309301
}
310-
311-
async fn is_ignored(&self, uri: &Uri) -> bool {
312-
let gitignore_globs = &(*self.gitignore_glob.lock().await);
313-
for gitignore in gitignore_globs {
314-
if let Some(uri_path) = uri.to_file_path() {
315-
if !uri_path.starts_with(gitignore.path()) {
316-
continue;
317-
}
318-
if gitignore.matched_path_or_any_parents(&uri_path, uri_path.is_dir()).is_ignore() {
319-
debug!("ignored: {uri:?}");
320-
return true;
321-
}
322-
}
323-
}
324-
false
325-
}
326302
}
327303

328304
fn range_overlaps(a: Range, b: Range) -> bool {

0 commit comments

Comments
 (0)