From 7968ce69befd271d51afa2bb1387b5a183bfdc92 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Fri, 13 Sep 2024 15:46:55 -0500 Subject: [PATCH 01/15] Add gritql setup --- Cargo.lock | 7 ++ crates/biome_grit_formatter/Cargo.toml | 10 +- crates/biome_grit_formatter/src/lib.rs | 141 +++++++++++++++++++++++-- 3 files changed, 144 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8b8821bba573..6bc3012b843d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -584,6 +584,13 @@ dependencies = [ [[package]] name = "biome_grit_formatter" version = "0.0.0" +dependencies = [ + "biome_formatter", + "biome_grit_syntax", + "biome_rowan", + "serde", + "serde_json", +] [[package]] name = "biome_grit_parser" diff --git a/crates/biome_grit_formatter/Cargo.toml b/crates/biome_grit_formatter/Cargo.toml index 91f0705dd274..6d79810951d0 100644 --- a/crates/biome_grit_formatter/Cargo.toml +++ b/crates/biome_grit_formatter/Cargo.toml @@ -13,13 +13,13 @@ version = "0.0.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -#biome_formatter = { workspace = true } -#biome_grit_syntax = { workspace = true } -#biome_rowan = { workspace = true } +biome_formatter = { workspace = true } +biome_grit_syntax = { workspace = true } +biome_rowan = { workspace = true } [dev-dependencies] -#serde = { workspace = true, features = ["derive"] } -#serde_json = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } # cargo-workspaces metadata [package.metadata.workspaces] diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index 7d12d9af8195..59e77c8ff7d1 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -1,14 +1,137 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right +/// Used to get an object that knows how to format this object. +pub(crate) trait AsFormat { + type Format<'a>: biome_formatter::Format + where + Self: 'a; + + /// Returns an object that is able to format this object. + fn format(&self) -> Self::Format<'_>; +} + +/// Implement [AsFormat] for references to types that implement [AsFormat]. +impl AsFormat for &T +where + T: AsFormat, +{ + type Format<'a> = T::Format<'a> where Self: 'a; + + fn format(&self) -> Self::Format<'_> { + AsFormat::format(&**self) + } +} + +/// Implement [AsFormat] for [SyntaxResult] where `T` implements [AsFormat]. +/// +/// Useful to format mandatory AST fields without having to unwrap the value first. +impl AsFormat for biome_rowan::SyntaxResult +where + T: AsFormat, +{ + type Format<'a> = biome_rowan::SyntaxResult> where Self: 'a; + + fn format(&self) -> Self::Format<'_> { + match self { + Ok(value) => Ok(value.format()), + Err(err) => Err(*err), + } + } +} + +/// Implement [AsFormat] for [Option] when `T` implements [AsFormat] +/// +/// Allows to call format on optional AST fields without having to unwrap the field first. +impl AsFormat for Option +where + T: AsFormat, +{ + type Format<'a> = Option> where Self: 'a; + + fn format(&self) -> Self::Format<'_> { + self.as_ref().map(|value| value.format()) + } +} + +/// Used to convert this object into an object that can be formatted. +/// +/// The difference to [AsFormat] is that this trait takes ownership of `self`. +pub(crate) trait IntoFormat { + type Format: biome_formatter::Format; + + fn into_format(self) -> Self::Format; +} + +impl IntoFormat for biome_rowan::SyntaxResult +where + T: IntoFormat, +{ + type Format = biome_rowan::SyntaxResult; + + fn into_format(self) -> Self::Format { + self.map(IntoFormat::into_format) + } } -#[cfg(test)] -mod tests { - use super::*; +/// Implement [IntoFormat] for [Option] when `T` implements [IntoFormat] +/// +/// Allows to call format on optional AST fields without having to unwrap the field first. +impl IntoFormat for Option +where + T: IntoFormat, +{ + type Format = Option; - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + fn into_format(self) -> Self::Format { + self.map(IntoFormat::into_format) } } + +/// Formatting specific [Iterator] extensions +pub(crate) trait FormattedIterExt { + /// Converts every item to an object that knows how to format it. + fn formatted(self) -> FormattedIter + where + Self: Iterator + Sized, + Self::Item: IntoFormat, + { + FormattedIter { + inner: self, + options: std::marker::PhantomData, + } + } +} + +impl FormattedIterExt for I where I: std::iter::Iterator {} + +pub(crate) struct FormattedIter +where + Iter: Iterator, +{ + inner: Iter, + options: std::marker::PhantomData, +} + +impl std::iter::Iterator for FormattedIter +where + Iter: Iterator, + Item: IntoFormat, +{ + type Item = Item::Format; + + fn next(&mut self) -> Option { + Some(self.inner.next()?.into_format()) + } +} + +impl std::iter::FusedIterator for FormattedIter +where + Iter: std::iter::FusedIterator, + Item: IntoFormat, +{ +} + +impl std::iter::ExactSizeIterator for FormattedIter +where + Iter: Iterator + std::iter::ExactSizeIterator, + Item: IntoFormat, +{ +} From 933cee8b9fd24d27ed7947f7073dbd7a26596d7f Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Fri, 13 Sep 2024 17:21:04 -0500 Subject: [PATCH 02/15] Add comments for gritql --- crates/biome_grit_formatter/src/comments.rs | 28 +++++++++++++++++++++ crates/biome_grit_formatter/src/lib.rs | 1 + 2 files changed, 29 insertions(+) create mode 100644 crates/biome_grit_formatter/src/comments.rs diff --git a/crates/biome_grit_formatter/src/comments.rs b/crates/biome_grit_formatter/src/comments.rs new file mode 100644 index 000000000000..ddfb3287b3c9 --- /dev/null +++ b/crates/biome_grit_formatter/src/comments.rs @@ -0,0 +1,28 @@ +use biome_formatter::comments::{CommentStyle, Comments}; +use biome_grit_syntax::GritLanguage; + +pub type GritComments = Comments; + +#[derive(Eq, PartialEq, Copy, Clone, Debug, Default)] +pub struct GritCommentStyle; + +impl CommentStyle for GritCommentStyle { + type Language = GritLanguage; + + fn is_suppression(_text: &str) -> bool { + false + } + + fn get_comment_kind( + comment: &biome_rowan::SyntaxTriviaPieceComments, + ) -> biome_formatter::comments::CommentKind { + todo!() + } + + fn place_comment( + &self, + comment: biome_formatter::comments::DecoratedComment, + ) -> biome_formatter::comments::CommentPlacement { + biome_formatter::comments::CommentPlacement::Default(comment) + } +} diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index 59e77c8ff7d1..f770e354e175 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -1,3 +1,4 @@ +mod comments; /// Used to get an object that knows how to format this object. pub(crate) trait AsFormat { type Format<'a>: biome_formatter::Format From 20f0c80da3d542a7b07fa58f06af517a631ec4a0 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Fri, 13 Sep 2024 19:03:08 -0500 Subject: [PATCH 03/15] Additional gritql setup --- crates/biome_formatter/CONTRIBUTING.md | 14 +- crates/biome_grit_formatter/src/context.rs | 147 +++++++++++++++++++++ crates/biome_grit_formatter/src/cst.rs | 30 +++++ crates/biome_grit_formatter/src/lib.rs | 4 + crates/biome_grit_formatter/src/prelude.rs | 14 ++ 5 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 crates/biome_grit_formatter/src/context.rs create mode 100644 crates/biome_grit_formatter/src/cst.rs create mode 100644 crates/biome_grit_formatter/src/prelude.rs diff --git a/crates/biome_formatter/CONTRIBUTING.md b/crates/biome_formatter/CONTRIBUTING.md index e2d0cf9b20dd..8ec9eee564b1 100644 --- a/crates/biome_formatter/CONTRIBUTING.md +++ b/crates/biome_formatter/CONTRIBUTING.md @@ -161,11 +161,11 @@ where -Then, you'll have to create three types: +Then, you'll have to create four types: 1. `HtmlCommentStyle` -1. `HtmlFormatContext` -1. `FormatHtmlSyntaxNode` -1. `HtmlLanguage` +2. `HtmlFormatContext` +3. `FormatHtmlSyntaxNode` +4. `HtmlLanguage` ### `HtmlCommentStyle` @@ -195,12 +195,12 @@ Usually, the type context must contain `comments` and `source_map` fields: ```rust pub struct HtmlFormatContext { /// The comments of the nodes and tokens in the program. - comments: Rc, - source_map: Option, + comments: Rc, + source_map: Option, } impl HtmlFormatContext { - pub fn new(comments: CssComments) -> Self { + pub fn new(comments: HtmlComments) -> Self { Self { comments: Rc::new(comments), source_map: None, diff --git a/crates/biome_grit_formatter/src/context.rs b/crates/biome_grit_formatter/src/context.rs new file mode 100644 index 000000000000..36260e20f3c1 --- /dev/null +++ b/crates/biome_grit_formatter/src/context.rs @@ -0,0 +1,147 @@ +use crate::comments::{GritCommentStyle, GritComments}; +use biome_formatter::{ + CstFormatContext, FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding, + LineWidth, QuoteStyle, TransformSourceMap, +}; +use biome_grit_syntax::GritLanguage; +use std::rc::Rc; + +pub struct GritFormatOptions { + indent_style: IndentStyle, + indent_width: IndentWidth, + line_ending: LineEnding, + line_width: LineWidth, + quote_style: QuoteStyle, +} + +impl GritFormatOptions { + pub fn new() -> Self { + Self { + indent_style: IndentStyle::default(), + indent_width: IndentWidth::default(), + line_ending: LineEnding::default(), + line_width: LineWidth::default(), + quote_style: QuoteStyle::default(), + } + } + pub fn with_indent_style(mut self, indent_style: IndentStyle) -> Self { + self.indent_style = indent_style; + self + } + + pub fn with_indent_width(mut self, indent_width: IndentWidth) -> Self { + self.indent_width = indent_width; + self + } + + pub fn with_line_ending(mut self, line_ending: LineEnding) -> Self { + self.line_ending = line_ending; + self + } + + pub fn with_line_width(mut self, line_width: LineWidth) -> Self { + self.line_width = line_width; + 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; + } + + pub fn set_indent_width(&mut self, indent_width: IndentWidth) { + self.indent_width = indent_width; + } + + pub fn set_line_ending(&mut self, line_ending: LineEnding) { + self.line_ending = line_ending; + } + + pub fn set_line_width(&mut self, line_width: LineWidth) { + 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 + } +} + +impl FormatOptions for GritFormatOptions { + fn indent_style(&self) -> IndentStyle { + todo!() + } + + fn indent_width(&self) -> IndentWidth { + todo!() + } + + fn line_width(&self) -> LineWidth { + todo!() + } + + fn line_ending(&self) -> LineEnding { + todo!() + } + + fn attribute_position(&self) -> biome_formatter::AttributePosition { + todo!() + } + + fn bracket_spacing(&self) -> biome_formatter::BracketSpacing { + todo!() + } + + fn as_print_options(&self) -> biome_formatter::prelude::PrinterOptions { + todo!() + } +} + +pub struct GritFormatContext { + comments: Rc, + source_map: Option, +} + +impl GritFormatContext { + pub fn new(comments: GritComments) -> Self { + Self { + comments: Rc::new(comments), + source_map: None, + } + } + + pub fn with_source_map(mut self, source_map: Option) -> Self { + self.source_map = source_map; + self + } +} + +impl FormatContext for GritFormatContext { + type Options = GritFormatOptions; + + fn options(&self) -> &Self::Options { + todo!() + } + + fn source_map(&self) -> Option<&TransformSourceMap> { + todo!() + } +} +impl CstFormatContext for GritFormatContext { + type Language = GritLanguage; + + type Style = GritCommentStyle; + + type CommentRule; + + fn comments(&self) -> &biome_formatter::comments::Comments { + todo!() + } +} diff --git a/crates/biome_grit_formatter/src/cst.rs b/crates/biome_grit_formatter/src/cst.rs new file mode 100644 index 000000000000..9c94a98145bf --- /dev/null +++ b/crates/biome_grit_formatter/src/cst.rs @@ -0,0 +1,30 @@ +use crate::prelude::*; +use biome_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatResult}; +use biome_grit_syntax::{map_syntax_node, GritSyntaxNode}; + +#[derive(Debug, Copy, Clone, Default)] +pub struct FormatGritSyntaxNode; + +impl FormatRule for FormatGritSyntaxNode { + type Context = GritFormatContext; + + fn fmt(&self, node: &GritSyntaxNode, f: &mut GritFormatter) -> FormatResult<()> { + map_syntax_node!(node.clone(), node => node.format().fmt(f)) + } +} + +impl AsFormat for FormatGritSyntaxNode { + type Format<'a> = FormatRefWithRule<'a, HtmlSyntaxNode, FormatHtmlSyntaxNode>; + + fn format(&self) -> Self::Format<'_> { + FormatRefWithRule::new(self, FormatHtmlSyntaxNode) + } +} + +impl IntoFormat for FormatHtmlSyntaxNode { + type Format = FormatOwnedWithRule; + + fn into_format(self) -> Self::Format { + FormatOwnedWithRule::new(self, FormatHtmlSyntaxNode) + } +} diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index f770e354e175..1e22c1fb1aab 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -1,4 +1,8 @@ mod comments; +pub mod context; +mod cst; +mod prelude; + /// Used to get an object that knows how to format this object. pub(crate) trait AsFormat { type Format<'a>: biome_formatter::Format diff --git a/crates/biome_grit_formatter/src/prelude.rs b/crates/biome_grit_formatter/src/prelude.rs new file mode 100644 index 000000000000..2ab35a94d691 --- /dev/null +++ b/crates/biome_grit_formatter/src/prelude.rs @@ -0,0 +1,14 @@ +//! This module provides important and useful traits to help to format tokens and nodes +//! when implementing the [crate::FormatNodeRule] trait. + +#[allow(unused_imports)] +pub(crate) use crate::{ + AsFormat, FormatNodeRule, FormattedIterExt as _, GritFormatContext, GritFormatter, IntoFormat, +}; +pub(crate) use biome_formatter::prelude::*; +#[allow(unused_imports)] +pub(crate) use biome_rowan::{ + AstNode as _, AstNodeList as _, AstNodeSlotMap as _, AstSeparatedList as _, +}; + +pub(crate) use crate::separated::FormatAstSeparatedListExtension; From c3b0d7f6614d11f860e031308ac67e2126d03cd8 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sat, 14 Sep 2024 19:33:23 -0500 Subject: [PATCH 04/15] Additional gritql format language and fix up prelude --- crates/biome_formatter/CONTRIBUTING.md | 2 +- crates/biome_grit_formatter/src/context.rs | 85 +++++++++++----------- crates/biome_grit_formatter/src/cst.rs | 12 +-- crates/biome_grit_formatter/src/lib.rs | 58 +++++++++++++++ crates/biome_grit_formatter/src/prelude.rs | 6 +- 5 files changed, 109 insertions(+), 54 deletions(-) diff --git a/crates/biome_formatter/CONTRIBUTING.md b/crates/biome_formatter/CONTRIBUTING.md index 8ec9eee564b1..7dbbf760cb04 100644 --- a/crates/biome_formatter/CONTRIBUTING.md +++ b/crates/biome_formatter/CONTRIBUTING.md @@ -268,7 +268,7 @@ impl IntoFormat for FormatHtmlSyntaxNode { This is small type that you need to instruct the formatter infra about a certain language. This type needs to implement the trait `biome_formatter::FormatLanguage` ```rust -impl FormatLanguage for HtmlLanguage { +impl FormatLanguage for HtmlFormatLanguage { type SyntaxLanguage = HtmlLanguage; type Context = HtmlFormatContext; type FormatRule = FormatHtmlSyntaxNode; diff --git a/crates/biome_grit_formatter/src/context.rs b/crates/biome_grit_formatter/src/context.rs index 36260e20f3c1..49ce3da87f21 100644 --- a/crates/biome_grit_formatter/src/context.rs +++ b/crates/biome_grit_formatter/src/context.rs @@ -6,6 +6,49 @@ use biome_formatter::{ use biome_grit_syntax::GritLanguage; use std::rc::Rc; +#[derive(Debug)] +pub struct GritFormatContext { + comments: Rc, + source_map: Option, +} + +impl GritFormatContext { + pub fn new(comments: GritComments) -> Self { + Self { + comments: Rc::new(comments), + source_map: None, + } + } + + pub fn with_source_map(mut self, source_map: Option) -> Self { + self.source_map = source_map; + self + } +} + +impl FormatContext for GritFormatContext { + type Options = GritFormatOptions; + + fn options(&self) -> &Self::Options { + todo!() + } + + fn source_map(&self) -> Option<&TransformSourceMap> { + todo!() + } +} +impl CstFormatContext for GritFormatContext { + type Language = GritLanguage; + + type Style = GritCommentStyle; + + type CommentRule; + + fn comments(&self) -> &biome_formatter::comments::Comments { + todo!() + } +} + pub struct GritFormatOptions { indent_style: IndentStyle, indent_width: IndentWidth, @@ -103,45 +146,3 @@ impl FormatOptions for GritFormatOptions { todo!() } } - -pub struct GritFormatContext { - comments: Rc, - source_map: Option, -} - -impl GritFormatContext { - pub fn new(comments: GritComments) -> Self { - Self { - comments: Rc::new(comments), - source_map: None, - } - } - - pub fn with_source_map(mut self, source_map: Option) -> Self { - self.source_map = source_map; - self - } -} - -impl FormatContext for GritFormatContext { - type Options = GritFormatOptions; - - fn options(&self) -> &Self::Options { - todo!() - } - - fn source_map(&self) -> Option<&TransformSourceMap> { - todo!() - } -} -impl CstFormatContext for GritFormatContext { - type Language = GritLanguage; - - type Style = GritCommentStyle; - - type CommentRule; - - fn comments(&self) -> &biome_formatter::comments::Comments { - todo!() - } -} diff --git a/crates/biome_grit_formatter/src/cst.rs b/crates/biome_grit_formatter/src/cst.rs index 9c94a98145bf..62d6a2d006f7 100644 --- a/crates/biome_grit_formatter/src/cst.rs +++ b/crates/biome_grit_formatter/src/cst.rs @@ -1,4 +1,4 @@ -use crate::prelude::*; +use crate::{context::GritFormatContext, prelude::*}; use biome_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatResult}; use biome_grit_syntax::{map_syntax_node, GritSyntaxNode}; @@ -14,17 +14,17 @@ impl FormatRule for FormatGritSyntaxNode { } impl AsFormat for FormatGritSyntaxNode { - type Format<'a> = FormatRefWithRule<'a, HtmlSyntaxNode, FormatHtmlSyntaxNode>; + type Format<'a> = FormatRefWithRule<'a, GritSyntaxNode, FormatGritSyntaxNode>; fn format(&self) -> Self::Format<'_> { - FormatRefWithRule::new(self, FormatHtmlSyntaxNode) + FormatRefWithRule::new(self, FormatGritSyntaxNode) } } -impl IntoFormat for FormatHtmlSyntaxNode { - type Format = FormatOwnedWithRule; +impl IntoFormat for FormatGritSyntaxNode { + type Format = FormatOwnedWithRule; fn into_format(self) -> Self::Format { - FormatOwnedWithRule::new(self, FormatHtmlSyntaxNode) + FormatOwnedWithRule::new(self, FormatGritSyntaxNode) } } diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index 1e22c1fb1aab..a03eb3e82ea4 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -1,8 +1,66 @@ mod comments; pub mod context; mod cst; +mod generated; +mod grit; mod prelude; +use biome_formatter::FormatLanguage; +use biome_grit_syntax::GritLanguage; + +use context::GritFormatOptions; +use cst::FormatGritSyntaxNode; + +pub(crate) use crate::context::GritFormatContext; + +#[derive(Debug, Clone)] +pub struct GritFormatLanguage { + options: GritFormatOptions, +} + +impl GritFormatLanguage { + pub fn new(options: GritFormatOptions) -> Self { + Self { options } + } +} + +impl FormatLanguage for GritFormatLanguage { + type SyntaxLanguage = GritLanguage; + + type Context = GritFormatContext; + + type FormatRule = FormatGritSyntaxNode; + + fn transform( + &self, + _root: &biome_rowan::SyntaxNode, + ) -> Option<( + biome_rowan::SyntaxNode, + biome_formatter::TransformSourceMap, + )> { + None + } + + fn is_range_formatting_node( + &self, + _node: &biome_rowan::SyntaxNode, + ) -> bool { + true + } + + fn options(&self) -> &::Options { + todo!() + } + + fn create_context( + self, + root: &biome_rowan::SyntaxNode, + source_map: Option, + ) -> Self::Context { + todo!() + } +} + /// Used to get an object that knows how to format this object. pub(crate) trait AsFormat { type Format<'a>: biome_formatter::Format diff --git a/crates/biome_grit_formatter/src/prelude.rs b/crates/biome_grit_formatter/src/prelude.rs index 2ab35a94d691..2811a80200fe 100644 --- a/crates/biome_grit_formatter/src/prelude.rs +++ b/crates/biome_grit_formatter/src/prelude.rs @@ -2,13 +2,9 @@ //! when implementing the [crate::FormatNodeRule] trait. #[allow(unused_imports)] -pub(crate) use crate::{ - AsFormat, FormatNodeRule, FormattedIterExt as _, GritFormatContext, GritFormatter, IntoFormat, -}; +pub(crate) use crate::{AsFormat, FormattedIterExt as _, GritFormatContext, IntoFormat}; pub(crate) use biome_formatter::prelude::*; #[allow(unused_imports)] pub(crate) use biome_rowan::{ AstNode as _, AstNodeList as _, AstNodeSlotMap as _, AstSeparatedList as _, }; - -pub(crate) use crate::separated::FormatAstSeparatedListExtension; From eaa13376769d71c6678158d394a95c0d8c76d8d1 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sat, 14 Sep 2024 19:42:21 -0500 Subject: [PATCH 05/15] hook up trait to handle formatting --- crates/biome_formatter/CONTRIBUTING.md | 12 ++--- crates/biome_grit_formatter/src/cst.rs | 2 +- crates/biome_grit_formatter/src/lib.rs | 62 +++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/crates/biome_formatter/CONTRIBUTING.md b/crates/biome_formatter/CONTRIBUTING.md index 7dbbf760cb04..930b702fe33e 100644 --- a/crates/biome_formatter/CONTRIBUTING.md +++ b/crates/biome_formatter/CONTRIBUTING.md @@ -294,7 +294,7 @@ where N: AstNode, { // this is the method that actually start the formatting - fn fmt(&self, node: &N, f: &mut HtmlForamtter) -> FormatResult<()> { + fn fmt(&self, node: &N, f: &mut HtmlFormatter) -> FormatResult<()> { if self.is_suppressed(node, f) { return write!(f, [format_suppressed_node(node.syntax())]); } @@ -305,10 +305,10 @@ where self.fmt_trailing_comments(node, f) } - fn fmt_fields(&self, node: &N, f: &mut HtmlForamtter) -> FormatResult<()>; + fn fmt_fields(&self, node: &N, f: &mut HtmlFormatter) -> FormatResult<()>; /// Returns `true` if the node has a suppression comment and should use the same formatting as in the source document. - fn is_suppressed(&self, node: &N, f: &JsonFormatter) -> bool { + fn is_suppressed(&self, node: &N, f: &HtmlFormatter) -> bool { f.context().comments().is_suppressed(node.syntax()) } @@ -316,7 +316,7 @@ where /// /// You may want to override this method if you want to manually handle the formatting of comments /// inside of the `fmt_fields` method or customize the formatting of the leading comments. - fn fmt_leading_comments(&self, node: &N, f: &mut HtmlForamtter) -> FormatResult<()> { + fn fmt_leading_comments(&self, node: &N, f: &mut HtmlFormatter) -> FormatResult<()> { format_leading_comments(node.syntax()).fmt(f) } @@ -327,7 +327,7 @@ where /// no comments are dropped. /// /// A node can have dangling comments if all its children are tokens or if all node childrens are optional. - fn fmt_dangling_comments(&self, node: &N, f: &mut HtmlForamtter) -> FormatResult<()> { + fn fmt_dangling_comments(&self, node: &N, f: &mut HtmlFormatter) -> FormatResult<()> { format_dangling_comments(node.syntax()) .with_soft_block_indent() .fmt(f) @@ -337,7 +337,7 @@ where /// /// You may want to override this method if you want to manually handle the formatting of comments /// inside of the `fmt_fields` method or customize the formatting of the trailing comments. - fn fmt_trailing_comments(&self, node: &N, f: &mut HtmlForamtter) -> FormatResult<()> { + fn fmt_trailing_comments(&self, node: &N, f: &mut HtmlFormatter) -> FormatResult<()> { format_trailing_comments(node.syntax()).fmt(f) } } diff --git a/crates/biome_grit_formatter/src/cst.rs b/crates/biome_grit_formatter/src/cst.rs index 62d6a2d006f7..d1435088b02e 100644 --- a/crates/biome_grit_formatter/src/cst.rs +++ b/crates/biome_grit_formatter/src/cst.rs @@ -1,4 +1,4 @@ -use crate::{context::GritFormatContext, prelude::*}; +use crate::{context::GritFormatContext, prelude::*, GritFormatter}; use biome_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatResult}; use biome_grit_syntax::{map_syntax_node, GritSyntaxNode}; diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index a03eb3e82ea4..c51191f9ad9d 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -5,14 +5,74 @@ mod generated; mod grit; mod prelude; -use biome_formatter::FormatLanguage; +use biome_formatter::{ + prelude::{format_suppressed_node, Formatter}, + trivia::{format_dangling_comments, format_leading_comments, format_trailing_comments}, + write, FormatLanguage, FormatResult, +}; use biome_grit_syntax::GritLanguage; +use biome_rowan::AstNode; use context::GritFormatOptions; use cst::FormatGritSyntaxNode; pub(crate) use crate::context::GritFormatContext; +pub(crate) type GritFormatter<'buf> = Formatter<'buf, GritFormatContext>; + +pub(crate) trait FormatNodeRule +where + N: AstNode, +{ + // this is the method that actually start the formatting + fn fmt(&self, node: &N, f: &mut GritFormatter) -> FormatResult<()> { + if self.is_suppressed(node, f) { + return write!(f, [format_suppressed_node(node.syntax())]); + } + + self.fmt_leading_comments(node, f)?; + self.fmt_fields(node, f)?; + self.fmt_dangling_comments(node, f)?; + self.fmt_trailing_comments(node, f) + } + + fn fmt_fields(&self, node: &N, f: &mut GritFormatter) -> FormatResult<()>; + + /// Returns `true` if the node has a suppression comment and should use the same formatting as in the source document. + fn is_suppressed(&self, node: &N, f: &GritFormatter) -> bool { + f.context().comments().is_suppressed(node.syntax()) + } + + /// Formats the [leading comments](biome_formatter::comments#leading-comments) of the node. + /// + /// You may want to override this method if you want to manually handle the formatting of comments + /// inside of the `fmt_fields` method or customize the formatting of the leading comments. + fn fmt_leading_comments(&self, node: &N, f: &mut GritFormatter) -> FormatResult<()> { + format_leading_comments(node.syntax()).fmt(f) + } + + /// Formats the [dangling comments](biome_formatter::comments#dangling-comments) of the node. + /// + /// You should override this method if the node handled by this rule can have dangling comments because the + /// default implementation formats the dangling comments at the end of the node, which isn't ideal but ensures that + /// no comments are dropped. + /// + /// A node can have dangling comments if all its children are tokens or if all node childrens are optional. + fn fmt_dangling_comments(&self, node: &N, f: &mut GritFormatter) -> FormatResult<()> { + format_dangling_comments(node.syntax()) + .with_soft_block_indent() + .fmt(f) + } + + /// Formats the [trailing comments](biome_formatter::comments#trailing-comments) of the node. + /// + /// You may want to override this method if you want to manually handle the formatting of comments + /// inside of the `fmt_fields` method or customize the formatting of the trailing comments. + fn fmt_trailing_comments(&self, node: &N, f: &mut GritFormatter) -> FormatResult<()> { + format_trailing_comments(node.syntax()).fmt(f) + } +} + #[derive(Debug, Clone)] pub struct GritFormatLanguage { options: GritFormatOptions, From 64c14451bb05e07677fcc00a8ac70bb987df793e Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sat, 14 Sep 2024 19:43:54 -0500 Subject: [PATCH 06/15] Revert change to contributing.md --- crates/biome_formatter/CONTRIBUTING.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/biome_formatter/CONTRIBUTING.md b/crates/biome_formatter/CONTRIBUTING.md index 930b702fe33e..1f6f70b02ea8 100644 --- a/crates/biome_formatter/CONTRIBUTING.md +++ b/crates/biome_formatter/CONTRIBUTING.md @@ -163,9 +163,9 @@ where Then, you'll have to create four types: 1. `HtmlCommentStyle` -2. `HtmlFormatContext` -3. `FormatHtmlSyntaxNode` -4. `HtmlLanguage` +1. `HtmlFormatContext` +1. `FormatHtmlSyntaxNode` +1. `HtmlLanguage` ### `HtmlCommentStyle` From 55ca57ab168cec78ea23d7e18719fa56ea8e35d7 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sat, 14 Sep 2024 19:50:00 -0500 Subject: [PATCH 07/15] ADd formatgritleadingcomment for cst.rs --- crates/biome_grit_formatter/src/comments.rs | 55 ++++++++++++++++++++- crates/biome_grit_formatter/src/context.rs | 2 +- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/crates/biome_grit_formatter/src/comments.rs b/crates/biome_grit_formatter/src/comments.rs index ddfb3287b3c9..94a5ab2e8370 100644 --- a/crates/biome_grit_formatter/src/comments.rs +++ b/crates/biome_grit_formatter/src/comments.rs @@ -1,6 +1,12 @@ -use biome_formatter::comments::{CommentStyle, Comments}; +use biome_formatter::{ + comments::{is_doc_comment, CommentStyle, Comments, SourceComment}, + prelude::{align, dynamic_text, format_once, hard_line_break, Formatter}, + write, FormatResult, FormatRule, +}; use biome_grit_syntax::GritLanguage; +use crate::GritFormatContext; + pub type GritComments = Comments; #[derive(Eq, PartialEq, Copy, Clone, Debug, Default)] @@ -26,3 +32,50 @@ impl CommentStyle for GritCommentStyle { biome_formatter::comments::CommentPlacement::Default(comment) } } + +#[derive(Default)] +pub struct FormatGritLeadingComment; + +impl FormatRule> for FormatGritLeadingComment { + type Context = GritFormatContext; + // Copied and pasted this from the css formatter, not sure how much this needs to change. + fn fmt( + &self, + comment: &SourceComment, + f: &mut Formatter, + ) -> FormatResult<()> { + if is_doc_comment(comment.piece()) { + let mut source_offset = comment.piece().text_range().start(); + + let mut lines = comment.piece().text().lines(); + + // SAFETY: Safe, `is_doc_comment` only returns `true` for multiline comments + let first_line = lines.next().unwrap(); + write!(f, [dynamic_text(first_line.trim_end(), source_offset)])?; + + source_offset += first_line.text_len(); + + // Indent the remaining lines by one space so that all `*` are aligned. + write!( + f, + [align( + 1, + &format_once(|f| { + for line in lines { + write!( + f, + [hard_line_break(), dynamic_text(line.trim(), source_offset)] + )?; + + source_offset += line.text_len(); + } + + Ok(()) + }) + )] + ) + } else { + write!(f, [comment.piece().as_piece()]) + } + } +} diff --git a/crates/biome_grit_formatter/src/context.rs b/crates/biome_grit_formatter/src/context.rs index 49ce3da87f21..c403aa88ab84 100644 --- a/crates/biome_grit_formatter/src/context.rs +++ b/crates/biome_grit_formatter/src/context.rs @@ -42,7 +42,7 @@ impl CstFormatContext for GritFormatContext { type Style = GritCommentStyle; - type CommentRule; + type CommentRule = FormatGritLeadingComment; fn comments(&self) -> &biome_formatter::comments::Comments { todo!() From 7f198229e290e681b704ae26eb06795cc3ec12c4 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sat, 14 Sep 2024 19:51:24 -0500 Subject: [PATCH 08/15] add comment rule --- crates/biome_grit_formatter/src/context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/biome_grit_formatter/src/context.rs b/crates/biome_grit_formatter/src/context.rs index c403aa88ab84..8075b6e8177d 100644 --- a/crates/biome_grit_formatter/src/context.rs +++ b/crates/biome_grit_formatter/src/context.rs @@ -1,4 +1,4 @@ -use crate::comments::{GritCommentStyle, GritComments}; +use crate::comments::{FormatGritLeadingComment, GritCommentStyle, GritComments}; use biome_formatter::{ CstFormatContext, FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding, LineWidth, QuoteStyle, TransformSourceMap, From 98e02ef0f85329a36da3ee449ba119250825203f Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sun, 15 Sep 2024 14:33:06 -0500 Subject: [PATCH 09/15] Resolve outstanding errors --- crates/biome_formatter/CONTRIBUTING.md | 3 +- crates/biome_grit_formatter/src/comments.rs | 4 +-- crates/biome_grit_formatter/src/context.rs | 2 ++ crates/biome_grit_formatter/src/cst.rs | 6 ++-- crates/biome_grit_formatter/src/lib.rs | 32 +++++++++++++++++---- crates/biome_grit_formatter/src/prelude.rs | 4 ++- 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/crates/biome_formatter/CONTRIBUTING.md b/crates/biome_formatter/CONTRIBUTING.md index 1f6f70b02ea8..7a7a90bea07a 100644 --- a/crates/biome_formatter/CONTRIBUTING.md +++ b/crates/biome_formatter/CONTRIBUTING.md @@ -349,9 +349,10 @@ Now that everything is wired, you just needs to expose a public method that does ```rust pub fn format_node( + options: HtmlFormatOptions, root: &HtmlSyntaxNode, ) -> FormatResult> { - biome_formatter::format_node(root, HtmlFormatLanguage {}) + biome_formatter::format_node(root, HtmlFormatLanguage::new(options)) } ``` diff --git a/crates/biome_grit_formatter/src/comments.rs b/crates/biome_grit_formatter/src/comments.rs index 94a5ab2e8370..aba65e24d12c 100644 --- a/crates/biome_grit_formatter/src/comments.rs +++ b/crates/biome_grit_formatter/src/comments.rs @@ -1,11 +1,11 @@ +use crate::GritFormatContext; use biome_formatter::{ comments::{is_doc_comment, CommentStyle, Comments, SourceComment}, prelude::{align, dynamic_text, format_once, hard_line_break, Formatter}, write, FormatResult, FormatRule, }; use biome_grit_syntax::GritLanguage; - -use crate::GritFormatContext; +use biome_rowan::TextLen; pub type GritComments = Comments; diff --git a/crates/biome_grit_formatter/src/context.rs b/crates/biome_grit_formatter/src/context.rs index 8075b6e8177d..6cf82ebe34e2 100644 --- a/crates/biome_grit_formatter/src/context.rs +++ b/crates/biome_grit_formatter/src/context.rs @@ -49,6 +49,8 @@ impl CstFormatContext for GritFormatContext { } } +#[derive(Debug, Default, Clone, PartialEq)] + pub struct GritFormatOptions { indent_style: IndentStyle, indent_width: IndentWidth, diff --git a/crates/biome_grit_formatter/src/cst.rs b/crates/biome_grit_formatter/src/cst.rs index d1435088b02e..58595b25d5a5 100644 --- a/crates/biome_grit_formatter/src/cst.rs +++ b/crates/biome_grit_formatter/src/cst.rs @@ -1,4 +1,4 @@ -use crate::{context::GritFormatContext, prelude::*, GritFormatter}; +use crate::prelude::*; use biome_formatter::{FormatOwnedWithRule, FormatRefWithRule, FormatResult}; use biome_grit_syntax::{map_syntax_node, GritSyntaxNode}; @@ -13,7 +13,7 @@ impl FormatRule for FormatGritSyntaxNode { } } -impl AsFormat for FormatGritSyntaxNode { +impl AsFormat for GritSyntaxNode { type Format<'a> = FormatRefWithRule<'a, GritSyntaxNode, FormatGritSyntaxNode>; fn format(&self) -> Self::Format<'_> { @@ -21,7 +21,7 @@ impl AsFormat for FormatGritSyntaxNode { } } -impl IntoFormat for FormatGritSyntaxNode { +impl IntoFormat for GritSyntaxNode { type Format = FormatOwnedWithRule; fn into_format(self) -> Self::Format { diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index c51191f9ad9d..bfcfb1a844fb 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -6,20 +6,30 @@ mod grit; mod prelude; use biome_formatter::{ - prelude::{format_suppressed_node, Formatter}, + prelude::{format_bogus_node, format_suppressed_node, Formatter}, trivia::{format_dangling_comments, format_leading_comments, format_trailing_comments}, - write, FormatLanguage, FormatResult, + write, CstFormatContext, Format, FormatLanguage, FormatResult, Formatted, }; -use biome_grit_syntax::GritLanguage; +use biome_grit_syntax::{GritLanguage, GritSyntaxNode}; + +pub(crate) use crate::context::GritFormatContext; use biome_rowan::AstNode; use context::GritFormatOptions; use cst::FormatGritSyntaxNode; -pub(crate) use crate::context::GritFormatContext; - pub(crate) type GritFormatter<'buf> = Formatter<'buf, GritFormatContext>; +/// Formats a GritQL file based on its features. +/// +/// It returns a [Formatted] result, which the user can use to override a file. +pub fn format_node( + options: GritFormatOptions, + root: &GritSyntaxNode, +) -> FormatResult> { + biome_formatter::format_node(root, GritFormatLanguage::new(options)) +} + pub(crate) trait FormatNodeRule where N: AstNode, @@ -73,7 +83,7 @@ where } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct GritFormatLanguage { options: GritFormatOptions, } @@ -258,3 +268,13 @@ where Item: IntoFormat, { } + +/// Rule for formatting an bogus nodes. +pub(crate) trait FormatBogusNodeRule +where + N: AstNode, +{ + fn fmt(&self, node: &N, f: &mut GritFormatter) -> FormatResult<()> { + format_bogus_node(node.syntax()).fmt(f) + } +} diff --git a/crates/biome_grit_formatter/src/prelude.rs b/crates/biome_grit_formatter/src/prelude.rs index 2811a80200fe..3dc661422125 100644 --- a/crates/biome_grit_formatter/src/prelude.rs +++ b/crates/biome_grit_formatter/src/prelude.rs @@ -2,7 +2,9 @@ //! when implementing the [crate::FormatNodeRule] trait. #[allow(unused_imports)] -pub(crate) use crate::{AsFormat, FormattedIterExt as _, GritFormatContext, IntoFormat}; +pub(crate) use crate::{ + AsFormat, FormatNodeRule, FormattedIterExt as _, GritFormatContext, GritFormatter, IntoFormat, +}; pub(crate) use biome_formatter::prelude::*; #[allow(unused_imports)] pub(crate) use biome_rowan::{ From 48d57f05e260e582b71490f7ba3a19f4212fe273 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sun, 15 Sep 2024 14:51:00 -0500 Subject: [PATCH 10/15] finish setup for grit formatter --- crates/biome_grit_formatter/Cargo.toml | 15 ++++++++++++--- crates/biome_grit_formatter/src/lib.rs | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/biome_grit_formatter/Cargo.toml b/crates/biome_grit_formatter/Cargo.toml index 6d79810951d0..31753b9bd455 100644 --- a/crates/biome_grit_formatter/Cargo.toml +++ b/crates/biome_grit_formatter/Cargo.toml @@ -18,9 +18,18 @@ biome_grit_syntax = { workspace = true } biome_rowan = { workspace = true } [dev-dependencies] -serde = { workspace = true, features = ["derive"] } -serde_json = { workspace = true } - +biome_formatter_test = { path = "../biome_formatter_test" } +biome_grit_factory = { path = "../biome_grit_parser" } +biome_grit_parser = { path = "../biome_grit_parser" } +biome_parser = { path = "../biome_parser" } +biome_service = { path = "../biome_service" } +countme = { workspace = true, features = ["enable"] } +iai = "0.1.1" +quickcheck = { workspace = true } +quickcheck_macros = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +tests_macros = { path = "../tests_macros" } # cargo-workspaces metadata [package.metadata.workspaces] independent = true diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index bfcfb1a844fb..a7bb5c7107cc 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -119,7 +119,7 @@ impl FormatLanguage for GritFormatLanguage { } fn options(&self) -> &::Options { - todo!() + &self.options } fn create_context( From 7838b4b24f5e4bb38fbff06c170b58fdc2228e12 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sun, 15 Sep 2024 14:58:11 -0500 Subject: [PATCH 11/15] Update some more documentation --- crates/biome_formatter/CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/biome_formatter/CONTRIBUTING.md b/crates/biome_formatter/CONTRIBUTING.md index 7a7a90bea07a..21bd4cc0db91 100644 --- a/crates/biome_formatter/CONTRIBUTING.md +++ b/crates/biome_formatter/CONTRIBUTING.md @@ -244,7 +244,7 @@ impl FormatRule for FormatHtmlSyntaxNode { } } -impl AsFormat for FormatHtmlSyntaxNode { +impl AsFormat for HtmlSyntaxNode { type Format<'a> = FormatRefWithRule<'a, HtmlSyntaxNode, FormatHtmlSyntaxNode>; fn format(&self) -> Self::Format<'_> { @@ -252,7 +252,7 @@ impl AsFormat for FormatHtmlSyntaxNode { } } -impl IntoFormat for FormatHtmlSyntaxNode { +impl IntoFormat for HtmlSyntaxNode { type Format = FormatOwnedWithRule; fn into_format(self) -> Self::Format { From e4b66f8e1bea33d2fa482ed068eb1d173f26ad32 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sun, 15 Sep 2024 17:00:46 -0500 Subject: [PATCH 12/15] Use correct dev dep name and update documentation --- Cargo.lock | 10 ++++++++++ crates/biome_formatter/CONTRIBUTING.md | 2 +- crates/biome_grit_formatter/Cargo.toml | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6bc3012b843d..1ff0e0dfe598 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -586,10 +586,20 @@ name = "biome_grit_formatter" version = "0.0.0" dependencies = [ "biome_formatter", + "biome_formatter_test", + "biome_grit_factory", + "biome_grit_parser", "biome_grit_syntax", + "biome_parser", "biome_rowan", + "biome_service", + "countme", + "iai", + "quickcheck", + "quickcheck_macros", "serde", "serde_json", + "tests_macros", ] [[package]] diff --git a/crates/biome_formatter/CONTRIBUTING.md b/crates/biome_formatter/CONTRIBUTING.md index 21bd4cc0db91..0d36e8f2bf9a 100644 --- a/crates/biome_formatter/CONTRIBUTING.md +++ b/crates/biome_formatter/CONTRIBUTING.md @@ -380,7 +380,7 @@ Updated the `Cargo.toml` file to import some testing utility: ```toml [dev-dependencies] biome_formatter_test = { path = "../biome_formatter_test" } -biome_html_factory = { path = "../biome_html_parser" } +biome_html_factory = { path = "../biome_html_factory" } biome_html_parser = { path = "../biome_html_parser" } biome_parser = { path = "../biome_parser" } biome_service = { path = "../biome_service" } diff --git a/crates/biome_grit_formatter/Cargo.toml b/crates/biome_grit_formatter/Cargo.toml index 31753b9bd455..e75a5e99f567 100644 --- a/crates/biome_grit_formatter/Cargo.toml +++ b/crates/biome_grit_formatter/Cargo.toml @@ -19,7 +19,7 @@ biome_rowan = { workspace = true } [dev-dependencies] biome_formatter_test = { path = "../biome_formatter_test" } -biome_grit_factory = { path = "../biome_grit_parser" } +biome_grit_factory = { path = "../biome_grit_factory" } biome_grit_parser = { path = "../biome_grit_parser" } biome_parser = { path = "../biome_parser" } biome_service = { path = "../biome_service" } From 66a068869b68a083a1acbaa82c7f32eb8352f289 Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sun, 15 Sep 2024 17:25:36 -0500 Subject: [PATCH 13/15] add buffer trait --- crates/biome_grit_formatter/src/context.rs | 2 +- crates/biome_grit_formatter/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/biome_grit_formatter/src/context.rs b/crates/biome_grit_formatter/src/context.rs index 6cf82ebe34e2..abc872667575 100644 --- a/crates/biome_grit_formatter/src/context.rs +++ b/crates/biome_grit_formatter/src/context.rs @@ -6,7 +6,7 @@ use biome_formatter::{ use biome_grit_syntax::GritLanguage; use std::rc::Rc; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct GritFormatContext { comments: Rc, source_map: Option, diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index a7bb5c7107cc..b33a68ee9805 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -8,7 +8,7 @@ mod prelude; use biome_formatter::{ prelude::{format_bogus_node, format_suppressed_node, Formatter}, trivia::{format_dangling_comments, format_leading_comments, format_trailing_comments}, - write, CstFormatContext, Format, FormatLanguage, FormatResult, Formatted, + write, Buffer, CstFormatContext, Format, FormatLanguage, FormatResult, Formatted, }; use biome_grit_syntax::{GritLanguage, GritSyntaxNode}; @@ -119,7 +119,7 @@ impl FormatLanguage for GritFormatLanguage { } fn options(&self) -> &::Options { - &self.options + todo!() } fn create_context( From 9120f0ca5d7e02ea2fcb56e075ee07d6b56cb32e Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sun, 15 Sep 2024 17:29:47 -0500 Subject: [PATCH 14/15] add prelude --- crates/biome_grit_formatter/src/comments.rs | 2 ++ crates/biome_grit_formatter/src/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/biome_grit_formatter/src/comments.rs b/crates/biome_grit_formatter/src/comments.rs index aba65e24d12c..3980222dc2d6 100644 --- a/crates/biome_grit_formatter/src/comments.rs +++ b/crates/biome_grit_formatter/src/comments.rs @@ -1,6 +1,8 @@ use crate::GritFormatContext; + use biome_formatter::{ comments::{is_doc_comment, CommentStyle, Comments, SourceComment}, + prelude::*, prelude::{align, dynamic_text, format_once, hard_line_break, Formatter}, write, FormatResult, FormatRule, }; diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index b33a68ee9805..26cdfc18d89b 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -6,9 +6,9 @@ mod grit; mod prelude; use biome_formatter::{ - prelude::{format_bogus_node, format_suppressed_node, Formatter}, + prelude::*, trivia::{format_dangling_comments, format_leading_comments, format_trailing_comments}, - write, Buffer, CstFormatContext, Format, FormatLanguage, FormatResult, Formatted, + write, CstFormatContext, Format, FormatLanguage, FormatResult, Formatted, }; use biome_grit_syntax::{GritLanguage, GritSyntaxNode}; From b7133b027f3757ebf192e1d548431c26e58c471e Mon Sep 17 00:00:00 2001 From: Brandon Ly Date: Sun, 15 Sep 2024 18:26:23 -0500 Subject: [PATCH 15/15] Resolve linting issues --- Cargo.lock | 10 ---- crates/biome_grit_formatter/Cargo.toml | 15 +---- crates/biome_grit_formatter/src/comments.rs | 2 +- crates/biome_grit_formatter/src/context.rs | 1 + crates/biome_grit_formatter/src/lib.rs | 8 ++- crates/biome_unicode_table/src/tables.rs | 63 +++++++++++++++++---- 6 files changed, 63 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ff0e0dfe598..6bc3012b843d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -586,20 +586,10 @@ name = "biome_grit_formatter" version = "0.0.0" dependencies = [ "biome_formatter", - "biome_formatter_test", - "biome_grit_factory", - "biome_grit_parser", "biome_grit_syntax", - "biome_parser", "biome_rowan", - "biome_service", - "countme", - "iai", - "quickcheck", - "quickcheck_macros", "serde", "serde_json", - "tests_macros", ] [[package]] diff --git a/crates/biome_grit_formatter/Cargo.toml b/crates/biome_grit_formatter/Cargo.toml index e75a5e99f567..6d79810951d0 100644 --- a/crates/biome_grit_formatter/Cargo.toml +++ b/crates/biome_grit_formatter/Cargo.toml @@ -18,18 +18,9 @@ biome_grit_syntax = { workspace = true } biome_rowan = { workspace = true } [dev-dependencies] -biome_formatter_test = { path = "../biome_formatter_test" } -biome_grit_factory = { path = "../biome_grit_factory" } -biome_grit_parser = { path = "../biome_grit_parser" } -biome_parser = { path = "../biome_parser" } -biome_service = { path = "../biome_service" } -countme = { workspace = true, features = ["enable"] } -iai = "0.1.1" -quickcheck = { workspace = true } -quickcheck_macros = { workspace = true } -serde = { workspace = true, features = ["derive"] } -serde_json = { workspace = true } -tests_macros = { path = "../tests_macros" } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } + # cargo-workspaces metadata [package.metadata.workspaces] independent = true diff --git a/crates/biome_grit_formatter/src/comments.rs b/crates/biome_grit_formatter/src/comments.rs index 3980222dc2d6..6e72b0684c94 100644 --- a/crates/biome_grit_formatter/src/comments.rs +++ b/crates/biome_grit_formatter/src/comments.rs @@ -22,7 +22,7 @@ impl CommentStyle for GritCommentStyle { } fn get_comment_kind( - comment: &biome_rowan::SyntaxTriviaPieceComments, + _comment: &biome_rowan::SyntaxTriviaPieceComments, ) -> biome_formatter::comments::CommentKind { todo!() } diff --git a/crates/biome_grit_formatter/src/context.rs b/crates/biome_grit_formatter/src/context.rs index abc872667575..b4a8e093459a 100644 --- a/crates/biome_grit_formatter/src/context.rs +++ b/crates/biome_grit_formatter/src/context.rs @@ -6,6 +6,7 @@ use biome_formatter::{ use biome_grit_syntax::GritLanguage; use std::rc::Rc; +#[allow(dead_code)] #[derive(Debug, Clone)] pub struct GritFormatContext { comments: Rc, diff --git a/crates/biome_grit_formatter/src/lib.rs b/crates/biome_grit_formatter/src/lib.rs index 26cdfc18d89b..c935554e575c 100644 --- a/crates/biome_grit_formatter/src/lib.rs +++ b/crates/biome_grit_formatter/src/lib.rs @@ -83,6 +83,7 @@ where } } +#[allow(dead_code)] #[derive(Debug, Clone, Default)] pub struct GritFormatLanguage { options: GritFormatOptions, @@ -124,8 +125,8 @@ impl FormatLanguage for GritFormatLanguage { fn create_context( self, - root: &biome_rowan::SyntaxNode, - source_map: Option, + _root: &biome_rowan::SyntaxNode, + _source_map: Option, ) -> Self::Context { todo!() } @@ -187,6 +188,7 @@ where /// Used to convert this object into an object that can be formatted. /// /// The difference to [AsFormat] is that this trait takes ownership of `self`. +#[allow(dead_code)] pub(crate) trait IntoFormat { type Format: biome_formatter::Format; @@ -219,6 +221,7 @@ where } /// Formatting specific [Iterator] extensions +#[allow(dead_code)] pub(crate) trait FormattedIterExt { /// Converts every item to an object that knows how to format it. fn formatted(self) -> FormattedIter @@ -235,6 +238,7 @@ pub(crate) trait FormattedIterExt { impl FormattedIterExt for I where I: std::iter::Iterator {} +#[allow(dead_code)] pub(crate) struct FormattedIter where Iter: Iterator, diff --git a/crates/biome_unicode_table/src/tables.rs b/crates/biome_unicode_table/src/tables.rs index 2521478eca81..2016f15b83d8 100644 --- a/crates/biome_unicode_table/src/tables.rs +++ b/crates/biome_unicode_table/src/tables.rs @@ -71,7 +71,7 @@ pub mod derived_property { ('ࡠ', 'ࡪ'), ('ࡰ', 'ࢇ'), ('ࢉ', 'ࢎ'), - ('\u{898}', '\u{8e1}'), + ('\u{897}', '\u{8e1}'), ('\u{8e3}', '\u{963}'), ('०', '९'), ('ॱ', 'ঃ'), @@ -294,7 +294,7 @@ pub mod derived_property { ('ᰀ', '\u{1c37}'), ('᱀', '᱉'), ('ᱍ', 'ᱽ'), - ('ᲀ', 'ᲈ'), + ('ᲀ', '\u{1c8a}'), ('Ა', 'Ჺ'), ('Ჽ', 'Ჿ'), ('\u{1cd0}', '\u{1cd2}'), @@ -378,10 +378,10 @@ pub mod derived_property { ('ꙿ', '\u{a6f1}'), ('ꜗ', 'ꜟ'), ('Ꜣ', 'ꞈ'), - ('Ꞌ', 'ꟊ'), + ('Ꞌ', '\u{a7cd}'), ('Ꟑ', 'ꟑ'), ('ꟓ', 'ꟓ'), - ('ꟕ', 'ꟙ'), + ('ꟕ', '\u{a7dc}'), ('ꟲ', 'ꠧ'), ('\u{a82c}', '\u{a82c}'), ('ꡀ', 'ꡳ'), @@ -479,6 +479,7 @@ pub mod derived_property { ('𐖣', '𐖱'), ('𐖳', '𐖹'), ('𐖻', '𐖼'), + ('\u{105c0}', '\u{105f3}'), ('𐘀', '𐜶'), ('𐝀', '𐝕'), ('𐝠', '𐝧'), @@ -519,10 +520,14 @@ pub mod derived_property { ('𐳀', '𐳲'), ('𐴀', '\u{10d27}'), ('𐴰', '𐴹'), + ('\u{10d40}', '\u{10d65}'), + ('\u{10d69}', '\u{10d6d}'), + ('\u{10d6f}', '\u{10d85}'), ('𐺀', '𐺩'), ('\u{10eab}', '\u{10eac}'), ('𐺰', '𐺱'), - ('\u{10efd}', '𐼜'), + ('\u{10ec2}', '\u{10ec4}'), + ('\u{10efc}', '𐼜'), ('𐼧', '𐼧'), ('𐼰', '\u{10f50}'), ('𐽰', '\u{10f85}'), @@ -568,6 +573,16 @@ pub mod derived_property { ('𑍝', '𑍣'), ('\u{11366}', '\u{1136c}'), ('\u{11370}', '\u{11374}'), + ('\u{11380}', '\u{11389}'), + ('\u{1138b}', '\u{1138b}'), + ('\u{1138e}', '\u{1138e}'), + ('\u{11390}', '\u{113b5}'), + ('\u{113b7}', '\u{113c0}'), + ('\u{113c2}', '\u{113c2}'), + ('\u{113c5}', '\u{113c5}'), + ('\u{113c7}', '\u{113ca}'), + ('\u{113cc}', '\u{113d3}'), + ('\u{113e1}', '\u{113e2}'), ('𑐀', '𑑊'), ('𑑐', '𑑙'), ('\u{1145e}', '𑑡'), @@ -582,6 +597,7 @@ pub mod derived_property { ('𑙐', '𑙙'), ('𑚀', '𑚸'), ('𑛀', '𑛉'), + ('\u{116d0}', '\u{116e3}'), ('𑜀', '𑜚'), ('\u{1171d}', '\u{1172b}'), ('𑜰', '𑜹'), @@ -605,6 +621,8 @@ pub mod derived_property { ('𑩐', '\u{11a99}'), ('𑪝', '𑪝'), ('𑪰', '𑫸'), + ('\u{11bc0}', '\u{11be0}'), + ('\u{11bf0}', '\u{11bf9}'), ('𑰀', '𑰈'), ('𑰊', '\u{11c36}'), ('\u{11c38}', '𑱀'), @@ -629,7 +647,7 @@ pub mod derived_property { ('\u{11f00}', '𑼐'), ('𑼒', '\u{11f3a}'), ('𑼾', '\u{11f42}'), - ('𑽐', '𑽙'), + ('𑽐', '\u{11f5a}'), ('𑾰', '𑾰'), ('𒀀', '𒎙'), ('𒐀', '𒑮'), @@ -637,7 +655,9 @@ pub mod derived_property { ('𒾐', '𒿰'), ('𓀀', '𓐯'), ('\u{13440}', '\u{13455}'), + ('\u{13460}', '\u{143fa}'), ('𔐀', '𔙆'), + ('\u{16100}', '\u{16139}'), ('𖠀', '𖨸'), ('𖩀', '𖩞'), ('𖩠', '𖩩'), @@ -650,6 +670,8 @@ pub mod derived_property { ('𖭐', '𖭙'), ('𖭣', '𖭷'), ('𖭽', '𖮏'), + ('\u{16d40}', '\u{16d6c}'), + ('\u{16d70}', '\u{16d79}'), ('𖹀', '𖹿'), ('𖼀', '𖽊'), ('\u{16f4f}', '𖾇'), @@ -659,7 +681,7 @@ pub mod derived_property { ('𖿰', '𖿱'), ('𗀀', '𘟷'), ('𘠀', '𘳕'), - ('𘴀', '𘴈'), + ('\u{18cff}', '𘴈'), ('𚿰', '𚿳'), ('𚿵', '𚿻'), ('𚿽', '𚿾'), @@ -674,6 +696,7 @@ pub mod derived_property { ('𛲀', '𛲈'), ('𛲐', '𛲙'), ('\u{1bc9d}', '\u{1bc9e}'), + ('\u{1ccf0}', '\u{1ccf9}'), ('\u{1cf00}', '\u{1cf2d}'), ('\u{1cf30}', '\u{1cf46}'), ('\u{1d165}', '\u{1d169}'), @@ -735,6 +758,7 @@ pub mod derived_property { ('𞊐', '\u{1e2ae}'), ('𞋀', '𞋹'), ('𞓐', '𞓹'), + ('\u{1e5d0}', '\u{1e5fa}'), ('𞟠', '𞟦'), ('𞟨', '𞟫'), ('𞟭', '𞟮'), @@ -1016,7 +1040,7 @@ pub mod derived_property { ('ᰀ', 'ᰣ'), ('ᱍ', 'ᱏ'), ('ᱚ', 'ᱽ'), - ('ᲀ', 'ᲈ'), + ('ᲀ', '\u{1c8a}'), ('Ა', 'Ჺ'), ('Ჽ', 'Ჿ'), ('ᳩ', 'ᳬ'), @@ -1099,10 +1123,10 @@ pub mod derived_property { ('ꚠ', 'ꛯ'), ('ꜗ', 'ꜟ'), ('Ꜣ', 'ꞈ'), - ('Ꞌ', 'ꟊ'), + ('Ꞌ', '\u{a7cd}'), ('Ꟑ', 'ꟑ'), ('ꟓ', 'ꟓ'), - ('ꟕ', 'ꟙ'), + ('ꟕ', '\u{a7dc}'), ('ꟲ', 'ꠁ'), ('ꠃ', 'ꠅ'), ('ꠇ', 'ꠊ'), @@ -1200,6 +1224,7 @@ pub mod derived_property { ('𐖣', '𐖱'), ('𐖳', '𐖹'), ('𐖻', '𐖼'), + ('\u{105c0}', '\u{105f3}'), ('𐘀', '𐜶'), ('𐝀', '𐝕'), ('𐝠', '𐝧'), @@ -1236,8 +1261,11 @@ pub mod derived_property { ('𐲀', '𐲲'), ('𐳀', '𐳲'), ('𐴀', '𐴣'), + ('\u{10d4a}', '\u{10d65}'), + ('\u{10d6f}', '\u{10d85}'), ('𐺀', '𐺩'), ('𐺰', '𐺱'), + ('\u{10ec2}', '\u{10ec4}'), ('𐼀', '𐼜'), ('𐼧', '𐼧'), ('𐼰', '𐽅'), @@ -1276,6 +1304,13 @@ pub mod derived_property { ('𑌽', '𑌽'), ('𑍐', '𑍐'), ('𑍝', '𑍡'), + ('\u{11380}', '\u{11389}'), + ('\u{1138b}', '\u{1138b}'), + ('\u{1138e}', '\u{1138e}'), + ('\u{11390}', '\u{113b5}'), + ('\u{113b7}', '\u{113b7}'), + ('\u{113d1}', '\u{113d1}'), + ('\u{113d3}', '\u{113d3}'), ('𑐀', '𑐴'), ('𑑇', '𑑊'), ('𑑟', '𑑡'), @@ -1310,6 +1345,7 @@ pub mod derived_property { ('𑩜', '𑪉'), ('𑪝', '𑪝'), ('𑪰', '𑫸'), + ('\u{11bc0}', '\u{11be0}'), ('𑰀', '𑰈'), ('𑰊', '𑰮'), ('𑱀', '𑱀'), @@ -1333,7 +1369,9 @@ pub mod derived_property { ('𒾐', '𒿰'), ('𓀀', '𓐯'), ('𓑁', '𓑆'), + ('\u{13460}', '\u{143fa}'), ('𔐀', '𔙆'), + ('\u{16100}', '\u{1611d}'), ('𖠀', '𖨸'), ('𖩀', '𖩞'), ('𖩰', '𖪾'), @@ -1342,6 +1380,7 @@ pub mod derived_property { ('𖭀', '𖭃'), ('𖭣', '𖭷'), ('𖭽', '𖮏'), + ('\u{16d40}', '\u{16d6c}'), ('𖹀', '𖹿'), ('𖼀', '𖽊'), ('𖽐', '𖽐'), @@ -1350,7 +1389,7 @@ pub mod derived_property { ('𖿣', '𖿣'), ('𗀀', '𘟷'), ('𘠀', '𘳕'), - ('𘴀', '𘴈'), + ('\u{18cff}', '𘴈'), ('𚿰', '𚿳'), ('𚿵', '𚿻'), ('𚿽', '𚿾'), @@ -1403,6 +1442,8 @@ pub mod derived_property { ('𞊐', '𞊭'), ('𞋀', '𞋫'), ('𞓐', '𞓫'), + ('\u{1e5d0}', '\u{1e5ed}'), + ('\u{1e5f0}', '\u{1e5f0}'), ('𞟠', '𞟦'), ('𞟨', '𞟫'), ('𞟭', '𞟮'),