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(format/grit): grit formatter initial configuration #3885

Merged
merged 16 commits into from
Sep 16, 2024
Merged
Prev Previous commit
Next Next commit
Additional gritql format language and fix up prelude
  • Loading branch information
branberry committed Sep 15, 2024
commit c3b0d7f6614d11f860e031308ac67e2126d03cd8
2 changes: 1 addition & 1 deletion crates/biome_formatter/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl IntoFormat<HtmlFormatContext> 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;
Expand Down
85 changes: 43 additions & 42 deletions crates/biome_grit_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,49 @@ use biome_formatter::{
use biome_grit_syntax::GritLanguage;
use std::rc::Rc;

#[derive(Debug)]
pub struct GritFormatContext {
comments: Rc<GritComments>,
source_map: Option<TransformSourceMap>,
}

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<TransformSourceMap>) -> 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<Self::Language> {
todo!()
}
}

pub struct GritFormatOptions {
indent_style: IndentStyle,
indent_width: IndentWidth,
Expand Down Expand Up @@ -103,45 +146,3 @@ impl FormatOptions for GritFormatOptions {
todo!()
}
}

pub struct GritFormatContext {
comments: Rc<GritComments>,
source_map: Option<TransformSourceMap>,
}

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<TransformSourceMap>) -> 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<Self::Language> {
todo!()
}
}
12 changes: 6 additions & 6 deletions crates/biome_grit_formatter/src/cst.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -14,17 +14,17 @@ impl FormatRule<GritSyntaxNode> for FormatGritSyntaxNode {
}

impl AsFormat<GritFormatContext> 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<HtmlFormatContext> for FormatHtmlSyntaxNode {
type Format = FormatOwnedWithRule<HtmlSyntaxNode, FormatHtmlSyntaxNode>;
impl IntoFormat<GritFormatContext> for FormatGritSyntaxNode {
type Format = FormatOwnedWithRule<GritSyntaxNode, FormatGritSyntaxNode>;

fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(self, FormatHtmlSyntaxNode)
FormatOwnedWithRule::new(self, FormatGritSyntaxNode)
}
}
58 changes: 58 additions & 0 deletions crates/biome_grit_formatter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Self::SyntaxLanguage>,
) -> Option<(
biome_rowan::SyntaxNode<Self::SyntaxLanguage>,
biome_formatter::TransformSourceMap,
)> {
None
}

fn is_range_formatting_node(
&self,
_node: &biome_rowan::SyntaxNode<Self::SyntaxLanguage>,
) -> bool {
true
}

fn options(&self) -> &<Self::Context as biome_formatter::FormatContext>::Options {
todo!()
}

fn create_context(
self,
root: &biome_rowan::SyntaxNode<Self::SyntaxLanguage>,
source_map: Option<biome_formatter::TransformSourceMap>,
) -> Self::Context {
todo!()
}
}

/// Used to get an object that knows how to format this object.
pub(crate) trait AsFormat<Context> {
type Format<'a>: biome_formatter::Format<Context>
Expand Down
6 changes: 1 addition & 5 deletions crates/biome_grit_formatter/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading