Skip to content

Commit 61a7ade

Browse files
committed
refactor(language_server): improve file watching for different tools
1 parent 55fd06f commit 61a7ade

File tree

4 files changed

+247
-224
lines changed

4 files changed

+247
-224
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: 16 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,21 @@ impl ServerFormatter {
103103
}
104104
}
105105
}
106+
107+
#[expect(clippy::unused_self)]
108+
pub fn did_change_configuration(
109+
&self,
110+
old_options: &LSPFormatOptions,
111+
new_options: &LSPFormatOptions,
112+
) -> Option<Pattern> {
113+
if old_options != new_options && new_options.experimental {
114+
return Some(
115+
new_options.config_path.as_ref().map_or(FORMAT_CONFIG_FILE, |v| v).to_owned(),
116+
);
117+
}
118+
119+
None
120+
}
106121
}
107122

108123
/// 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: 29 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,33 @@ 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 did_change_configuration(
382+
&self,
383+
old_options: &LSPLintOptions,
384+
new_options: &LSPLintOptions,
385+
) -> Option<Vec<Pattern>> {
386+
if old_options.config_path == new_options.config_path
387+
&& old_options.use_nested_configs() == new_options.use_nested_configs()
388+
{
389+
return None;
390+
}
391+
let mut watchers = vec![
392+
new_options.config_path.as_ref().unwrap_or(&"**/.oxlintrc.json".to_string()).to_owned(),
393+
];
394+
395+
for path in &self.extended_paths {
396+
// ignore .oxlintrc.json files when using nested configs
397+
if path.ends_with(".oxlintrc.json") && new_options.use_nested_configs() {
398+
continue;
399+
}
400+
401+
// let pattern = path.strip_prefix(root_path).unwrap_or(path);
402+
403+
watchers.push(normalize_path(path).to_string_lossy().to_string());
404+
}
405+
Some(watchers)
406+
}
379407
}
380408

381409
#[cfg(test)]

0 commit comments

Comments
 (0)