Skip to content

Commit

Permalink
feat(lsp): respect editor indentation options (denoland#24181)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn committed Jun 12, 2024
1 parent 05ec506 commit 85e9a79
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
4 changes: 3 additions & 1 deletion cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,12 +1331,14 @@ impl Inner {

// spawn a blocking task to allow doing other work while this is occurring
let text_edits = deno_core::unsync::spawn_blocking({
let fmt_options = self
let mut fmt_options = self
.config
.tree
.fmt_options_for_specifier(&specifier)
.options
.clone();
fmt_options.use_tabs = Some(!params.options.insert_spaces);
fmt_options.indent_width = Some(params.options.tab_size as u8);
let document = document.clone();
move || {
let format_result = match document.maybe_parsed_source() {
Expand Down
77 changes: 69 additions & 8 deletions tests/integration/lsp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9765,13 +9765,13 @@ fn lsp_format_json() {
let res = client.write_request(
"textDocument/formatting",
json!({
"textDocument": {
"uri": json_file.uri(),
},
"options": {
"tabSize": 2,
"insertSpaces": true
}
"textDocument": {
"uri": json_file.uri(),
},
"options": {
"tabSize": 2,
"insertSpaces": true
}
}),
);

Expand Down Expand Up @@ -9802,6 +9802,67 @@ fn lsp_format_json() {
client.shutdown();
}

#[test]
fn lsp_format_editor_options() {
let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir();
let file = source_file(
temp_dir.path().join("file.ts"),
"if (true) {\n console.log();\n}\n",
);
let mut client = context.new_lsp_command().build();
client.initialize_default();
let res = client.write_request(
"textDocument/formatting",
json!({
"textDocument": {
"uri": file.uri(),
},
"options": {
"tabSize": 4,
"insertSpaces": true,
},
}),
);
assert_eq!(
res,
json!([
{
"range": {
"start": { "line": 1, "character": 0 },
"end": { "line": 1, "character": 0 },
},
"newText": " ",
},
])
);
let res = client.write_request(
"textDocument/formatting",
json!({
"textDocument": {
"uri": file.uri(),
},
"options": {
"tabSize": 2,
"insertSpaces": false,
},
}),
);
assert_eq!(
res,
json!([
{
"range": {
"start": { "line": 1, "character": 0 },
"end": { "line": 1, "character": 2 },
},
"newText": "\t",
},
])
);
client.shutdown();
}

#[test]
fn lsp_json_no_diagnostics() {
let context = TestContextBuilder::new().use_temp_cwd().build();
Expand Down Expand Up @@ -9964,7 +10025,7 @@ fn lsp_format_with_config() {
},
"options": {
"tabSize": 2,
"insertSpaces": true
"insertSpaces": false
}
}),
);
Expand Down

0 comments on commit 85e9a79

Please sign in to comment.