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

refactor(test): better infra for formatting #2189

Merged
merged 4 commits into from
Mar 25, 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ To know the technical details of how our parser works and how to write test, ple

### Formatter

To know the technical details of how our formatter works and how to write test, please check our [internal page](https://docs.rs/biome_formatter/latest/biome_formatter/)
To know the technical details of how our formatter works and how to write test, please check our [internal page](https://github.com/biomejs/biome/blob/main/crates/biome_formatter/CONTRIBUTING.md)


## Crate dependencies
Expand Down
4 changes: 4 additions & 0 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 crates/biome_css_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ biome_suppression = { workspace = true }
biome_css_parser = { path = "../biome_css_parser" }
biome_formatter_test = { path = "../biome_formatter_test" }
biome_parser = { path = "../biome_parser" }
biome_service = { path = "../biome_service" }
countme = { workspace = true, features = ["enable"] }
serde = { version = "1", features = ["derive"] }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tests_macros = { path = "../tests_macros" }

Expand Down
123 changes: 12 additions & 111 deletions crates/biome_css_formatter/tests/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ use biome_css_formatter::context::{CssFormatContext, CssFormatOptions};
use biome_css_formatter::{format_node, format_range, CssFormatLanguage};
use biome_css_parser::{parse_css, CssParserOptions};
use biome_css_syntax::{CssFileSource, CssLanguage};
use biome_formatter::{
FormatContext, FormatResult, Formatted, IndentStyle, LineEnding, LineWidth, Printed, QuoteStyle,
};
use biome_formatter::{FormatResult, Formatted, Printed};
use biome_formatter_test::TestFormatLanguage;
use biome_parser::AnyParse;
use biome_rowan::{SyntaxNode, TextRange};
use serde::{Deserialize, Serialize};
use biome_service::settings::{ServiceLanguage, WorkspaceSettings};

#[derive(Default)]
pub struct CssTestFormatLanguage {
_source_type: CssFileSource,
}

impl TestFormatLanguage for CssTestFormatLanguage {
type SyntaxLanguage = CssLanguage;
type Options = CssFormatOptions;
type ServiceLanguage = CssLanguage;
type Context = CssFormatContext;
type FormatLanguage = CssFormatLanguage;

Expand All @@ -27,127 +24,31 @@ impl TestFormatLanguage for CssTestFormatLanguage {
AnyParse::new(parse.syntax().as_send().unwrap(), parse.into_diagnostics())
}

fn deserialize_format_options(
fn to_language_settings<'a>(
&self,
options: &str,
) -> Vec<<Self::Context as FormatContext>::Options> {
let test_options: TestOptions = serde_json::from_str(options).unwrap();

test_options
.cases
.into_iter()
.map(|case| case.into())
.collect()
settings: &'a WorkspaceSettings,
) -> &'a <Self::ServiceLanguage as ServiceLanguage>::FormatterSettings {
&settings.languages.css.formatter
}

fn format_node(
&self,
options: Self::Options,
node: &SyntaxNode<Self::SyntaxLanguage>,
options: <Self::ServiceLanguage as ServiceLanguage>::FormatOptions,
node: &SyntaxNode<Self::ServiceLanguage>,
) -> FormatResult<Formatted<Self::Context>> {
format_node(options, node)
}

fn format_range(
&self,
options: Self::Options,
node: &SyntaxNode<Self::SyntaxLanguage>,
options: <Self::ServiceLanguage as ServiceLanguage>::FormatOptions,
node: &SyntaxNode<Self::ServiceLanguage>,
range: TextRange,
) -> FormatResult<Printed> {
format_range(options, node, range)
}
}

#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
pub enum CssSerializableIndentStyle {
/// Tab
Tab,
/// Space
Space,
}

impl From<CssSerializableIndentStyle> for IndentStyle {
fn from(test: CssSerializableIndentStyle) -> Self {
match test {
CssSerializableIndentStyle::Tab => IndentStyle::Tab,
CssSerializableIndentStyle::Space => IndentStyle::Space,
}
}
}

#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
pub enum CssSerializableQuoteStyle {
Double,
Single,
}

impl From<CssSerializableQuoteStyle> for QuoteStyle {
fn from(test: CssSerializableQuoteStyle) -> Self {
match test {
CssSerializableQuoteStyle::Double => QuoteStyle::Double,
CssSerializableQuoteStyle::Single => QuoteStyle::Single,
}
}
}

#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize, Serialize)]
pub enum CssSerializableLineEnding {
/// Line Feed only (\n), common on Linux and macOS as well as inside git repos
Lf,

/// Carriage Return + Line Feed characters (\r\n), common on Windows
Crlf,

/// Carriage Return character only (\r), used very rarely
Cr,
}

impl From<CssSerializableLineEnding> for LineEnding {
fn from(test: CssSerializableLineEnding) -> Self {
match test {
CssSerializableLineEnding::Lf => LineEnding::Lf,
CssSerializableLineEnding::Crlf => LineEnding::Crlf,
CssSerializableLineEnding::Cr => LineEnding::Cr,
}
}
}

#[derive(Debug, Deserialize, Serialize, Clone, Copy)]
pub struct CssSerializableFormatOptions {
/// The indent style.
pub indent_style: Option<CssSerializableIndentStyle>,

/// The indent width.
pub indent_width: Option<u8>,

/// The type of line ending.
pub line_ending: Option<CssSerializableLineEnding>,

/// What's the max width of a line. Defaults to 80.
pub line_width: Option<u16>,

/// The style for quotes. Defaults to double.
pub quote_style: Option<CssSerializableQuoteStyle>,
}

impl From<CssSerializableFormatOptions> for CssFormatOptions {
fn from(test: CssSerializableFormatOptions) -> Self {
fn default_options(&self) -> <Self::ServiceLanguage as ServiceLanguage>::FormatOptions {
CssFormatOptions::default()
.with_indent_style(test.indent_style.map(Into::into).unwrap_or_default())
.with_indent_width(test.indent_width.map(Into::into).unwrap_or_default())
.with_line_width(
test.line_width
.and_then(|width| LineWidth::try_from(width).ok())
.unwrap_or_default(),
)
.with_quote_style(
test.quote_style
.map_or_else(|| QuoteStyle::Double, |value| value.into()),
)
}
}

#[derive(Debug, Deserialize, Serialize)]
struct TestOptions {
cases: Vec<CssSerializableFormatOptions>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: css/quote_style/normalize_quotes.css
---

# Input

```css
Expand Down Expand Up @@ -68,7 +67,7 @@ div {
}
```

## Output 2
## Output 1

-----
Indent style: Tab
Expand Down Expand Up @@ -101,5 +100,3 @@ div {
color: var(--\eeunquoted);
}
```


Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"cases": [
{
"quote_style": "Single"
"$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json",
"css": {
"formatter": {
"quoteStyle": "single"
}
]
}
}
}
4 changes: 4 additions & 0 deletions crates/biome_deserialize/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub fn deserialize_from_json_str<Output: Deserializable>(
}
}

pub fn deserialize_from_str<Output: Deserializable>(source: &str) -> Deserialized<Output> {
deserialize_from_json_str(source, JsonParserOptions::default(), "")
}

/// Attempts to deserialize a JSON AST, given the `Output`.
///
/// `name` corresponds to the name used in a diagnostic to designate the deserialized value.
Expand Down
Loading
Loading