Skip to content

Commit

Permalink
feat(format/grit): add basic formatting for where pattern (#4095)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Antony David <antonydavid945@gmail.com>
Co-authored-by: Victorien Elvinger <victorien@elvinger.fr>
Co-authored-by: matsuura <79092292+tunamaguro@users.noreply.github.com>
  • Loading branch information
6 people authored Oct 8, 2024
1 parent c97a4af commit d7cc01f
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 93 deletions.
1 change: 1 addition & 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_grit_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ biome_rowan = { workspace = true }
[dev-dependencies]
biome_configuration = { path = "../biome_configuration" }
biome_formatter_test = { path = "../biome_formatter_test" }
biome_fs = { path = "../biome_fs" }
biome_grit_factory = { path = "../biome_grit_factory" }
biome_grit_parser = { path = "../biome_grit_parser" }
biome_parser = { path = "../biome_parser" }
Expand Down
25 changes: 7 additions & 18 deletions crates/biome_grit_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::comments::{FormatGritLeadingComment, GritCommentStyle, GritComments};
use biome_formatter::printer::PrinterOptions;
use biome_formatter::{
AttributePosition, BracketSpacing, CstFormatContext, FormatContext, FormatOptions, IndentStyle,
IndentWidth, LineEnding, LineWidth, QuoteStyle, TransformSourceMap,
IndentWidth, LineEnding, LineWidth, TransformSourceMap,
};
use biome_grit_syntax::file_source::GritFileSource;
use biome_grit_syntax::GritLanguage;
use std::fmt::Display;
use std::rc::Rc;
Expand Down Expand Up @@ -54,27 +55,28 @@ impl CstFormatContext for GritFormatContext {
}
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct GritFormatOptions {
indent_style: IndentStyle,
indent_width: IndentWidth,
line_ending: LineEnding,
line_width: LineWidth,
quote_style: QuoteStyle,
attribute_position: AttributePosition,
_file_source: GritFileSource,
}

impl GritFormatOptions {
pub fn new() -> Self {
pub fn new(file_source: GritFileSource) -> Self {
Self {
_file_source: file_source,
indent_style: IndentStyle::default(),
indent_width: IndentWidth::default(),
line_ending: LineEnding::default(),
line_width: LineWidth::default(),
quote_style: QuoteStyle::default(),
attribute_position: AttributePosition::default(),
}
}

pub fn with_indent_style(mut self, indent_style: IndentStyle) -> Self {
self.indent_style = indent_style;
self
Expand All @@ -95,11 +97,6 @@ impl GritFormatOptions {
self
}

pub fn with_quote_style(mut self, quote_style: QuoteStyle) -> Self {
self.quote_style = quote_style;
self
}

pub fn set_indent_style(&mut self, indent_style: IndentStyle) {
self.indent_style = indent_style;
}
Expand All @@ -116,14 +113,6 @@ impl GritFormatOptions {
self.line_width = line_width;
}

pub fn set_quote_style(&mut self, quote_style: QuoteStyle) {
self.quote_style = quote_style;
}

pub fn quote_style(&self) -> QuoteStyle {
self.quote_style
}

pub fn attribute_position(&self) -> AttributePosition {
self.attribute_position
}
Expand Down
13 changes: 12 additions & 1 deletion crates/biome_grit_formatter/src/grit/lists/definition_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ pub(crate) struct FormatGritDefinitionList;
impl FormatRule<GritDefinitionList> for FormatGritDefinitionList {
type Context = GritFormatContext;
fn fmt(&self, node: &GritDefinitionList, f: &mut GritFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let mut join = f.join_nodes_with_hardline();

for definition in node {
let definition = definition?;

join.entry(
definition.syntax(),
&format_or_verbatim(definition.format()),
);
}

join.finish()
}
}
9 changes: 8 additions & 1 deletion crates/biome_grit_formatter/src/grit/lists/predicate_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ pub(crate) struct FormatGritPredicateList;
impl FormatRule<GritPredicateList> for FormatGritPredicateList {
type Context = GritFormatContext;
fn fmt(&self, node: &GritPredicateList, f: &mut GritFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let mut join = f.join_nodes_with_hardline();

for predicate in node {
let predicate = predicate?;
join.entry(predicate.syntax(), &format_or_verbatim(predicate.format()));
}

join.finish()
}
}
23 changes: 20 additions & 3 deletions crates/biome_grit_formatter/src/grit/patterns/pattern_where.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
use crate::prelude::*;
use biome_grit_syntax::GritPatternWhere;
use biome_rowan::AstNode;
use biome_formatter::write;
use biome_grit_syntax::{GritPatternWhere, GritPatternWhereFields};

#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGritPatternWhere;
impl FormatNodeRule<GritPatternWhere> for FormatGritPatternWhere {
fn fmt_fields(&self, node: &GritPatternWhere, f: &mut GritFormatter) -> FormatResult<()> {
format_verbatim_node(node.syntax()).fmt(f)
let GritPatternWhereFields {
pattern,
side_condition,
where_token,
} = node.as_fields();

write!(
f,
[
space(),
pattern.format(),
space(),
where_token.format(),
space(),
side_condition.format(),
]
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::prelude::*;
use biome_grit_syntax::GritPredicateAnd;
use biome_rowan::AstNode;
#[derive(Debug, Clone, Default)]
pub(crate) struct FormatGritPredicateAnd;
impl FormatNodeRule<GritPredicateAnd> for FormatGritPredicateAnd {
Expand Down
16 changes: 13 additions & 3 deletions crates/biome_grit_formatter/tests/language.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use biome_formatter_test::TestFormatLanguage;
use biome_fs::BiomePath;
use biome_grit_formatter::{context::GritFormatContext, GritFormatLanguage};
use biome_grit_parser::parse_grit;
use biome_grit_syntax::GritLanguage;
use biome_service::settings::ServiceLanguage;

#[derive(Default)]
pub struct GritTestFormatLanguage;
Expand All @@ -17,9 +19,17 @@ impl TestFormatLanguage for GritTestFormatLanguage {

fn to_format_language(
&self,
_settings: &biome_service::settings::Settings,
_file_source: &biome_service::workspace::DocumentFileSource,
settings: &biome_service::settings::Settings,
file_source: &biome_service::workspace::DocumentFileSource,
) -> Self::FormatLanguage {
todo!()
let language_settings = &settings.languages.grit.formatter;
let options = Self::ServiceLanguage::resolve_format_options(
Some(&settings.formatter),
Some(&settings.override_settings),
Some(language_settings),
&BiomePath::new(""),
file_source,
);
GritFormatLanguage::new(options)
}
}
7 changes: 3 additions & 4 deletions crates/biome_grit_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use biome_formatter::{IndentStyle, LineWidth, QuoteStyle};
use biome_formatter::{IndentStyle, LineWidth};
use biome_formatter_test::check_reformat::CheckReformat;
use biome_grit_formatter::context::GritFormatOptions;
use biome_grit_formatter::{format_node, GritFormatLanguage};
Expand All @@ -22,10 +22,9 @@ fn quick_test() {
}
"#;
let tree = parse_grit(src);
let options = GritFormatOptions::new()
let options = GritFormatOptions::default()
.with_indent_style(IndentStyle::Space)
.with_line_width(LineWidth::try_from(80).unwrap())
.with_quote_style(QuoteStyle::Double);
.with_line_width(LineWidth::try_from(80).unwrap());

let doc = format_node(options.clone(), &tree.syntax()).unwrap();
let result = doc.print().unwrap();
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
`$method('$message')`where{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: grit/patterns/where_pattern.grit
---
# Input

```grit
`$method('$message')`where{
}
```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Attribute Position: Auto
-----

```grit
`$method('$message')` where {
}
```



## Unimplemented nodes/tokens

"`$method('$message')`" => 0..21
" {\n\n" => 27..31
65 changes: 57 additions & 8 deletions crates/biome_service/src/file_handlers/grit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
WorkspaceError,
};
use biome_analyze::{AnalyzerConfiguration, AnalyzerOptions};
use biome_formatter::Printed;
use biome_formatter::{IndentStyle, IndentWidth, LineEnding, LineWidth, Printed};
use biome_fs::BiomePath;
use biome_grit_formatter::{context::GritFormatOptions, format_node};
use biome_grit_parser::parse_grit_with_cache;
Expand All @@ -16,8 +16,30 @@ use super::{
FormatterCapabilities, ParseResult, ParserCapabilities, SearchCapabilities,
};

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct GritFormatterSettings {
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 GritFormatterSettings {
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 GritLanguage {
type FormatterSettings = ();
type FormatterSettings = GritFormatterSettings;
type LinterSettings = ();
type OrganizeImportsSettings = ();
type FormatOptions = GritFormatOptions;
Expand All @@ -30,13 +52,40 @@ impl ServiceLanguage for GritLanguage {
}

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 {
GritFormatOptions::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 = GritFormatOptions::new(file_source.to_grit_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_grit_format_options(path, options)
} else {
options
}
}

fn resolve_analyzer_options(
Expand Down
Loading

0 comments on commit d7cc01f

Please sign in to comment.