Skip to content

Commit

Permalink
feat(format/html): add quick_test to formatter crate (#3777)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 authored Sep 4, 2024
1 parent b835e0b commit 1ab9a92
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
7 changes: 7 additions & 0 deletions crates/biome_html_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions crates/biome_html_formatter/src/context.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_html_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use context::HtmlFormatOptions;
use cst::FormatHtmlSyntaxNode;

mod comments;
mod context;
pub mod context;
mod cst;
mod generated;
mod html;
Expand Down Expand Up @@ -112,6 +112,7 @@ where
}
}

#[derive(Debug, Clone)]
pub struct HtmlFormatLanguage {
options: HtmlFormatOptions,
}
Expand Down
47 changes: 47 additions & 0 deletions crates/biome_html_formatter/tests/language.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
44 changes: 44 additions & 0 deletions crates/biome_html_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
@@ -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#"
<div>
<p>hello
</p>
</div>
"#;
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();
}
15 changes: 14 additions & 1 deletion crates/biome_html_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<HtmlParse> 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,
)
}
}
2 changes: 2 additions & 0 deletions crates/biome_service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
41 changes: 41 additions & 0 deletions crates/biome_service/src/file_handlers/html.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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!()
}
}
1 change: 1 addition & 0 deletions crates/biome_service/src/file_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use std::path::Path;
mod astro;
mod css;
mod graphql;
mod html;
mod javascript;
mod json;
mod svelte;
Expand Down

0 comments on commit 1ab9a92

Please sign in to comment.