From 06ee71b243e1df2af16de045d2fe5bd29898260e Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sat, 4 Jan 2025 09:16:57 -0500 Subject: [PATCH] feat(html): set up HTML configuration --- Cargo.lock | 3 + crates/biome_configuration/Cargo.toml | 23 ++-- crates/biome_configuration/src/html.rs | 120 ++++++++++++++++++ crates/biome_configuration/src/lib.rs | 7 + .../top_level_extraneous_field.json.snap | 2 +- crates/biome_html_formatter/Cargo.toml | 2 + crates/biome_html_formatter/src/context.rs | 5 +- .../biome_service/src/file_handlers/html.rs | 11 +- 8 files changed, 158 insertions(+), 15 deletions(-) create mode 100644 crates/biome_configuration/src/html.rs diff --git a/Cargo.lock b/Cargo.lock index 5d56a351ac6a..3300a8a88ff2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -228,6 +228,7 @@ dependencies = [ "biome_formatter", "biome_graphql_analyze", "biome_graphql_syntax", + "biome_html_formatter", "biome_html_syntax", "biome_js_analyze", "biome_js_formatter", @@ -729,6 +730,8 @@ dependencies = [ name = "biome_html_formatter" version = "0.0.0" dependencies = [ + "biome_deserialize", + "biome_deserialize_macros", "biome_diagnostics_categories", "biome_formatter", "biome_formatter_test", diff --git a/crates/biome_configuration/Cargo.toml b/crates/biome_configuration/Cargo.toml index e38875f1f5f8..ee1989f0fd82 100644 --- a/crates/biome_configuration/Cargo.toml +++ b/crates/biome_configuration/Cargo.toml @@ -24,6 +24,7 @@ biome_flags = { workspace = true } biome_formatter = { workspace = true, features = ["serde"] } biome_graphql_analyze = { workspace = true } biome_graphql_syntax = { workspace = true } +biome_html_formatter = { workspace = true, features = ["serde"] } biome_html_syntax = { workspace = true } biome_js_analyze = { workspace = true } biome_js_formatter = { workspace = true, features = ["serde"] } @@ -42,17 +43,17 @@ serde_ini = { workspace = true } [features] schema = [ - "dep:schemars", - "biome_js_analyze/schema", - "biome_css_analyze/schema", - "biome_formatter/schema", - "biome_json_syntax/schema", - "biome_css_syntax/schema", - "biome_graphql_syntax/schema", - "biome_html_syntax/schema", - "biome_analyze/schema", - "biome_json_formatter/schema", - "biome_js_formatter/schema", + "dep:schemars", + "biome_js_analyze/schema", + "biome_css_analyze/schema", + "biome_formatter/schema", + "biome_json_syntax/schema", + "biome_css_syntax/schema", + "biome_graphql_syntax/schema", + "biome_html_syntax/schema", + "biome_analyze/schema", + "biome_json_formatter/schema", + "biome_js_formatter/schema", ] [dev-dependencies] diff --git a/crates/biome_configuration/src/html.rs b/crates/biome_configuration/src/html.rs new file mode 100644 index 000000000000..7df30f674711 --- /dev/null +++ b/crates/biome_configuration/src/html.rs @@ -0,0 +1,120 @@ +use biome_deserialize_macros::{Deserializable, Merge, Partial}; +use biome_formatter::{ + AttributePosition, BracketSameLine, IndentStyle, IndentWidth, LineEnding, LineWidth, +}; +use biome_html_formatter::context::{IndentScriptAndStyle, WhitespaceSensitivity}; +use bpaf::Bpaf; +use serde::{Deserialize, Serialize}; + +/// Options applied to HTML files +#[derive(Clone, Default, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)] +#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))] +#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))] +#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))] +pub struct HtmlConfiguration { + /// HTML parsing options + #[partial(type, bpaf(external(partial_html_parser), optional))] + pub parser: HtmlParser, + + /// HTML formatter options + #[partial(type, bpaf(external(partial_html_formatter), optional))] + pub formatter: HtmlFormatter, +} + +/// Options that changes how the HTML parser behaves +#[derive(Clone, Default, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)] +#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))] +#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))] +#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))] +pub struct HtmlParser; + +/// Options that changes how the HTML formatter behaves +#[derive(Clone, Debug, Deserialize, Eq, Partial, PartialEq, Serialize)] +#[partial(derive(Bpaf, Clone, Deserializable, Eq, Merge, PartialEq))] +#[partial(cfg_attr(feature = "schema", derive(schemars::JsonSchema)))] +#[partial(serde(rename_all = "camelCase", default, deny_unknown_fields))] +pub struct HtmlFormatter { + /// Control the formatter for HTML (and its super languages) files. + #[partial(bpaf(long("html-formatter-enabled"), argument("true|false"), optional))] + pub enabled: bool, + + /// The indent style applied to HTML (and its super languages) files. + #[partial(bpaf(long("html-formatter-indent-style"), argument("tab|space"), optional))] + pub indent_style: Option, + + /// The size of the indentation applied to HTML (and its super languages) files. Default to 2. + #[partial(bpaf(long("html-formatter-indent-width"), argument("NUMBER"), optional))] + pub indent_width: Option, + + /// The type of line ending applied to HTML (and its super languages) files. + #[partial(bpaf(long("html-formatter-line-ending"), argument("lf|crlf|cr"), optional))] + pub line_ending: Option, + + /// What's the max width of a line applied to HTML (and its super languages) files. Defaults to 80. + #[partial(bpaf(long("html-formatter-line-width"), argument("NUMBER"), optional))] + pub line_width: Option, + + /// The attribute position style in HTML elements. Defaults to auto. + #[partial(bpaf( + long("html-formatter-attribute-position"), + argument("multiline|auto"), + optional + ))] + pub attribute_position: Option, + + /// Whether to hug the closing bracket of multiline HTMLtags to the end of the last line, rather than being alone on the following line. Defaults to false. + #[partial(bpaf( + long("html-formatter-bracket-same-line"), + argument("true|false"), + optional + ))] + pub bracket_same_line: Option, + + /// Whether or not to account for whitespace sensitivity when formatting HTML (and its super languages). Defaults to "strict". + #[partial(bpaf( + long("html-formatter-whitespace-sensitivity"), + argument("strict|ignore"), + optional + ))] + pub whitespace_sensitivity: Option, + + /// Whether or not to indent the `