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(formatter_test): refactor TestFormatLanguage trait #3395

Merged
merged 4 commits into from
Jul 9, 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 Cargo.lock

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

1 change: 1 addition & 0 deletions crates/biome_css_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ biome_suppression = { workspace = true }
biome_configuration = { path = "../biome_configuration" }
biome_css_parser = { path = "../biome_css_parser" }
biome_formatter_test = { path = "../biome_formatter_test" }
biome_fs = { path = "../biome_fs" }
biome_parser = { path = "../biome_parser" }
biome_service = { path = "../biome_service" }
countme = { workspace = true, features = ["enable"] }
Expand Down
50 changes: 20 additions & 30 deletions crates/biome_css_formatter/tests/language.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use biome_css_formatter::context::{CssFormatContext, CssFormatOptions};
use biome_css_formatter::{format_node, format_range, CssFormatLanguage};
use biome_css_formatter::context::CssFormatContext;
use biome_css_formatter::CssFormatLanguage;
use biome_css_parser::{parse_css, CssParserOptions};
use biome_css_syntax::{CssFileSource, CssLanguage};
use biome_formatter::{FormatResult, Formatted, Printed};
use biome_formatter_test::TestFormatLanguage;
use biome_fs::BiomePath;
use biome_parser::AnyParse;
use biome_rowan::{SyntaxNode, TextRange};
use biome_service::settings::{ServiceLanguage, Settings};
use biome_service::{
settings::{ServiceLanguage, Settings},
workspace::DocumentFileSource,
};

#[derive(Default)]
pub struct CssTestFormatLanguage {
Expand All @@ -26,31 +28,19 @@ impl TestFormatLanguage for CssTestFormatLanguage {
parse_css(text, options).into()
}

fn to_language_settings<'a>(
fn to_format_language(
&self,
settings: &'a Settings,
) -> &'a <Self::ServiceLanguage as ServiceLanguage>::FormatterSettings {
&settings.languages.css.formatter
}

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

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

fn default_options(&self) -> <Self::ServiceLanguage as ServiceLanguage>::FormatOptions {
CssFormatOptions::default()
settings: &Settings,
file_source: &DocumentFileSource,
) -> Self::FormatLanguage {
let language_settings = &settings.languages.css.formatter;
let options = Self::ServiceLanguage::resolve_format_options(
Some(&settings.formatter),
Some(&settings.override_settings),
Some(language_settings),
&BiomePath::new(""),
file_source,
);
CssFormatLanguage::new(options)
}
}
4 changes: 2 additions & 2 deletions crates/biome_css_formatter/tests/prettier_tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{env, path::Path};

use biome_css_formatter::context::CssFormatOptions;
use biome_css_formatter::{context::CssFormatOptions, CssFormatLanguage};
use biome_formatter::{IndentStyle, IndentWidth};
use biome_formatter_test::test_prettier_snapshot::{PrettierSnapshot, PrettierTestFile};

Expand All @@ -21,7 +21,7 @@ fn test_snapshot(input: &'static str, _: &str, _: &str, _: &str) {
.with_indent_style(IndentStyle::Space)
.with_indent_width(IndentWidth::default());
let language = language::CssTestFormatLanguage::default();
let snapshot = PrettierSnapshot::new(test_file, language, options);
let snapshot = PrettierSnapshot::new(test_file, language, CssFormatLanguage::new(options));

snapshot.test()
}
11 changes: 9 additions & 2 deletions crates/biome_css_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_css_formatter::context::CssFormatOptions;
use biome_css_formatter::format_node;
use biome_css_formatter::{context::CssFormatOptions, CssFormatLanguage};
use biome_css_parser::{parse_css, CssParserOptions};
use biome_formatter::{IndentStyle, LineWidth};
use biome_formatter_test::check_reformat::CheckReformat;
Expand Down Expand Up @@ -34,5 +34,12 @@ fn quick_test() {
println!("{}", doc.into_document());
eprintln!("{}", result.as_code());

CheckReformat::new(root, result.as_code(), "quick_test", &language, options).check_reformat();
CheckReformat::new(
root,
result.as_code(),
"quick_test",
&language,
CssFormatLanguage::new(options),
)
.check_reformat();
}
9 changes: 7 additions & 2 deletions crates/biome_css_formatter/tests/spec_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_configuration::{PartialConfiguration, PartialCssConfiguration, PartialCssFormatter};
use biome_css_formatter::context::CssFormatOptions;
use biome_css_formatter::{context::CssFormatOptions, CssFormatLanguage};
use biome_formatter_test::spec::{SpecSnapshot, SpecTestFile};
use biome_service::workspace::UpdateSettingsParams;
use std::path::Path;
Expand Down Expand Up @@ -51,7 +51,12 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f
let options = CssFormatOptions::default();
let language = language::CssTestFormatLanguage::default();

let snapshot = SpecSnapshot::new(test_file, test_directory, language, options);
let snapshot = SpecSnapshot::new(
test_file,
test_directory,
language,
CssFormatLanguage::new(options),
);

snapshot.test()
}
11 changes: 5 additions & 6 deletions crates/biome_formatter_test/src/check_reformat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use biome_diagnostics::console::markup;
use biome_diagnostics::termcolor;
use biome_diagnostics::{DiagnosticExt, PrintDiagnostic};
use biome_rowan::SyntaxNode;
use biome_service::settings::ServiceLanguage;

/// Perform a second pass of formatting on a file, printing a diff if the
/// output doesn't match the input
Expand All @@ -18,7 +17,7 @@ where
file_name: &'a str,

language: &'a L,
options: <L::ServiceLanguage as ServiceLanguage>::FormatOptions,
format_language: L::FormatLanguage,
}

impl<'a, L> CheckReformat<'a, L>
Expand All @@ -31,15 +30,15 @@ where
file_name: &'a str,

language: &'a L,
options: <L::ServiceLanguage as ServiceLanguage>::FormatOptions,
format_language: L::FormatLanguage,
) -> Self {
CheckReformat {
root,
text,
file_name,

language,
options,
format_language,
}
}

Expand Down Expand Up @@ -70,7 +69,7 @@ where

let formatted = match self
.language
.format_node(self.options.clone(), &re_parse.syntax())
.format_node(self.format_language.clone(), &re_parse.syntax())
{
Ok(formatted) => formatted,
Err(err) => {
Expand All @@ -83,7 +82,7 @@ where
if self.text != printed.as_code() {
let input_format_element = self
.language
.format_node(self.options.clone(), self.root)
.format_node(self.format_language.clone(), self.root)
.unwrap();
let pretty_input_ir = format!("{}", formatted.into_document());
let pretty_reformat_ir = format!("{}", input_format_element.into_document());
Expand Down
39 changes: 13 additions & 26 deletions crates/biome_formatter_test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use biome_formatter::{CstFormatContext, FormatLanguage, FormatResult, Formatted, Printed};
use biome_fs::BiomePath;
use biome_parser::AnyParse;
use biome_rowan::{SyntaxNode, TextRange};
use biome_service::file_handlers::DocumentFileSource;
use biome_service::settings::ServiceLanguage;
use biome_service::settings::Settings;
use biome_service::settings::{ServiceLanguage, Settings};

pub mod check_reformat;
pub mod diff_report;
Expand All @@ -19,42 +17,31 @@ pub trait TestFormatLanguage {
Options = <Self::ServiceLanguage as ServiceLanguage>::FormatOptions,
>;
type FormatLanguage: FormatLanguage<Context = Self::Context, SyntaxLanguage = Self::ServiceLanguage>
+ 'static;
+ 'static
+ Clone;

fn parse(&self, text: &str) -> AnyParse;

fn to_language_settings<'a>(
&self,
settings: &'a Settings,
) -> &'a <Self::ServiceLanguage as ServiceLanguage>::FormatterSettings;

fn format_node(
&self,
options: <Self::ServiceLanguage as ServiceLanguage>::FormatOptions,
language: Self::FormatLanguage,
node: &SyntaxNode<Self::ServiceLanguage>,
) -> FormatResult<Formatted<Self::Context>>;
) -> FormatResult<Formatted<Self::Context>> {
biome_formatter::format_node(node, language)
}

fn format_range(
&self,
options: <Self::ServiceLanguage as ServiceLanguage>::FormatOptions,
language: Self::FormatLanguage,
node: &SyntaxNode<Self::ServiceLanguage>,
range: TextRange,
) -> FormatResult<Printed>;

fn default_options(&self) -> <Self::ServiceLanguage as ServiceLanguage>::FormatOptions;
) -> FormatResult<Printed> {
biome_formatter::format_range(node, range, language)
}

fn to_options(
fn to_format_language(
&self,
settings: &Settings,
file_source: &DocumentFileSource,
) -> <Self::ServiceLanguage as ServiceLanguage>::FormatOptions {
let language_settings = self.to_language_settings(settings);
Self::ServiceLanguage::resolve_format_options(
Some(&settings.formatter),
Some(&settings.override_settings),
Some(language_settings),
&BiomePath::new(""),
file_source,
)
}
) -> Self::FormatLanguage;
}
Loading