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

feat(html): resolve format options #4017

Merged
merged 1 commit into from
Sep 22, 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: 2 additions & 2 deletions crates/biome_html_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use biome_formatter::{
printer::PrinterOptions, AttributePosition, BracketSpacing, CstFormatContext, FormatContext,
FormatOptions, IndentStyle, IndentWidth, LineEnding, LineWidth, TransformSourceMap,
};
use biome_html_syntax::HtmlLanguage;
use biome_html_syntax::{HtmlFileSource, HtmlLanguage};

use crate::comments::{FormatHtmlLeadingComment, HtmlCommentStyle, HtmlComments};

Expand All @@ -27,7 +27,7 @@ pub struct HtmlFormatOptions {
}

impl HtmlFormatOptions {
pub fn new() -> Self {
pub fn new(_file_source: HtmlFileSource) -> Self {
Self {
..Default::default()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_html_formatter/tests/prettier_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn test_snapshot(input: &'static str, _: &str, _: &str, _: &str) {
let test_file = PrettierTestFile::new(input, root_path);
let source_type = HtmlFileSource::html();

let options = HtmlFormatOptions::new()
let options = HtmlFormatOptions::new(HtmlFileSource::html())
.with_indent_style(IndentStyle::Space)
.with_indent_width(IndentWidth::default());

Expand Down
2 changes: 1 addition & 1 deletion crates/biome_html_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn quick_test() {
"#;
let source_type = HtmlFileSource::html();
let tree = parse_html(src);
let options = HtmlFormatOptions::new()
let options = HtmlFormatOptions::new(HtmlFileSource::html())
.with_indent_style(IndentStyle::Space)
.with_line_width(LineWidth::try_from(80).unwrap())
.with_attribute_position(AttributePosition::Multiline);
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_html_formatter/tests/spec_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn run(spec_input_file: &str, _expected_file: &str, test_directory: &str, _f

let source_type: HtmlFileSource = test_file.input_file().as_path().try_into().unwrap();

let options = HtmlFormatOptions::new();
let options = HtmlFormatOptions::new(HtmlFileSource::html());
let language = language::HtmlTestFormatLanguage::new(source_type);

let snapshot = SpecSnapshot::new(
Expand Down
66 changes: 57 additions & 9 deletions crates/biome_service/src/file_handlers/html.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_analyze::{AnalyzerConfiguration, AnalyzerOptions};
use biome_formatter::Printed;
use biome_formatter::{IndentStyle, IndentWidth, LineEnding, LineWidth, Printed};
use biome_fs::BiomePath;
use biome_html_formatter::{format_node, HtmlFormatOptions};
use biome_html_parser::parse_html_with_cache;
Expand All @@ -17,8 +17,30 @@ use super::{
FormatterCapabilities, ParseResult, ParserCapabilities, SearchCapabilities,
};

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct HtmlFormatterSettings {
pub line_ending: Option<LineEnding>,
pub line_width: Option<LineWidth>,
pub indent_width: Option<IndentWidth>,
pub indent_style: Option<IndentStyle>,
pub enabled: Option<bool>,
}

impl Default for HtmlFormatterSettings {
fn default() -> Self {
Self {
enabled: Some(false),
indent_style: Default::default(),
indent_width: Default::default(),
line_ending: Default::default(),
line_width: Default::default(),
}
}
}

impl ServiceLanguage for HtmlLanguage {
type FormatterSettings = ();
type FormatterSettings = HtmlFormatterSettings;
type LinterSettings = ();
type OrganizeImportsSettings = ();
type FormatOptions = HtmlFormatOptions;
Expand All @@ -32,14 +54,40 @@ impl ServiceLanguage for HtmlLanguage {
}

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,
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()
let indent_style = language
.and_then(|l| l.indent_style)
.or(global.and_then(|g| g.indent_style))
.unwrap_or_default();
let line_width = language
.and_then(|l| l.line_width)
.or(global.and_then(|g| g.line_width))
.unwrap_or_default();
let indent_width = language
.and_then(|l| l.indent_width)
.or(global.and_then(|g| g.indent_width))
.unwrap_or_default();

let line_ending = language
.and_then(|l| l.line_ending)
.or(global.and_then(|g| g.line_ending))
.unwrap_or_default();

let options = HtmlFormatOptions::new(file_source.to_html_file_source().unwrap_or_default())
.with_indent_style(indent_style)
.with_indent_width(indent_width)
.with_line_width(line_width)
.with_line_ending(line_ending);
if let Some(overrides) = overrides {
overrides.to_override_html_format_options(path, options)
} else {
options
}
}

fn resolve_analyzer_options(
Expand Down
7 changes: 7 additions & 0 deletions crates/biome_service/src/file_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ impl DocumentFileSource {
}
}

pub fn to_html_file_source(&self) -> Option<HtmlFileSource> {
match self {
DocumentFileSource::Html(html) => Some(*html),
_ => None,
}
}

pub fn can_parse(path: &Path, content: &str) -> bool {
let file_source = DocumentFileSource::from(path);
match file_source {
Expand Down
45 changes: 45 additions & 0 deletions crates/biome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use biome_formatter::{
use biome_fs::BiomePath;
use biome_graphql_formatter::context::GraphqlFormatOptions;
use biome_graphql_syntax::GraphqlLanguage;
use biome_html_formatter::HtmlFormatOptions;
use biome_html_syntax::HtmlLanguage;
use biome_js_formatter::context::JsFormatOptions;
use biome_js_parser::JsParserOptions;
Expand Down Expand Up @@ -1001,6 +1002,19 @@ impl OverrideSettings {
options
}

pub fn to_override_html_format_options(
&self,
path: &Path,
mut options: HtmlFormatOptions,
) -> HtmlFormatOptions {
for pattern in self.patterns.iter() {
if pattern.include.matches_path(path) && !pattern.exclude.matches_path(path) {
pattern.apply_overrides_to_html_format_options(&mut options);
}
}
options
}

pub fn to_override_js_parser_options(
&self,
path: &Path,
Expand Down Expand Up @@ -1152,6 +1166,7 @@ pub struct OverrideSettingPattern {
pub(crate) cached_json_format_options: RwLock<Option<JsonFormatOptions>>,
pub(crate) cached_css_format_options: RwLock<Option<CssFormatOptions>>,
pub(crate) cached_graphql_format_options: RwLock<Option<GraphqlFormatOptions>>,
pub(crate) cached_html_format_options: RwLock<Option<HtmlFormatOptions>>,
pub(crate) cached_js_parser_options: RwLock<Option<JsParserOptions>>,
pub(crate) _cached_json_parser_options: RwLock<Option<JsonParserOptions>>,
pub(crate) cached_css_parser_options: RwLock<Option<CssParserOptions>>,
Expand Down Expand Up @@ -1321,6 +1336,36 @@ impl OverrideSettingPattern {
}
}

fn apply_overrides_to_html_format_options(&self, options: &mut HtmlFormatOptions) {
if let Ok(readonly_cache) = self.cached_html_format_options.read() {
if let Some(cached_options) = readonly_cache.as_ref() {
*options = cached_options.clone();
return;
}
}

let html_formatter = &self.languages.html.formatter;
let formatter = &self.formatter;

if let Some(indent_style) = html_formatter.indent_style.or(formatter.indent_style) {
options.set_indent_style(indent_style);
}
if let Some(indent_width) = html_formatter.indent_width.or(formatter.indent_width) {
options.set_indent_width(indent_width)
}
if let Some(line_ending) = html_formatter.line_ending.or(formatter.line_ending) {
options.set_line_ending(line_ending);
}
if let Some(line_width) = html_formatter.line_width.or(formatter.line_width) {
options.set_line_width(line_width);
}

if let Ok(mut writeonly_cache) = self.cached_html_format_options.write() {
let options = options.clone();
let _ = writeonly_cache.insert(options);
}
}

fn apply_overrides_to_js_parser_options(&self, options: &mut JsParserOptions) {
if let Ok(readonly_cache) = self.cached_js_parser_options.read() {
if let Some(cached_options) = readonly_cache.as_ref() {
Expand Down
Loading