Skip to content

Commit f9ab58b

Browse files
committed
feat(language_server): able to set off for run configuration
1 parent 74ad10b commit f9ab58b

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

crates/oxc_language_server/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ This crate provides an [LSP](https://microsoft.github.io/language-server-protoco
1919

2020
These options can be passed with [initialize](#initialize), [workspace/didChangeConfiguration](#workspace/didChangeConfiguration) and [workspace/configuration](#workspace/configuration).
2121

22-
| Option Key | Value(s) | Default | Description |
23-
| ------------------------- | ------------------------------ | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
24-
| `run` | `"onSave" \| "onType"` | `"onType"` | Should the server lint the files when the user is typing or saving |
25-
| `configPath` | `<string>` \| `null` | `null` | Path to a oxlint configuration file, passing a string will disable nested configuration |
26-
| `tsConfigPath` | `<string>` \| `null` | `null` | Path to a TypeScript configuration file. If your `tsconfig.json` is not at the root, alias paths will not be resolve correctly for the `import` plugin |
27-
| `unusedDisableDirectives` | `"allow" \| "warn"` \| "deny"` | `"allow"` | Define how directive comments like `// oxlint-disable-line` should be reported, when no errors would have been reported on that line anyway |
28-
| `typeAware` | `true` \| `false` | `false` | Enables type-aware linting |
29-
| `flags` | `Map<string, string>` | `<empty>` | Special oxc language server flags, currently only one flag key is supported: `disable_nested_config` |
30-
| `fmt.experimental` | `true` \| `false` | `false` | Enables experimental formatting with `oxc_formatter` |
31-
| `fmt.configPath` | `<string>` \| `null` | `null` | Path to a oxfmt configuration file, when `null` is passed, the server will use `.oxfmtrc.json` and the workspace root |
22+
| Option Key | Value(s) | Default | Description |
23+
| ------------------------- | ------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
24+
| `run` | `"onSave" \| "onType"` \| "off" | `"onType"` | Should the server lint the files when the user is typing or saving |
25+
| `configPath` | `<string>` \| `null` | `null` | Path to a oxlint configuration file, passing a string will disable nested configuration |
26+
| `tsConfigPath` | `<string>` \| `null` | `null` | Path to a TypeScript configuration file. If your `tsconfig.json` is not at the root, alias paths will not be resolve correctly for the `import` plugin |
27+
| `unusedDisableDirectives` | `"allow" \| "warn"` \| "deny"` | `"allow"` | Define how directive comments like `// oxlint-disable-line` should be reported, when no errors would have been reported on that line anyway |
28+
| `typeAware` | `true` \| `false` | `false` | Enables type-aware linting |
29+
| `flags` | `Map<string, string>` | `<empty>` | Special oxc language server flags, currently only one flag key is supported: `disable_nested_config` |
30+
| `fmt.experimental` | `true` \| `false` | `false` | Enables experimental formatting with `oxc_formatter` |
31+
| `fmt.configPath` | `<string>` \| `null` | `null` | Path to a oxfmt configuration file, when `null` is passed, the server will use `.oxfmtrc.json` and the workspace root |
3232

3333
## Supported LSP Specifications from Server
3434

crates/oxc_language_server/src/linter/options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub enum Run {
2020
OnSave,
2121
#[default]
2222
OnType,
23+
Off,
2324
}
2425

2526
#[derive(Debug, Default, Serialize, Clone)]

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ impl ServerLinter {
332332
// run only oxlint on type
333333
// tsgolint does not support memory source_text
334334
(ServerLinterRun::OnType, Run::OnType) => (true, false),
335-
// it does not match, run nothing
336-
(ServerLinterRun::OnType, Run::OnSave) => (false, false),
335+
// it is disabled, or it does not match, run nothing
336+
(_, Run::Off) | (ServerLinterRun::OnType, Run::OnSave) => (false, false),
337337
// In onType mode, only TypeScript type checking runs on save
338338
// If type_aware is disabled (tsgo_linter is None), skip everything to preserve diagnostics
339339
(ServerLinterRun::OnSave, Run::OnType) => {

crates/oxc_language_server/src/tester.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ impl Tester<'_> {
162162
match run_type {
163163
Run::OnSave => ServerLinterRun::OnSave,
164164
Run::OnType => ServerLinterRun::OnType,
165+
Run::Off => panic!("Linter is disabled"),
165166
},
166167
)
167168
.await

crates/oxc_language_server/src/worker.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
formatter::{options::FormatOptions, server_formatter::ServerFormatter},
1616
linter::{
1717
error_with_position::DiagnosticReport,
18-
options::LintOptions,
18+
options::{LintOptions, Run},
1919
server_linter::{ServerLinter, ServerLinterRun},
2020
},
2121
options::Options,
@@ -68,7 +68,12 @@ impl WorkspaceWorker {
6868
pub async fn start_worker(&self, options: &Options) {
6969
*self.options.lock().await = Some(options.clone());
7070

71-
*self.server_linter.write().await = Some(ServerLinter::new(&self.root_uri, &options.lint));
71+
if options.lint.run != Run::Off {
72+
debug!("linter enabled");
73+
*self.server_linter.write().await =
74+
Some(ServerLinter::new(&self.root_uri, &options.lint));
75+
}
76+
7277
if options.format.experimental {
7378
debug!("experimental formatter enabled");
7479
*self.server_formatter.write().await =
@@ -320,13 +325,15 @@ impl WorkspaceWorker {
320325
let format_options = options.as_ref().map(|o| o.format.clone()).unwrap_or_default();
321326
let lint_options = options.as_ref().map(|o| o.lint.clone()).unwrap_or_default();
322327

323-
if format_options.experimental {
328+
if format_options.experimental && lint_options.run != Run::Off {
324329
tokio::join!(
325330
self.refresh_server_formatter(&format_options),
326331
self.refresh_server_linter(&lint_options)
327332
);
328-
} else {
333+
} else if lint_options.run != Run::Off {
329334
self.refresh_server_linter(&lint_options).await;
335+
} else if format_options.experimental {
336+
self.refresh_server_formatter(&format_options).await;
330337
}
331338

332339
Some(self.revalidate_diagnostics(files).await)
@@ -599,7 +606,9 @@ mod test_watchers {
599606

600607
mod init_watchers {
601608
use crate::{
602-
formatter::options::FormatOptions, linter::options::LintOptions, options::Options,
609+
formatter::options::FormatOptions,
610+
linter::options::{LintOptions, Run},
611+
options::Options,
603612
worker::test_watchers::Tester,
604613
};
605614

@@ -612,6 +621,20 @@ mod test_watchers {
612621
tester.assert_eq_registration(&registrations[0], "linter", &["**/.oxlintrc.json"]);
613622
}
614623

624+
#[test]
625+
fn test_disabling_linter() {
626+
let tester = Tester::new(
627+
"fixtures/watcher/default",
628+
&Options {
629+
lint: LintOptions { run: Run::Off, ..Default::default() },
630+
..Default::default()
631+
},
632+
);
633+
let registrations = tester.init_watchers();
634+
635+
assert_eq!(registrations.len(), 0);
636+
}
637+
615638
#[test]
616639
fn test_custom_config_path() {
617640
let tester = Tester::new(
@@ -682,6 +705,21 @@ mod test_watchers {
682705
tester.assert_eq_registration(&watchers[1], "formatter", &[".oxfmtrc.json"]);
683706
}
684707

708+
#[test]
709+
fn test_formatter_with_disabled_linter() {
710+
let tester = Tester::new(
711+
"fixtures/watcher/default",
712+
&Options {
713+
lint: LintOptions { run: Run::Off, ..Default::default() },
714+
format: FormatOptions { experimental: true, ..Default::default() },
715+
},
716+
);
717+
let watchers = tester.init_watchers();
718+
719+
assert_eq!(watchers.len(), 1);
720+
tester.assert_eq_registration(&watchers[0], "formatter", &[".oxfmtrc.json"]);
721+
}
722+
685723
#[test]
686724
fn test_formatter_custom_config_path() {
687725
let tester = Tester::new(

0 commit comments

Comments
 (0)