Skip to content

Commit 7e4f449

Browse files
committed
refactor(oxfmt/lsp): move lsp code to apps/oxfmt, remove lsp formatter feature flag (#17462)
> This PR refactors the LSP formatter code by moving it from crates/oxc_language_server to apps/oxfmt, removing the formatter feature flag from the language server crate. This is now possible, because `oxc_language_server` is not anymore shipped #17457
1 parent ef619a2 commit 7e4f449

27 files changed

Lines changed: 59 additions & 68 deletions

Cargo.lock

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/oxfmt/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ doctest = false
2828

2929
[dependencies]
3030
oxc_allocator = { workspace = true, features = ["pool"] }
31+
oxc_data_structures = { workspace = true, features = ["rope"] }
3132
oxc_diagnostics = { workspace = true }
3233
oxc_formatter = { workspace = true }
33-
oxc_language_server = { workspace = true, default-features = false, features = ["formatter"] }
34+
oxc_language_server = { workspace = true, default-features = false }
3435
oxc_napi = { workspace = true }
3536
oxc_parser = { workspace = true }
3637
oxc_span = { workspace = true }
@@ -40,16 +41,19 @@ cow-utils = { workspace = true }
4041
editorconfig-parser = { workspace = true }
4142
ignore = { workspace = true, features = ["simd-accel"] }
4243
json-strip-comments = { workspace = true }
44+
log = { workspace = true }
4345
miette = { workspace = true }
4446
phf = { workspace = true, features = ["macros"] }
4547
rayon = { workspace = true }
48+
serde = { workspace = true }
4649
serde_json = { workspace = true }
4750
simdutf8 = { workspace = true }
4851
sort-package-json = { workspace = true }
4952
oxc-toml = { workspace = true }
5053
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
5154
tracing = { workspace = true }
5255
tracing-subscriber = { workspace = true, features = [] } # Omit the `regex` feature
56+
tower-lsp-server = { workspace = true, features = ["proposed"] }
5357

5458
# NAPI dependencies (conditional on napi feature)
5559
napi = { workspace = true, features = ["async", "serde-json"], optional = true }
@@ -58,6 +62,9 @@ napi-derive = { workspace = true, optional = true }
5862
[build-dependencies]
5963
napi-build = { workspace = true }
6064

65+
[dev-dependencies]
66+
insta = { workspace = true }
67+
6168
[target.'cfg(not(any(target_os = "linux", target_os = "freebsd", target_arch = "arm", target_family = "wasm")))'.dependencies]
6269
mimalloc-safe = { workspace = true, optional = true, features = ["skip_collect_on_exit"] }
6370

apps/oxfmt/src/lsp/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
use oxc_language_server::{ServerFormatterBuilder, run_server};
1+
use oxc_language_server::run_server;
2+
3+
mod options;
4+
mod server_formatter;
5+
#[cfg(test)]
6+
mod tester;
7+
const FORMAT_CONFIG_FILES: &[&str; 2] = &[".oxfmtrc.json", ".oxfmtrc.jsonc"];
28

39
/// Run the language server
410
pub async fn run_lsp() {
511
run_server(
612
"oxfmt".to_string(),
713
env!("CARGO_PKG_VERSION").to_string(),
8-
vec![Box::new(ServerFormatterBuilder)],
14+
vec![Box::new(server_formatter::ServerFormatterBuilder)],
915
)
1016
.await;
1117
}

crates/oxc_language_server/src/formatter/options.rs renamed to apps/oxfmt/src/lsp/options.rs

File renamed without changes.

crates/oxc_language_server/src/formatter/server_formatter.rs renamed to apps/oxfmt/src/lsp/server_formatter.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ use oxc_formatter::{
1111
use oxc_parser::Parser;
1212
use tower_lsp_server::ls_types::{Pattern, Position, Range, ServerCapabilities, TextEdit, Uri};
1313

14-
use crate::{
15-
capabilities::Capabilities,
16-
formatter::{FORMAT_CONFIG_FILES, options::FormatOptions as LSPFormatOptions},
17-
tool::{Tool, ToolBuilder, ToolRestartChanges},
14+
use crate::lsp::{FORMAT_CONFIG_FILES, options::FormatOptions as LSPFormatOptions};
15+
16+
use oxc_language_server::{
17+
Capabilities,
1818
utils::normalize_path,
19+
{Tool, ToolBuilder, ToolRestartChanges},
1920
};
2021

2122
pub struct ServerFormatterBuilder;
@@ -370,7 +371,8 @@ fn load_ignore_paths(cwd: &Path) -> Vec<PathBuf> {
370371

371372
#[cfg(test)]
372373
mod tests_builder {
373-
use crate::{ServerFormatterBuilder, ToolBuilder, capabilities::Capabilities};
374+
use crate::lsp::server_formatter::ServerFormatterBuilder;
375+
use oxc_language_server::{Capabilities, ToolBuilder};
374376

375377
#[test]
376378
fn test_server_capabilities() {
@@ -392,7 +394,7 @@ mod test_watchers {
392394
const FAKE_DIR: &str = "fixtures/formatter/watchers";
393395

394396
mod init_watchers {
395-
use crate::formatter::{server_formatter::test_watchers::FAKE_DIR, tester::Tester};
397+
use crate::lsp::{server_formatter::test_watchers::FAKE_DIR, tester::Tester};
396398
use serde_json::json;
397399

398400
#[test]
@@ -432,10 +434,8 @@ mod test_watchers {
432434
}
433435

434436
mod handle_configuration_change {
435-
use crate::{
436-
ToolRestartChanges,
437-
formatter::{server_formatter::test_watchers::FAKE_DIR, tester::Tester},
438-
};
437+
use crate::lsp::{server_formatter::test_watchers::FAKE_DIR, tester::Tester};
438+
use oxc_language_server::ToolRestartChanges;
439439
use serde_json::json;
440440

441441
#[test]
@@ -465,7 +465,7 @@ mod tests {
465465
use serde_json::json;
466466

467467
use super::compute_minimal_text_edit;
468-
use crate::formatter::tester::Tester;
468+
use crate::lsp::tester::Tester;
469469

470470
#[test]
471471
#[should_panic(expected = "assertion failed")]
@@ -551,7 +551,7 @@ mod tests {
551551
#[test]
552552
fn test_formatter() {
553553
Tester::new(
554-
"fixtures/formatter/basic",
554+
"test/fixtures/lsp/basic",
555555
json!({
556556
"fmt.experimental": true
557557
}),
@@ -562,7 +562,7 @@ mod tests {
562562
#[test]
563563
fn test_root_config_detection() {
564564
Tester::new(
565-
"fixtures/formatter/root_config",
565+
"test/fixtures/lsp/root_config",
566566
json!({
567567
"fmt.experimental": true
568568
}),
@@ -573,7 +573,7 @@ mod tests {
573573
#[test]
574574
fn test_custom_config_path() {
575575
Tester::new(
576-
"fixtures/formatter/custom_config_path",
576+
"test/fixtures/lsp/custom_config_path",
577577
json!({
578578
"fmt.experimental": true,
579579
"fmt.configPath": "./format.json",
@@ -585,7 +585,7 @@ mod tests {
585585
#[test]
586586
fn test_ignore_files() {
587587
Tester::new(
588-
"fixtures/formatter/ignore-file",
588+
"test/fixtures/lsp/ignore-file",
589589
json!({
590590
"fmt.experimental": true
591591
}),
@@ -596,7 +596,7 @@ mod tests {
596596
#[test]
597597
fn test_ignore_pattern() {
598598
Tester::new(
599-
"fixtures/formatter/ignore-pattern",
599+
"test/fixtures/lsp/ignore-pattern",
600600
json!({
601601
"fmt.experimental": true
602602
}),

crates/oxc_language_server/src/formatter/snapshots/fixtures_formatter_basic@basic.ts.snap renamed to apps/oxfmt/src/lsp/snapshots/test_fixtures_lsp_basic@basic.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
source: crates/oxc_language_server/src/formatter/tester.rs
2+
source: apps/oxfmt/src/lsp/tester.rs
33
---
44
========================================
5-
File: fixtures/formatter/basic/basic.ts
5+
File: test/fixtures/lsp/basic/basic.ts
66
========================================
77
Range: Range {
88
start: Position {

crates/oxc_language_server/src/formatter/snapshots/fixtures_formatter_root_config@semicolons-as-needed.ts.snap renamed to apps/oxfmt/src/lsp/snapshots/test_fixtures_lsp_custom_config_path@semicolons-as-needed.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
source: crates/oxc_language_server/src/formatter/tester.rs
2+
source: apps/oxfmt/src/lsp/tester.rs
33
---
44
========================================
5-
File: fixtures/formatter/root_config/semicolons-as-needed.ts
5+
File: test/fixtures/lsp/custom_config_path/semicolons-as-needed.ts
66
========================================
77
Range: Range {
88
start: Position {

crates/oxc_language_server/src/formatter/snapshots/fixtures_formatter_ignore-file@ignored.ts_not-ignored.js.snap renamed to apps/oxfmt/src/lsp/snapshots/test_fixtures_lsp_ignore-file@ignored.ts_not-ignored.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
source: crates/oxc_language_server/src/formatter/tester.rs
2+
source: apps/oxfmt/src/lsp/tester.rs
33
---
44
========================================
5-
File: fixtures/formatter/ignore-file/ignored.ts
5+
File: test/fixtures/lsp/ignore-file/ignored.ts
66
========================================
77
File is ignored
88
========================================
9-
File: fixtures/formatter/ignore-file/not-ignored.js
9+
File: test/fixtures/lsp/ignore-file/not-ignored.js
1010
========================================
1111
Range: Range {
1212
start: Position {

crates/oxc_language_server/src/formatter/snapshots/fixtures_formatter_ignore-pattern@ignored.ts_not-ignored.js.snap renamed to apps/oxfmt/src/lsp/snapshots/test_fixtures_lsp_ignore-pattern@ignored.ts_not-ignored.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
source: crates/oxc_language_server/src/formatter/tester.rs
2+
source: apps/oxfmt/src/lsp/tester.rs
33
---
44
========================================
5-
File: fixtures/formatter/ignore-pattern/ignored.ts
5+
File: test/fixtures/lsp/ignore-pattern/ignored.ts
66
========================================
77
File is ignored
88
========================================
9-
File: fixtures/formatter/ignore-pattern/not-ignored.js
9+
File: test/fixtures/lsp/ignore-pattern/not-ignored.js
1010
========================================
1111
Range: Range {
1212
start: Position {

crates/oxc_language_server/src/formatter/snapshots/fixtures_formatter_custom_config_path@semicolons-as-needed.ts.snap renamed to apps/oxfmt/src/lsp/snapshots/test_fixtures_lsp_root_config@semicolons-as-needed.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
source: crates/oxc_language_server/src/formatter/tester.rs
2+
source: apps/oxfmt/src/lsp/tester.rs
33
---
44
========================================
5-
File: fixtures/formatter/custom_config_path/semicolons-as-needed.ts
5+
File: test/fixtures/lsp/root_config/semicolons-as-needed.ts
66
========================================
77
Range: Range {
88
start: Position {

0 commit comments

Comments
 (0)