Skip to content

Commit

Permalink
feat(fmt): support HTML, Svelte, Vue, Astro and Angular (#25019)
Browse files Browse the repository at this point in the history
This commit adds capability to format HTML, Svelte, Vue, Astro and Angular
files.

"--unstable-html" is required to format HTML files, and "--unstable-component"
flag is needed to format other formats. These can also be specified in the config
file.

Close #25015
  • Loading branch information
g-plane authored Aug 14, 2024
1 parent 22a834f commit 3a3315c
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 9 deletions.
31 changes: 27 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ libc.workspace = true
libz-sys.workspace = true
log = { workspace = true, features = ["serde"] }
lsp-types.workspace = true
malva = "=0.8.0"
malva = "=0.9.0"
markup_fmt = "=0.12.0"
memmem.workspace = true
monch.workspace = true
notify.workspace = true
Expand Down
45 changes: 44 additions & 1 deletion cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ pub struct FmtFlags {
pub no_semicolons: Option<bool>,
pub watch: Option<WatchFlags>,
pub unstable_css: bool,
pub unstable_html: bool,
pub unstable_component: bool,
pub unstable_yaml: bool,
}

Expand Down Expand Up @@ -2104,7 +2106,8 @@ Ignore formatting a file by adding an ignore comment at the top of the file:
.default_value("ts")
.value_parser([
"ts", "tsx", "js", "jsx", "md", "json", "jsonc", "css", "scss",
"sass", "less", "yml", "yaml", "ipynb",
"sass", "less", "html", "svelte", "vue", "astro", "yml", "yaml",
"ipynb",
]),
)
.arg(
Expand Down Expand Up @@ -2188,6 +2191,20 @@ Ignore formatting a file by adding an ignore comment at the top of the file:
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("unstable-html")
.long("unstable-html")
.help("Enable formatting HTML files.")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("unstable-component")
.long("unstable-component")
.help("Enable formatting Svelte, Vue, Astro and Angular files.")
.value_parser(FalseyValueParser::new())
.action(ArgAction::SetTrue),
)
.arg(
Arg::new("unstable-yaml")
.long("unstable-yaml")
Expand Down Expand Up @@ -4058,6 +4075,8 @@ fn fmt_parse(flags: &mut Flags, matches: &mut ArgMatches) {
let prose_wrap = matches.remove_one::<String>("prose-wrap");
let no_semicolons = matches.remove_one::<bool>("no-semicolons");
let unstable_css = matches.get_flag("unstable-css");
let unstable_html = matches.get_flag("unstable-html");
let unstable_component = matches.get_flag("unstable-component");
let unstable_yaml = matches.get_flag("unstable-yaml");

flags.subcommand = DenoSubcommand::Fmt(FmtFlags {
Expand All @@ -4071,6 +4090,8 @@ fn fmt_parse(flags: &mut Flags, matches: &mut ArgMatches) {
no_semicolons,
watch: watch_arg_parse(matches),
unstable_css,
unstable_html,
unstable_component,
unstable_yaml,
});
}
Expand Down Expand Up @@ -5891,6 +5912,8 @@ mod tests {
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_html: false,
unstable_component: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand All @@ -5916,6 +5939,8 @@ mod tests {
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_html: false,
unstable_component: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand All @@ -5941,6 +5966,8 @@ mod tests {
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_html: false,
unstable_component: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand All @@ -5966,6 +5993,8 @@ mod tests {
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_html: false,
unstable_component: false,
unstable_yaml: false,
watch: Some(Default::default()),
}),
Expand All @@ -5980,6 +6009,8 @@ mod tests {
"--watch",
"--no-clear-screen",
"--unstable-css",
"--unstable-html",
"--unstable-component",
"--unstable-yaml"
]);
assert_eq!(
Expand All @@ -5998,6 +6029,8 @@ mod tests {
prose_wrap: None,
no_semicolons: None,
unstable_css: true,
unstable_html: true,
unstable_component: true,
unstable_yaml: true,
watch: Some(WatchFlags {
hmr: false,
Expand Down Expand Up @@ -6034,6 +6067,8 @@ mod tests {
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_html: false,
unstable_component: false,
unstable_yaml: false,
watch: Some(Default::default()),
}),
Expand All @@ -6059,6 +6094,8 @@ mod tests {
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_html: false,
unstable_component: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand Down Expand Up @@ -6092,6 +6129,8 @@ mod tests {
prose_wrap: None,
no_semicolons: None,
unstable_css: false,
unstable_html: false,
unstable_component: false,
unstable_yaml: false,
watch: Some(Default::default()),
}),
Expand Down Expand Up @@ -6130,6 +6169,8 @@ mod tests {
prose_wrap: Some("never".to_string()),
no_semicolons: Some(true),
unstable_css: false,
unstable_html: false,
unstable_component: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand Down Expand Up @@ -6162,6 +6203,8 @@ mod tests {
prose_wrap: None,
no_semicolons: Some(false),
unstable_css: false,
unstable_html: false,
unstable_component: false,
unstable_yaml: false,
watch: Default::default(),
}),
Expand Down
8 changes: 8 additions & 0 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ impl BenchOptions {
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct UnstableFmtOptions {
pub css: bool,
pub html: bool,
pub component: bool,
pub yaml: bool,
}

Expand Down Expand Up @@ -316,6 +318,8 @@ impl FmtOptions {
options: resolve_fmt_options(fmt_flags, fmt_config.options),
unstable: UnstableFmtOptions {
css: unstable.css || fmt_flags.unstable_css,
html: unstable.html || fmt_flags.unstable_html,
component: unstable.component || fmt_flags.unstable_component,
yaml: unstable.yaml || fmt_flags.unstable_yaml,
},
files: fmt_config.files,
Expand Down Expand Up @@ -1339,6 +1343,8 @@ impl CliOptions {
let workspace = self.workspace();
UnstableFmtOptions {
css: workspace.has_unstable("fmt-css"),
html: workspace.has_unstable("fmt-html"),
component: workspace.has_unstable("fmt-component"),
yaml: workspace.has_unstable("fmt-yaml"),
}
}
Expand Down Expand Up @@ -1673,6 +1679,8 @@ impl CliOptions {
"byonm",
"bare-node-builtins",
"fmt-css",
"fmt-html",
"fmt-component",
"fmt-yaml",
]);
// add more unstable flags to the same vector holding granular flags
Expand Down
6 changes: 6 additions & 0 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,12 @@ impl Inner {
css: maybe_workspace
.map(|w| w.has_unstable("fmt-css"))
.unwrap_or(false),
html: maybe_workspace
.map(|w| w.has_unstable("fmt-html"))
.unwrap_or(false),
component: maybe_workspace
.map(|w| w.has_unstable("fmt-component"))
.unwrap_or(false),
yaml: maybe_workspace
.map(|w| w.has_unstable("fmt-yaml"))
.unwrap_or(false),
Expand Down
Loading

0 comments on commit 3a3315c

Please # to comment.