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(css_formatter): correctly resolve css format options #3170

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

### Formatter

#### Bug fixes

- Fix [#3103](https://github.com/biomejs/biome/issues/3103) by correctly resolving CSS formatter options. Contributed by @ah-yu

### JavaScript APIs

### Linter
Expand Down
8 changes: 4 additions & 4 deletions crates/biome_cli/src/commands/rage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ impl Display for RageConfiguration<'_, '_> {
markup! (
{Section("CSS Formatter")}
{KeyValuePair("Enabled", markup!({DebugDisplay(css_formatter_configuration.enabled)}))}
{KeyValuePair("Indent style", markup!({DebugDisplay(css_formatter_configuration.indent_style)}))}
{KeyValuePair("Indent width", markup!({DebugDisplay(css_formatter_configuration.indent_width)}))}
{KeyValuePair("Line ending", markup!({DebugDisplay(css_formatter_configuration.line_ending)}))}
{KeyValuePair("Line width", markup!({DebugDisplay(css_formatter_configuration.line_width)}))}
{KeyValuePair("Indent style", markup!({DebugDisplayOption(css_formatter_configuration.indent_style)}))}
{KeyValuePair("Indent width", markup!({DebugDisplayOption(css_formatter_configuration.indent_width)}))}
{KeyValuePair("Line ending", markup!({DebugDisplayOption(css_formatter_configuration.line_ending)}))}
{KeyValuePair("Line width", markup!({DebugDisplayOption(css_formatter_configuration.line_width)}))}
{KeyValuePair("Quote style", markup!({DebugDisplay(css_formatter_configuration.quote_style)}))}
).fmt(fmt)?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ JSON Formatter:

CSS Formatter:
Enabled: false
Indent style: Tab
Indent width: 2
Line ending: Lf
Line width: 80
Indent style: unset
Indent width: unset
Line ending: unset
Line width: unset
Quote style: Double

Server:
Expand Down
24 changes: 14 additions & 10 deletions crates/biome_configuration/src/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ pub struct CssFormatter {

/// The indent style applied to CSS (and its super languages) files.
#[partial(bpaf(long("css-formatter-indent-style"), argument("tab|space"), optional))]
pub indent_style: PlainIndentStyle,
pub indent_style: Option<PlainIndentStyle>,

/// The size of the indentation applied to CSS (and its super languages) files. Default to 2.
#[partial(bpaf(long("css-formatter-indent-width"), argument("NUMBER"), optional))]
pub indent_width: IndentWidth,
pub indent_width: Option<IndentWidth>,

/// The type of line ending applied to CSS (and its super languages) files.
#[partial(bpaf(long("css-formatter-line-ending"), argument("lf|crlf|cr"), optional))]
pub line_ending: LineEnding,
pub line_ending: Option<LineEnding>,

/// What's the max width of a line applied to CSS (and its super languages) files. Defaults to 80.
#[partial(bpaf(long("css-formatter-line-width"), argument("NUMBER"), optional))]
pub line_width: LineWidth,
pub line_width: Option<LineWidth>,

/// The type of quotes used in CSS code. Defaults to double.
#[partial(bpaf(long("css-formatter-quote-style"), argument("double|single"), optional))]
Expand All @@ -73,10 +73,10 @@ impl PartialCssFormatter {
pub fn get_formatter_configuration(&self) -> CssFormatter {
CssFormatter {
enabled: self.enabled.unwrap_or_default(),
indent_style: self.indent_style.unwrap_or_default(),
indent_width: self.indent_width.unwrap_or_default(),
line_ending: self.line_ending.unwrap_or_default(),
line_width: self.line_width.unwrap_or_default(),
indent_style: self.indent_style,
indent_width: self.indent_width,
line_ending: self.line_ending,
line_width: self.line_width,
quote_style: self.quote_style.unwrap_or_default(),
}
}
Expand Down Expand Up @@ -105,6 +105,10 @@ impl PartialCssLinter {
fn default_css() {
let css_configuration = CssFormatter::default();

assert_eq!(css_configuration.indent_width, IndentWidth::default());
assert_eq!(css_configuration.indent_width.value(), 2);
assert!(!css_configuration.enabled);
assert_eq!(css_configuration.indent_style, None);
assert_eq!(css_configuration.indent_width, None);
assert_eq!(css_configuration.line_ending, None);
assert_eq!(css_configuration.line_width, None);
assert_eq!(css_configuration.quote_style, QuoteStyle::Double);
}
2 changes: 1 addition & 1 deletion crates/biome_css_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl CstFormatContext for CssFormatContext {
}
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct CssFormatOptions {
indent_style: IndentStyle,
indent_width: IndentWidth,
Expand Down
25 changes: 25 additions & 0 deletions crates/biome_service/src/file_handlers/css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,28 @@ pub(crate) fn fix_all(params: FixAllParams) -> Result<FixFileResult, WorkspaceEr
}
}
}

#[cfg(test)]
mod test {
use super::*;
use biome_css_syntax::CssFileSource;

#[test]
fn inherit_global_format_settings() {
let format_options = CssLanguage::resolve_format_options(
Some(&FormatSettings::default()),
None,
None,
&BiomePath::new(""),
&DocumentFileSource::Css(CssFileSource::css()),
);
assert_eq!(
format_options,
CssFormatOptions::default()
.with_indent_style(IndentStyle::default())
.with_indent_width(IndentWidth::default())
.with_line_ending(LineEnding::default())
.with_line_width(LineWidth::default())
);
}
}
7 changes: 4 additions & 3 deletions crates/biome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,10 @@ impl From<CssConfiguration> for LanguageSettings<CssLanguage> {
language_setting.parser.css_modules = css.parser.css_modules;

language_setting.formatter.enabled = Some(css.formatter.enabled);
language_setting.formatter.line_width = Some(css.formatter.line_width);
language_setting.formatter.indent_width = Some(css.formatter.indent_width);
language_setting.formatter.indent_style = Some(css.formatter.indent_style.into());
language_setting.formatter.indent_width = css.formatter.indent_width;
language_setting.formatter.indent_style = css.formatter.indent_style.map(Into::into);
language_setting.formatter.line_width = css.formatter.line_width;
language_setting.formatter.line_ending = css.formatter.line_ending;
language_setting.formatter.quote_style = Some(css.formatter.quote_style);
language_setting.linter.enabled = Some(css.linter.enabled);

Expand Down