1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-18 13:22:55 -05:00

fix(lsp): sql and component file formatting (#27350)

This commit is contained in:
Nayeem Rahman 2024-12-13 14:14:55 +00:00 committed by GitHub
parent 369d68c848
commit 32b57f7b82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 235 additions and 10 deletions

View file

@ -65,6 +65,12 @@ pub enum LanguageId {
Html, Html,
Css, Css,
Yaml, Yaml,
Sql,
Svelte,
Vue,
Astro,
Vento,
Nunjucks,
Unknown, Unknown,
} }
@ -81,6 +87,12 @@ impl LanguageId {
LanguageId::Html => Some("html"), LanguageId::Html => Some("html"),
LanguageId::Css => Some("css"), LanguageId::Css => Some("css"),
LanguageId::Yaml => Some("yaml"), 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, LanguageId::Unknown => None,
} }
} }
@ -96,6 +108,12 @@ impl LanguageId {
LanguageId::Html => Some("text/html"), LanguageId::Html => Some("text/html"),
LanguageId::Css => Some("text/css"), LanguageId::Css => Some("text/css"),
LanguageId::Yaml => Some("application/yaml"), 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, LanguageId::Unknown => None,
} }
} }
@ -123,6 +141,12 @@ impl FromStr for LanguageId {
"html" => Ok(Self::Html), "html" => Ok(Self::Html),
"css" => Ok(Self::Css), "css" => Ok(Self::Css),
"yaml" => Ok(Self::Yaml), "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), _ => Ok(Self::Unknown),
} }
} }

View file

@ -11544,8 +11544,7 @@ fn lsp_json_import_with_query_string() {
fn lsp_format_markdown() { fn lsp_format_markdown() {
let context = TestContextBuilder::new().use_temp_cwd().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
let markdown_file = let file = source_file(temp_dir.path().join("file.md"), "# Hello World");
source_file(temp_dir.path().join("file.md"), "# Hello World");
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
@ -11553,7 +11552,7 @@ fn lsp_format_markdown() {
"textDocument/formatting", "textDocument/formatting",
json!({ json!({
"textDocument": { "textDocument": {
"uri": markdown_file.url() "uri": file.url()
}, },
"options": { "options": {
"tabSize": 2, "tabSize": 2,
@ -11587,14 +11586,13 @@ fn lsp_format_markdown() {
fn lsp_format_html() { fn lsp_format_html() {
let context = TestContextBuilder::new().use_temp_cwd().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); let temp_dir = context.temp_dir();
let html_file = let file = source_file(temp_dir.path().join("file.html"), " <html></html>");
source_file(temp_dir.path().join("file.html"), " <html></html>");
let mut client = context.new_lsp_command().build(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let res = client.write_request( let res = client.write_request(
"textDocument/formatting", "textDocument/formatting",
json!({ json!({
"textDocument": { "uri": html_file.url() }, "textDocument": { "uri": file.url() },
"options": { "options": {
"tabSize": 2, "tabSize": 2,
"insertSpaces": true, "insertSpaces": true,
@ -11627,13 +11625,13 @@ fn lsp_format_html() {
fn lsp_format_css() { fn lsp_format_css() {
let context = TestContextBuilder::new().use_temp_cwd().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); 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(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let res = client.write_request( let res = client.write_request(
"textDocument/formatting", "textDocument/formatting",
json!({ json!({
"textDocument": { "uri": css_file.url() }, "textDocument": { "uri": file.url() },
"options": { "options": {
"tabSize": 2, "tabSize": 2,
"insertSpaces": true, "insertSpaces": true,
@ -11666,13 +11664,13 @@ fn lsp_format_css() {
fn lsp_format_yaml() { fn lsp_format_yaml() {
let context = TestContextBuilder::new().use_temp_cwd().build(); let context = TestContextBuilder::new().use_temp_cwd().build();
let temp_dir = context.temp_dir(); 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(); let mut client = context.new_lsp_command().build();
client.initialize_default(); client.initialize_default();
let res = client.write_request( let res = client.write_request(
"textDocument/formatting", "textDocument/formatting",
json!({ json!({
"textDocument": { "uri": yaml_file.url() }, "textDocument": { "uri": file.url() },
"options": { "options": {
"tabSize": 2, "tabSize": 2,
"insertSpaces": true, "insertSpaces": true,
@ -11701,6 +11699,201 @@ fn lsp_format_yaml() {
client.shutdown(); 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"),
" <script module>\n // foo\n</script>\n",
);
let vue_file = source_file(
temp_dir.path().join("file.vue"),
" <script setup>\n// foo\n</script>\n",
);
let astro_file = source_file(
temp_dir.path().join("file.astro"),
" ---\n// foo\n---\n<html></html>\n",
);
let vento_file = source_file(
temp_dir.path().join("file.vto"),
" {{ layout \"foo.vto\" }}\n <h1>Foo!</h1>\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] #[test]
fn lsp_format_with_config() { fn lsp_format_with_config() {
let context = TestContextBuilder::new().use_temp_cwd().build(); let context = TestContextBuilder::new().use_temp_cwd().build();

View file

@ -1290,6 +1290,14 @@ impl SourceFile {
"html" => "html", "html" => "html",
"css" => "css", "css" => "css",
"yaml" => "yaml", "yaml" => "yaml",
"sql" => "sql",
"svelte" => "svelte",
"vue" => "vue",
"astro" => "astro",
"vto" => "vento",
"vento" => "vento",
"njk" => "nunjucks",
"nunjucks" => "nunjucks",
other => panic!("unsupported file extension: {other}"), other => panic!("unsupported file extension: {other}"),
}; };
Self { Self {