Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cli/lsp/documents.rs
Original file line number Diff line number Diff line change
@@ -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),
}
}
213 changes: 203 additions & 10 deletions tests/integration/lsp_tests.rs
Original file line number Diff line number Diff line change
@@ -11544,16 +11544,15 @@ 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();

let res = client.write_request(
"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"), " <html></html>");
let file = source_file(temp_dir.path().join("file.html"), " <html></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"),
" <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]
fn lsp_format_with_config() {
let context = TestContextBuilder::new().use_temp_cwd().build();
8 changes: 8 additions & 0 deletions tests/util/server/src/lsp.rs
Original file line number Diff line number Diff line change
@@ -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 {