From 32b57f7b82d26c40586fad1d08d5bd4f3061e6a8 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Fri, 13 Dec 2024 14:14:55 +0000 Subject: [PATCH] fix(lsp): sql and component file formatting (#27350) --- cli/lsp/documents.rs | 24 ++++ tests/integration/lsp_tests.rs | 213 +++++++++++++++++++++++++++++++-- tests/util/server/src/lsp.rs | 8 ++ 3 files changed, 235 insertions(+), 10 deletions(-) diff --git a/cli/lsp/documents.rs b/cli/lsp/documents.rs index b9ec8ffc46..bdb64c9da3 100644 --- a/cli/lsp/documents.rs +++ b/cli/lsp/documents.rs @@ -65,6 +65,12 @@ pub enum LanguageId { Html, Css, Yaml, + Sql, + Svelte, + Vue, + Astro, + Vento, + Nunjucks, Unknown, } @@ -81,6 +87,12 @@ impl LanguageId { LanguageId::Html => Some("html"), LanguageId::Css => Some("css"), LanguageId::Yaml => Some("yaml"), + LanguageId::Sql => Some("sql"), + LanguageId::Svelte => Some("svelte"), + LanguageId::Vue => Some("vue"), + LanguageId::Astro => Some("astro"), + LanguageId::Vento => Some("vto"), + LanguageId::Nunjucks => Some("njk"), LanguageId::Unknown => None, } } @@ -96,6 +108,12 @@ impl LanguageId { LanguageId::Html => Some("text/html"), LanguageId::Css => Some("text/css"), LanguageId::Yaml => Some("application/yaml"), + LanguageId::Sql => None, + LanguageId::Svelte => None, + LanguageId::Vue => None, + LanguageId::Astro => None, + LanguageId::Vento => None, + LanguageId::Nunjucks => None, LanguageId::Unknown => None, } } @@ -123,6 +141,12 @@ impl FromStr for LanguageId { "html" => Ok(Self::Html), "css" => Ok(Self::Css), "yaml" => Ok(Self::Yaml), + "sql" => Ok(Self::Sql), + "svelte" => Ok(Self::Svelte), + "vue" => Ok(Self::Vue), + "astro" => Ok(Self::Astro), + "vento" => Ok(Self::Vento), + "nunjucks" => Ok(Self::Nunjucks), _ => Ok(Self::Unknown), } } diff --git a/tests/integration/lsp_tests.rs b/tests/integration/lsp_tests.rs index 6142c55888..13a3c0d69b 100644 --- a/tests/integration/lsp_tests.rs +++ b/tests/integration/lsp_tests.rs @@ -11544,8 +11544,7 @@ fn lsp_json_import_with_query_string() { fn lsp_format_markdown() { let context = TestContextBuilder::new().use_temp_cwd().build(); let temp_dir = context.temp_dir(); - let markdown_file = - source_file(temp_dir.path().join("file.md"), "# Hello World"); + let file = source_file(temp_dir.path().join("file.md"), "# Hello World"); let mut client = context.new_lsp_command().build(); client.initialize_default(); @@ -11553,7 +11552,7 @@ fn lsp_format_markdown() { "textDocument/formatting", json!({ "textDocument": { - "uri": markdown_file.url() + "uri": file.url() }, "options": { "tabSize": 2, @@ -11587,14 +11586,13 @@ fn lsp_format_markdown() { fn lsp_format_html() { let context = TestContextBuilder::new().use_temp_cwd().build(); let temp_dir = context.temp_dir(); - let html_file = - source_file(temp_dir.path().join("file.html"), " "); + let file = source_file(temp_dir.path().join("file.html"), " "); let mut client = context.new_lsp_command().build(); client.initialize_default(); let res = client.write_request( "textDocument/formatting", json!({ - "textDocument": { "uri": html_file.url() }, + "textDocument": { "uri": file.url() }, "options": { "tabSize": 2, "insertSpaces": true, @@ -11627,13 +11625,13 @@ fn lsp_format_html() { fn lsp_format_css() { let context = TestContextBuilder::new().use_temp_cwd().build(); let temp_dir = context.temp_dir(); - let css_file = source_file(temp_dir.path().join("file.css"), " foo {}"); + let file = source_file(temp_dir.path().join("file.css"), " foo {}"); let mut client = context.new_lsp_command().build(); client.initialize_default(); let res = client.write_request( "textDocument/formatting", json!({ - "textDocument": { "uri": css_file.url() }, + "textDocument": { "uri": file.url() }, "options": { "tabSize": 2, "insertSpaces": true, @@ -11666,13 +11664,13 @@ fn lsp_format_css() { fn lsp_format_yaml() { let context = TestContextBuilder::new().use_temp_cwd().build(); let temp_dir = context.temp_dir(); - let yaml_file = source_file(temp_dir.path().join("file.yaml"), " foo: 1"); + let file = source_file(temp_dir.path().join("file.yaml"), " foo: 1"); let mut client = context.new_lsp_command().build(); client.initialize_default(); let res = client.write_request( "textDocument/formatting", json!({ - "textDocument": { "uri": yaml_file.url() }, + "textDocument": { "uri": file.url() }, "options": { "tabSize": 2, "insertSpaces": true, @@ -11701,6 +11699,201 @@ fn lsp_format_yaml() { client.shutdown(); } +#[test] +fn lsp_format_sql() { + let context = TestContextBuilder::new().use_temp_cwd().build(); + let temp_dir = context.temp_dir(); + temp_dir.write( + "deno.json", + json!({ + "unstable": ["fmt-sql"], + }) + .to_string(), + ); + let file = source_file( + temp_dir.path().join("file.sql"), + " CREATE TABLE item (id int NOT NULL IDENTITY(1, 1))", + ); + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + let res = client.write_request( + "textDocument/formatting", + json!({ + "textDocument": { "uri": file.url() }, + "options": { + "tabSize": 2, + "insertSpaces": true, + }, + }), + ); + assert_eq!( + res, + json!([ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 2 }, + }, + "newText": "", + }, + { + "range": { + "start": { "line": 0, "character": 52 }, + "end": { "line": 0, "character": 52 }, + }, + "newText": "\n", + }, + ]), + ); + client.shutdown(); +} + +#[test] +fn lsp_format_component() { + let context = TestContextBuilder::new().use_temp_cwd().build(); + let temp_dir = context.temp_dir(); + temp_dir.write( + "deno.json", + json!({ + "unstable": ["fmt-component"], + }) + .to_string(), + ); + let svelte_file = source_file( + temp_dir.path().join("file.svelte"), + " \n", + ); + let vue_file = source_file( + temp_dir.path().join("file.vue"), + " \n", + ); + let astro_file = source_file( + temp_dir.path().join("file.astro"), + " ---\n// foo\n---\n\n", + ); + let vento_file = source_file( + temp_dir.path().join("file.vto"), + " {{ layout \"foo.vto\" }}\n

Foo!

\n{{ /layout }}\n", + ); + let nunjucks_file = source_file( + temp_dir.path().join("file.njk"), + " {% block header %}\n Foo\n{% endblock %}\n", + ); + let mut client = context.new_lsp_command().build(); + client.initialize_default(); + let res = client.write_request( + "textDocument/formatting", + json!({ + "textDocument": { "uri": svelte_file.url() }, + "options": { + "tabSize": 2, + "insertSpaces": true, + }, + }), + ); + assert_eq!( + res, + json!([ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 2 }, + }, + "newText": "", + }, + ]), + ); + let res = client.write_request( + "textDocument/formatting", + json!({ + "textDocument": { "uri": vue_file.url() }, + "options": { + "tabSize": 2, + "insertSpaces": true, + }, + }), + ); + assert_eq!( + res, + json!([ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 2 }, + }, + "newText": "", + }, + ]), + ); + let res = client.write_request( + "textDocument/formatting", + json!({ + "textDocument": { "uri": astro_file.url() }, + "options": { + "tabSize": 2, + "insertSpaces": true, + }, + }), + ); + assert_eq!( + res, + json!([ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 2 }, + }, + "newText": "", + }, + ]), + ); + let res = client.write_request( + "textDocument/formatting", + json!({ + "textDocument": { "uri": vento_file.url() }, + "options": { + "tabSize": 2, + "insertSpaces": true, + }, + }), + ); + assert_eq!( + res, + json!([ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 2 }, + }, + "newText": "", + }, + ]), + ); + let res = client.write_request( + "textDocument/formatting", + json!({ + "textDocument": { "uri": nunjucks_file.url() }, + "options": { + "tabSize": 2, + "insertSpaces": true, + }, + }), + ); + assert_eq!( + res, + json!([ + { + "range": { + "start": { "line": 0, "character": 0 }, + "end": { "line": 0, "character": 2 }, + }, + "newText": "", + }, + ]), + ); + client.shutdown(); +} + #[test] fn lsp_format_with_config() { let context = TestContextBuilder::new().use_temp_cwd().build(); diff --git a/tests/util/server/src/lsp.rs b/tests/util/server/src/lsp.rs index d34deb2161..92169ee644 100644 --- a/tests/util/server/src/lsp.rs +++ b/tests/util/server/src/lsp.rs @@ -1290,6 +1290,14 @@ impl SourceFile { "html" => "html", "css" => "css", "yaml" => "yaml", + "sql" => "sql", + "svelte" => "svelte", + "vue" => "vue", + "astro" => "astro", + "vto" => "vento", + "vento" => "vento", + "njk" => "nunjucks", + "nunjucks" => "nunjucks", other => panic!("unsupported file extension: {other}"), }; Self {