Skip to content

Commit

Permalink
feat(html): set up HTML configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Jan 4, 2025
1 parent 78c8910 commit 06ee71b
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

23 changes: 12 additions & 11 deletions crates/biome_configuration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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]
Expand Down
120 changes: 120 additions & 0 deletions crates/biome_configuration/src/html.rs
Original file line number Diff line number Diff line change
@@ -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<IndentStyle>,

/// 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<IndentWidth>,

/// 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<LineEnding>,

/// 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<LineWidth>,

/// 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<AttributePosition>,

/// 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<BracketSameLine>,

/// 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<WhitespaceSensitivity>,

/// Whether or not to indent the `<script>` and `<style>` tags for HTML (and its super languages). Defaults to false.
#[partial(bpaf(
long("html-formatter-indent-script-and-style"),
argument("true|false"),
optional
))]
pub indent_script_and_style: Option<IndentScriptAndStyle>,
}

impl Default for HtmlFormatter {
fn default() -> Self {
Self {
enabled: false,
indent_style: Default::default(),
indent_width: Default::default(),
line_ending: Default::default(),
line_width: Default::default(),
attribute_position: Default::default(),
bracket_same_line: Default::default(),
whitespace_sensitivity: Default::default(),
indent_script_and_style: Default::default(),
}
}
}

impl PartialHtmlFormatter {
pub fn get_formatter_configuration(&self) -> HtmlFormatter {
HtmlFormatter {
enabled: self.enabled.unwrap_or_default(),
indent_style: self.indent_style,
indent_width: self.indent_width,
line_ending: self.line_ending,
line_width: self.line_width,
attribute_position: self.attribute_position,
bracket_same_line: self.bracket_same_line,
whitespace_sensitivity: self.whitespace_sensitivity,
indent_script_and_style: self.indent_script_and_style,
}
}
}
7 changes: 7 additions & 0 deletions crates/biome_configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod formatter;
pub mod generated;
pub mod graphql;
pub mod grit;
pub mod html;
pub mod javascript;
pub mod json;
mod overrides;
Expand Down Expand Up @@ -51,6 +52,7 @@ pub use graphql::{
partial_graphql_configuration, GraphqlConfiguration, GraphqlFormatter, GraphqlLinter,
PartialGraphqlConfiguration, PartialGraphqlFormatter, PartialGraphqlLinter,
};
use html::{partial_html_configuration, HtmlConfiguration, PartialHtmlConfiguration};
pub use javascript::{
partial_javascript_configuration, JavascriptConfiguration, JavascriptFormatter,
PartialJavascriptConfiguration, PartialJavascriptFormatter,
Expand Down Expand Up @@ -133,6 +135,11 @@ pub struct Configuration {
#[partial(type, bpaf(external(partial_grit_configuration), optional))]
pub grit: GritConfiguration,

// hidden for now. show when it's to be shown to end users.
/// Specific configuration for the HTML language
#[partial(type, bpaf(external(partial_html_configuration), optional, hide))]
pub html: HtmlConfiguration,

/// A list of granular patterns that should be applied only to a sub set of files
#[partial(bpaf(hide))]
pub overrides: Overrides,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_configuration/tests/spec_tests.rs
expression: top_level_extraneous_field.json
snapshot_kind: text
---
top_level_extraneous_field.json:2:2 deserialize ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Expand All @@ -26,6 +25,7 @@ top_level_extraneous_field.json:2:2 deserialize ━━━━━━━━━━
- css
- graphql
- grit
- html
- overrides
- plugins
- assist
2 changes: 2 additions & 0 deletions crates/biome_html_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ repository.workspace = true
version = "0.0.0"

[dependencies]
biome_deserialize = { workspace = true }
biome_deserialize_macros = { workspace = true }
biome_diagnostics_categories = { workspace = true }
biome_formatter = { workspace = true }
biome_html_syntax = { workspace = true }
Expand Down
5 changes: 3 additions & 2 deletions crates/biome_html_formatter/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{fmt, rc::Rc, str::FromStr};

use biome_deserialize_macros::{Deserializable, Merge};
use biome_formatter::{
printer::PrinterOptions, AttributePosition, BracketSameLine, BracketSpacing, CstFormatContext,
FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding, LineWidth,
Expand Down Expand Up @@ -223,7 +224,7 @@ impl FormatOptions for HtmlFormatOptions {
/// | without spaces | `1<b>2</b>3` | 1<b>2</b>3 |
///
/// This happens because whitespace is significant in inline elements.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserializable, Merge)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -272,7 +273,7 @@ impl FromStr for WhitespaceSensitivity {
/// Whether to indent the content of `<script>` and `<style>` tags for HTML-ish templating languages (Vue, Svelte, etc.).
///
/// When true, the content of `<script>` and `<style>` tags will be indented one level.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserializable, Merge)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down
11 changes: 10 additions & 1 deletion crates/biome_service/src/file_handlers/html.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use biome_analyze::AnalyzerOptions;
use biome_formatter::{BracketSameLine, IndentStyle, IndentWidth, LineEnding, LineWidth, Printed};
use biome_formatter::{
AttributePosition, BracketSameLine, IndentStyle, IndentWidth, LineEnding, LineWidth, Printed,
};
use biome_fs::BiomePath;
use biome_html_formatter::{
context::{IndentScriptAndStyle, WhitespaceSensitivity},
Expand Down Expand Up @@ -29,6 +31,7 @@ pub struct HtmlFormatterSettings {
pub line_width: Option<LineWidth>,
pub indent_width: Option<IndentWidth>,
pub indent_style: Option<IndentStyle>,
pub attribute_position: Option<AttributePosition>,
pub bracket_same_line: Option<BracketSameLine>,
pub whitespace_sensitivity: Option<WhitespaceSensitivity>,
pub indent_script_and_style: Option<IndentScriptAndStyle>,
Expand All @@ -42,6 +45,7 @@ impl Default for HtmlFormatterSettings {
indent_width: Default::default(),
line_ending: Default::default(),
line_width: Default::default(),
attribute_position: Default::default(),
bracket_same_line: Default::default(),
whitespace_sensitivity: Default::default(),
indent_script_and_style: Default::default(),
Expand Down Expand Up @@ -86,6 +90,10 @@ impl ServiceLanguage for HtmlLanguage {
.and_then(|l| l.line_ending)
.or(global.and_then(|g| g.line_ending))
.unwrap_or_default();
let attribute_position = language
.and_then(|l| l.attribute_position)
.or(global.and_then(|g| g.attribute_position))
.unwrap_or_default();
let bracket_same_line = language
.and_then(|l| l.bracket_same_line)
.or(global.and_then(|g| g.bracket_same_line))
Expand All @@ -102,6 +110,7 @@ impl ServiceLanguage for HtmlLanguage {
.with_indent_width(indent_width)
.with_line_width(line_width)
.with_line_ending(line_ending)
.with_attribute_position(attribute_position)
.with_bracket_same_line(bracket_same_line)
.with_whitespace_sensitivity(whitespace_sensitivity)
.with_indent_script_and_style(indent_script_and_style);
Expand Down

0 comments on commit 06ee71b

Please sign in to comment.