Skip to content

Commit 43b72c8

Browse files
committed
refactor(language_server): improve file watching for different tools
1 parent 8b322d4 commit 43b72c8

File tree

4 files changed

+293
-280
lines changed

4 files changed

+293
-280
lines changed

crates/oxc_language_server/src/backend.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl LanguageServer for Backend {
327327
continue;
328328
};
329329

330-
let (diagnostics, watchers, formatter_activated) =
330+
let (diagnostics, registrations, unregistrations, formatter_activated) =
331331
worker.did_change_configuration(&option.options).await;
332332

333333
if formatter_activated && self.capabilities.get().is_some_and(|c| c.dynamic_formatting)
@@ -344,23 +344,8 @@ impl LanguageServer for Backend {
344344
}
345345
}
346346

347-
if let Some(watchers) = watchers
348-
&& self.capabilities.get().is_some_and(|capabilities| capabilities.dynamic_watchers)
349-
{
350-
// remove the old watcher
351-
removing_registrations.push(Unregistration {
352-
id: format!("watcher-{}", worker.get_root_uri().as_str()),
353-
method: "workspace/didChangeWatchedFiles".to_string(),
354-
});
355-
// add the new watcher
356-
adding_registrations.push(Registration {
357-
id: format!("watcher-{}", worker.get_root_uri().as_str()),
358-
method: "workspace/didChangeWatchedFiles".to_string(),
359-
register_options: Some(json!(DidChangeWatchedFilesRegistrationOptions {
360-
watchers
361-
})),
362-
});
363-
}
347+
removing_registrations.extend(unregistrations);
348+
adding_registrations.extend(registrations);
364349
}
365350

366351
if !new_diagnostics.is_empty() {

crates/oxc_language_server/src/formatter/server_formatter.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use oxc_formatter::{
99
use oxc_parser::{ParseOptions, Parser};
1010
use tower_lsp_server::{
1111
UriExt,
12-
lsp_types::{Position, Range, TextEdit, Uri},
12+
lsp_types::{Pattern, Position, Range, TextEdit, Uri},
1313
};
1414

1515
use crate::formatter::options::FormatOptions as LSPFormatOptions;
@@ -103,6 +103,23 @@ impl ServerFormatter {
103103
}
104104
}
105105
}
106+
107+
#[expect(clippy::unused_self)]
108+
pub fn get_watcher_patterns(&self, options: &LSPFormatOptions) -> Pattern {
109+
options.config_path.as_ref().map_or(FORMAT_CONFIG_FILE, |v| v).to_owned()
110+
}
111+
112+
pub fn get_changed_watch_patterns(
113+
&self,
114+
old_options: &LSPFormatOptions,
115+
new_options: &LSPFormatOptions,
116+
) -> Option<Pattern> {
117+
if old_options != new_options && new_options.experimental {
118+
return Some(self.get_watcher_patterns(new_options));
119+
}
120+
121+
None
122+
}
106123
}
107124

108125
/// Returns the minimal text edit (start, end, replacement) to transform `source_text` into `formatted_text`

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use std::path::{Path, PathBuf};
22
use std::str::FromStr;
33
use std::sync::Arc;
4+
use std::vec;
45

56
use ignore::gitignore::Gitignore;
67
use log::{debug, warn};
78
use oxc_linter::{AllowWarnDeny, LintIgnoreMatcher};
89
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
910
use tokio::sync::Mutex;
10-
use tower_lsp_server::lsp_types::Uri;
11+
use tower_lsp_server::lsp_types::{Pattern, Uri};
1112

1213
use oxc_linter::{
1314
Config, ConfigStore, ConfigStoreBuilder, ExternalPluginStore, LintOptions, Oxlintrc,
@@ -376,6 +377,39 @@ impl ServerLinter {
376377
// TODO: only the TsgoLinter needs to be dropped or created
377378
|| old_options.type_aware != new_options.type_aware
378379
}
380+
381+
pub fn get_watch_patterns(&self, options: &LSPLintOptions, root_path: &Path) -> Vec<Pattern> {
382+
let mut watchers = vec![
383+
options.config_path.as_ref().unwrap_or(&"**/.oxlintrc.json".to_string()).to_owned(),
384+
];
385+
386+
for path in &self.extended_paths {
387+
// ignore .oxlintrc.json files when using nested configs
388+
if path.ends_with(".oxlintrc.json") && options.use_nested_configs() {
389+
continue;
390+
}
391+
392+
let pattern = path.strip_prefix(root_path).unwrap_or(path);
393+
394+
watchers.push(normalize_path(pattern).to_string_lossy().to_string());
395+
}
396+
watchers
397+
}
398+
399+
pub fn get_changed_watch_patterns(
400+
&self,
401+
old_options: &LSPLintOptions,
402+
new_options: &LSPLintOptions,
403+
root_path: &Path,
404+
) -> Option<Vec<Pattern>> {
405+
if old_options.config_path == new_options.config_path
406+
&& old_options.use_nested_configs() == new_options.use_nested_configs()
407+
{
408+
return None;
409+
}
410+
411+
Some(self.get_watch_patterns(new_options, root_path))
412+
}
379413
}
380414

381415
#[cfg(test)]

0 commit comments

Comments
 (0)