mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
feat(lsp): html/css/yaml file formatting (#25353)
This commit is contained in:
parent
9e6f41df66
commit
e804175a0a
3 changed files with 154 additions and 0 deletions
|
@ -60,6 +60,9 @@ pub enum LanguageId {
|
||||||
Json,
|
Json,
|
||||||
JsonC,
|
JsonC,
|
||||||
Markdown,
|
Markdown,
|
||||||
|
Html,
|
||||||
|
Css,
|
||||||
|
Yaml,
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,6 +76,9 @@ impl LanguageId {
|
||||||
LanguageId::Json => Some("json"),
|
LanguageId::Json => Some("json"),
|
||||||
LanguageId::JsonC => Some("jsonc"),
|
LanguageId::JsonC => Some("jsonc"),
|
||||||
LanguageId::Markdown => Some("md"),
|
LanguageId::Markdown => Some("md"),
|
||||||
|
LanguageId::Html => Some("html"),
|
||||||
|
LanguageId::Css => Some("css"),
|
||||||
|
LanguageId::Yaml => Some("yaml"),
|
||||||
LanguageId::Unknown => None,
|
LanguageId::Unknown => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,6 +91,9 @@ impl LanguageId {
|
||||||
LanguageId::Tsx => Some("text/tsx"),
|
LanguageId::Tsx => Some("text/tsx"),
|
||||||
LanguageId::Json | LanguageId::JsonC => Some("application/json"),
|
LanguageId::Json | LanguageId::JsonC => Some("application/json"),
|
||||||
LanguageId::Markdown => Some("text/markdown"),
|
LanguageId::Markdown => Some("text/markdown"),
|
||||||
|
LanguageId::Html => Some("text/html"),
|
||||||
|
LanguageId::Css => Some("text/css"),
|
||||||
|
LanguageId::Yaml => Some("application/yaml"),
|
||||||
LanguageId::Unknown => None,
|
LanguageId::Unknown => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,6 +118,9 @@ impl FromStr for LanguageId {
|
||||||
"json" => Ok(Self::Json),
|
"json" => Ok(Self::Json),
|
||||||
"jsonc" => Ok(Self::JsonC),
|
"jsonc" => Ok(Self::JsonC),
|
||||||
"markdown" => Ok(Self::Markdown),
|
"markdown" => Ok(Self::Markdown),
|
||||||
|
"html" => Ok(Self::Html),
|
||||||
|
"css" => Ok(Self::Css),
|
||||||
|
"yaml" => Ok(Self::Yaml),
|
||||||
_ => Ok(Self::Unknown),
|
_ => Ok(Self::Unknown),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10591,6 +10591,145 @@ fn lsp_format_markdown() {
|
||||||
client.shutdown();
|
client.shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lsp_format_html() {
|
||||||
|
let context = TestContextBuilder::new().use_temp_cwd().build();
|
||||||
|
let temp_dir = context.temp_dir();
|
||||||
|
temp_dir.write(
|
||||||
|
"deno.json",
|
||||||
|
json!({
|
||||||
|
"unstable": ["fmt-html"],
|
||||||
|
})
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
let html_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() },
|
||||||
|
"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": 15 },
|
||||||
|
"end": { "line": 0, "character": 15 },
|
||||||
|
},
|
||||||
|
"newText": "\n",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
client.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lsp_format_css() {
|
||||||
|
let context = TestContextBuilder::new().use_temp_cwd().build();
|
||||||
|
let temp_dir = context.temp_dir();
|
||||||
|
temp_dir.write(
|
||||||
|
"deno.json",
|
||||||
|
json!({
|
||||||
|
"unstable": ["fmt-css"],
|
||||||
|
})
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
let css_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() },
|
||||||
|
"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": 8 },
|
||||||
|
"end": { "line": 0, "character": 8 },
|
||||||
|
},
|
||||||
|
"newText": "\n",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
client.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn lsp_format_yaml() {
|
||||||
|
let context = TestContextBuilder::new().use_temp_cwd().build();
|
||||||
|
let temp_dir = context.temp_dir();
|
||||||
|
temp_dir.write(
|
||||||
|
"deno.json",
|
||||||
|
json!({
|
||||||
|
"unstable": ["fmt-yaml"],
|
||||||
|
})
|
||||||
|
.to_string(),
|
||||||
|
);
|
||||||
|
let yaml_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() },
|
||||||
|
"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": 8 },
|
||||||
|
"end": { "line": 0, "character": 8 },
|
||||||
|
},
|
||||||
|
"newText": "\n",
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
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();
|
||||||
|
|
|
@ -1307,6 +1307,9 @@ impl SourceFile {
|
||||||
"tsx" => "typescriptreact",
|
"tsx" => "typescriptreact",
|
||||||
"json" => "json",
|
"json" => "json",
|
||||||
"md" => "markdown",
|
"md" => "markdown",
|
||||||
|
"html" => "html",
|
||||||
|
"css" => "css",
|
||||||
|
"yaml" => "yaml",
|
||||||
other => panic!("unsupported file extension: {other}"),
|
other => panic!("unsupported file extension: {other}"),
|
||||||
};
|
};
|
||||||
Self {
|
Self {
|
||||||
|
|
Loading…
Add table
Reference in a new issue