From 1ab9a9212bed23c3fed25de168a19d6cea4bced0 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Wed, 4 Sep 2024 08:51:02 -0400 Subject: [PATCH] feat(format/html): add `quick_test` to formatter crate (#3777) --- Cargo.lock | 7 +++ Cargo.toml | 2 + crates/biome_html_formatter/Cargo.toml | 7 +++ crates/biome_html_formatter/src/context.rs | 6 +-- crates/biome_html_formatter/src/lib.rs | 3 +- crates/biome_html_formatter/tests/language.rs | 47 +++++++++++++++++++ .../biome_html_formatter/tests/quick_test.rs | 44 +++++++++++++++++ crates/biome_html_parser/src/lib.rs | 15 +++++- crates/biome_service/Cargo.toml | 2 + .../biome_service/src/file_handlers/html.rs | 41 ++++++++++++++++ crates/biome_service/src/file_handlers/mod.rs | 1 + 11 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 crates/biome_html_formatter/tests/language.rs create mode 100644 crates/biome_html_formatter/tests/quick_test.rs create mode 100644 crates/biome_service/src/file_handlers/html.rs diff --git a/Cargo.lock b/Cargo.lock index 41a559a37b7f..c4aca9c8b6bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -654,8 +654,13 @@ version = "0.0.0" dependencies = [ "biome_diagnostics_categories", "biome_formatter", + "biome_formatter_test", + "biome_fs", + "biome_html_parser", "biome_html_syntax", + "biome_parser", "biome_rowan", + "biome_service", "biome_suppression", ] @@ -1026,6 +1031,8 @@ dependencies = [ "biome_graphql_parser", "biome_graphql_syntax", "biome_grit_patterns", + "biome_html_formatter", + "biome_html_syntax", "biome_js_analyze", "biome_js_factory", "biome_js_formatter", diff --git a/Cargo.toml b/Cargo.toml index 5ace77fc87a2..b379647aa473 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,6 +118,8 @@ biome_grit_parser = { version = "0.1.0", path = "./crates/biome_grit_ biome_grit_patterns = { version = "0.0.1", path = "./crates/biome_grit_patterns" } biome_grit_syntax = { version = "0.5.7", path = "./crates/biome_grit_syntax" } biome_html_factory = { version = "0.5.7", path = "./crates/biome_html_factory" } +biome_html_formatter = { version = "0.0.0", path = "./crates/biome_html_formatter" } +biome_html_parser = { version = "0.0.1", path = "./crates/biome_html_parser" } biome_html_syntax = { version = "0.5.7", path = "./crates/biome_html_syntax" } biome_js_analyze = { version = "0.5.7", path = "./crates/biome_js_analyze" } biome_js_factory = { version = "0.5.7", path = "./crates/biome_js_factory" } diff --git a/crates/biome_html_formatter/Cargo.toml b/crates/biome_html_formatter/Cargo.toml index d9a8e293df2a..bde30b2536af 100644 --- a/crates/biome_html_formatter/Cargo.toml +++ b/crates/biome_html_formatter/Cargo.toml @@ -16,5 +16,12 @@ biome_html_syntax = { workspace = true } biome_rowan = { workspace = true } biome_suppression = { workspace = true } +[dev-dependencies] +biome_formatter_test = { workspace = true } +biome_fs = { workspace = true } +biome_html_parser = { workspace = true } +biome_parser = { workspace = true } +biome_service = { workspace = true } + [lints] workspace = true diff --git a/crates/biome_html_formatter/src/context.rs b/crates/biome_html_formatter/src/context.rs index 8aa53fddf304..3108c8e60668 100644 --- a/crates/biome_html_formatter/src/context.rs +++ b/crates/biome_html_formatter/src/context.rs @@ -1,8 +1,8 @@ use std::{fmt, rc::Rc}; use biome_formatter::{ - printer::PrinterOptions, AttributePosition, CstFormatContext, FormatContext, FormatOptions, - IndentStyle, IndentWidth, LineEnding, LineWidth, TransformSourceMap, + printer::PrinterOptions, AttributePosition, BracketSpacing, CstFormatContext, FormatContext, + FormatOptions, IndentStyle, IndentWidth, LineEnding, LineWidth, TransformSourceMap, }; use biome_html_syntax::HtmlLanguage; @@ -131,7 +131,7 @@ impl FormatOptions for HtmlFormatOptions { } fn bracket_spacing(&self) -> biome_formatter::BracketSpacing { - todo!("Bracket spacing doesn't really make sense for HTML") + BracketSpacing::default() } fn as_print_options(&self) -> biome_formatter::prelude::PrinterOptions { diff --git a/crates/biome_html_formatter/src/lib.rs b/crates/biome_html_formatter/src/lib.rs index f62963f43f6a..3b68dca55214 100644 --- a/crates/biome_html_formatter/src/lib.rs +++ b/crates/biome_html_formatter/src/lib.rs @@ -9,7 +9,7 @@ pub use context::HtmlFormatOptions; use cst::FormatHtmlSyntaxNode; mod comments; -mod context; +pub mod context; mod cst; mod generated; mod html; @@ -112,6 +112,7 @@ where } } +#[derive(Debug, Clone)] pub struct HtmlFormatLanguage { options: HtmlFormatOptions, } diff --git a/crates/biome_html_formatter/tests/language.rs b/crates/biome_html_formatter/tests/language.rs new file mode 100644 index 000000000000..56bea06baf85 --- /dev/null +++ b/crates/biome_html_formatter/tests/language.rs @@ -0,0 +1,47 @@ +use biome_formatter_test::TestFormatLanguage; +use biome_fs::BiomePath; +use biome_html_formatter::context::HtmlFormatContext; +use biome_html_formatter::HtmlFormatLanguage; +use biome_html_parser::parse_html; +use biome_html_syntax::{HtmlFileSource, HtmlLanguage}; +use biome_parser::AnyParse; +use biome_service::{ + settings::{ServiceLanguage, Settings}, + workspace::DocumentFileSource, +}; + +pub struct HtmlTestFormatLanguage { + #[allow(dead_code)] + source_type: HtmlFileSource, +} + +impl HtmlTestFormatLanguage { + pub fn new(source_type: HtmlFileSource) -> Self { + HtmlTestFormatLanguage { source_type } + } +} + +impl TestFormatLanguage for HtmlTestFormatLanguage { + type ServiceLanguage = HtmlLanguage; + type Context = HtmlFormatContext; + type FormatLanguage = HtmlFormatLanguage; + + fn parse(&self, text: &str) -> AnyParse { + parse_html(text).into() + } + + fn to_format_language( + &self, + settings: &Settings, + file_source: &DocumentFileSource, + ) -> Self::FormatLanguage { + let options = Self::ServiceLanguage::resolve_format_options( + Some(&settings.formatter), + Some(&settings.override_settings), + None, + &BiomePath::new(""), + file_source, + ); + HtmlFormatLanguage::new(options) + } +} diff --git a/crates/biome_html_formatter/tests/quick_test.rs b/crates/biome_html_formatter/tests/quick_test.rs new file mode 100644 index 000000000000..123d0e055e85 --- /dev/null +++ b/crates/biome_html_formatter/tests/quick_test.rs @@ -0,0 +1,44 @@ +use biome_formatter::{AttributePosition, IndentStyle, LineWidth}; +use biome_formatter_test::check_reformat::CheckReformat; +use biome_html_formatter::context::HtmlFormatOptions; +use biome_html_formatter::{format_node, HtmlFormatLanguage}; +use biome_html_parser::parse_html; +use biome_html_syntax::HtmlFileSource; + +mod language { + include!("language.rs"); +} + +#[ignore] +#[test] +// use this test check if your snippet prints as you wish, without using a snapshot +fn quick_test() { + let src = r#" +
+

hello +

+
+ + "#; + let source_type = HtmlFileSource::html(); + let tree = parse_html(src); + let options = HtmlFormatOptions::new() + .with_indent_style(IndentStyle::Space) + .with_line_width(LineWidth::try_from(80).unwrap()) + .with_attribute_position(AttributePosition::Multiline); + + let doc = format_node(options.clone(), &tree.syntax()).unwrap(); + let result = doc.print().unwrap(); + + println!("{}", doc.into_document()); + eprintln!("{}", result.as_code()); + + CheckReformat::new( + &tree.syntax(), + result.as_code(), + "testing", + &language::HtmlTestFormatLanguage::new(source_type), + HtmlFormatLanguage::new(options), + ) + .check_reformat(); +} diff --git a/crates/biome_html_parser/src/lib.rs b/crates/biome_html_parser/src/lib.rs index 5e1b8522186a..0396b0b17095 100644 --- a/crates/biome_html_parser/src/lib.rs +++ b/crates/biome_html_parser/src/lib.rs @@ -7,6 +7,7 @@ use crate::parser::{HtmlLosslessTreeSink, HtmlParser}; use crate::syntax::parse_root; use biome_html_syntax::{HtmlRoot, HtmlSyntaxNode}; use biome_parser::diagnostic::ParseDiagnostic; +use biome_parser::AnyParse; use biome_rowan::{AstNode, NodeCache}; /// Parses the provided string as HTML program using the provided node cache. @@ -85,9 +86,21 @@ impl HtmlParse { /// Convert this parse result into a typed AST node. /// /// # Panics - /// + /// /// It panics if the node represented by this parse result mismatches. pub fn tree(&self) -> HtmlRoot { HtmlRoot::unwrap_cast(self.syntax()) } } + +impl From for AnyParse { + fn from(parse: HtmlParse) -> Self { + let root = parse.syntax(); + let diagnostics = parse.into_diagnostics(); + Self::new( + // SAFETY: the parser should always return a root node + root.as_send().unwrap(), + diagnostics, + ) + } +} diff --git a/crates/biome_service/Cargo.toml b/crates/biome_service/Cargo.toml index a5400bd46a45..4508d79e6817 100644 --- a/crates/biome_service/Cargo.toml +++ b/crates/biome_service/Cargo.toml @@ -32,6 +32,8 @@ biome_graphql_formatter = { workspace = true } biome_graphql_parser = { workspace = true } biome_graphql_syntax = { workspace = true } biome_grit_patterns = { workspace = true } +biome_html_formatter = { workspace = true } +biome_html_syntax = { workspace = true } biome_js_analyze = { workspace = true } biome_js_factory = { workspace = true, optional = true } biome_js_formatter = { workspace = true, features = ["serde"] } diff --git a/crates/biome_service/src/file_handlers/html.rs b/crates/biome_service/src/file_handlers/html.rs new file mode 100644 index 000000000000..d670a306c5d6 --- /dev/null +++ b/crates/biome_service/src/file_handlers/html.rs @@ -0,0 +1,41 @@ +use biome_html_formatter::HtmlFormatOptions; +use biome_html_syntax::HtmlLanguage; + +use crate::settings::ServiceLanguage; + +impl ServiceLanguage for HtmlLanguage { + type FormatterSettings = (); + type LinterSettings = (); + type OrganizeImportsSettings = (); + type FormatOptions = HtmlFormatOptions; + type ParserSettings = (); + type EnvironmentSettings = (); + + fn lookup_settings( + _languages: &crate::settings::LanguageListSettings, + ) -> &crate::settings::LanguageSettings { + todo!() + } + + fn resolve_format_options( + _global: Option<&crate::settings::FormatSettings>, + _overrides: Option<&crate::settings::OverrideSettings>, + _language: Option<&Self::FormatterSettings>, + _path: &biome_fs::BiomePath, + _file_source: &super::DocumentFileSource, + ) -> Self::FormatOptions { + // TODO: actually resolve options + HtmlFormatOptions::default() + } + + fn resolve_analyzer_options( + _global: Option<&crate::settings::Settings>, + _linter: Option<&crate::settings::LinterSettings>, + _overrides: Option<&crate::settings::OverrideSettings>, + _language: Option<&Self::LinterSettings>, + _path: &biome_fs::BiomePath, + _file_source: &super::DocumentFileSource, + ) -> biome_analyze::AnalyzerOptions { + todo!() + } +} diff --git a/crates/biome_service/src/file_handlers/mod.rs b/crates/biome_service/src/file_handlers/mod.rs index 05a5e5e2d6a6..da1f1adbbf95 100644 --- a/crates/biome_service/src/file_handlers/mod.rs +++ b/crates/biome_service/src/file_handlers/mod.rs @@ -45,6 +45,7 @@ use std::path::Path; mod astro; mod css; mod graphql; +mod html; mod javascript; mod json; mod svelte;