Skip to content

Commit d7041c1

Browse files
committed
test(language_server): add linebreaks for formatter snapshot (#14173)
1 parent b83b1bd commit d7041c1

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

crates/oxc_language_server/src/formatter/tester.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,27 @@ pub fn get_file_uri(relative_file_path: &str) -> Uri {
1919
}
2020

2121
fn get_snapshot_from_text_edits(edits: &[TextEdit]) -> String {
22-
edits
23-
.iter()
24-
.map(|edit| format!("{:#?},\n\n{:?}", edit.range, edit.new_text))
25-
.collect::<Vec<_>>()
26-
.join("\n")
22+
if edits.len() == 1 {
23+
// Single edit - show range and the actual formatted content with proper indentation
24+
let edit = &edits[0];
25+
let indent = " ".repeat(edit.range.start.character as usize);
26+
let indented_content = format!("{}{}", indent, edit.new_text);
27+
28+
format!("Range: {:#?}\n\n{}", edit.range, indented_content)
29+
} else {
30+
// Multiple edits - show each edit separately
31+
edits
32+
.iter()
33+
.enumerate()
34+
.map(|(i, edit)| {
35+
let indent = " ".repeat(edit.range.start.character as usize);
36+
let indented_content = format!("{}{}", indent, edit.new_text);
37+
38+
format!("Edit {}: Range: {:#?}\n{}", i + 1, edit.range, indented_content)
39+
})
40+
.collect::<Vec<_>>()
41+
.join("\n----------\n")
42+
}
2743
}
2844

2945
/// Testing struct for the [formatter server][crate::formatter::server_formatter::ServerFormatter].
@@ -71,8 +87,8 @@ impl Tester<'_> {
7187

7288
let _ = write!(
7389
snapshot_result,
74-
"########## \nfile: {}/{relative_file_path}\n----------\n{snapshot}\n",
75-
self.relative_root_dir
90+
"========================================\nFile: {}/{}\n========================================\n{}\n",
91+
self.relative_root_dir, relative_file_path, snapshot
7692
);
7793
}
7894

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
source: crates/oxc_language_server/src/formatter/tester.rs
33
---
4-
##########
5-
file: fixtures/formatter/basic/basic.ts
6-
----------
7-
Range {
4+
========================================
5+
File: fixtures/formatter/basic/basic.ts
6+
========================================
7+
Range: Range {
88
start: Position {
99
line: 1,
1010
character: 2,
@@ -13,6 +13,30 @@ Range {
1313
line: 26,
1414
character: 0,
1515
},
16-
},
16+
}
1717

18-
"#name: string;\n\n public get name() {\n return this.#name;\n }\n\n private set name(name: string) {\n this.#name = name;\n }\n}\n\nclass C2 {\n #name: string;\n\n private set name(name: string) {\n this.#name = name;\n }\n\n public get name() {\n return this.#name;\n }\n}\n\nconst c1 = new C1();\nconst c2 = new C2();"
18+
#name: string;
19+
20+
public get name() {
21+
return this.#name;
22+
}
23+
24+
private set name(name: string) {
25+
this.#name = name;
26+
}
27+
}
28+
29+
class C2 {
30+
#name: string;
31+
32+
private set name(name: string) {
33+
this.#name = name;
34+
}
35+
36+
public get name() {
37+
return this.#name;
38+
}
39+
}
40+
41+
const c1 = new C1();
42+
const c2 = new C2();

0 commit comments

Comments
 (0)