From 7dcbc3b6743629cde57ba011e53d3e1199be2dd7 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Wed, 12 Jun 2024 17:41:01 +0100 Subject: [PATCH] feat(lsp): respect editor indentation options (#24181) --- cli/lsp/language_server.rs | 4 +- tests/integration/lsp_tests.rs | 77 ++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 9 deletions(-) diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index d70b418c02..7e4cf55ab1 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -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() { diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index 62cb84457b..9442852930 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -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 + } }), ); @@ -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(); @@ -9964,7 +10025,7 @@ fn lsp_format_with_config() { }, "options": { "tabSize": 2, - "insertSpaces": true + "insertSpaces": false } }), );