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
Show file tree
Hide file tree
Changes from 3 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
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.

14 changes: 7 additions & 7 deletions crates/biome_formatter/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ where
</details>


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`
branberry marked this conversation as resolved.
Show resolved Hide resolved

### `HtmlCommentStyle`

Expand Down Expand Up @@ -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<CssComments>,
source_map: Option<TransformSourceMap>,
comments: Rc<HtmlComments>,
source_map: Option<TransformSourceMap>,
}

impl HtmlFormatContext {
pub fn new(comments: CssComments) -> Self {
pub fn new(comments: HtmlComments) -> Self {
Self {
comments: Rc::new(comments),
source_map: None,
Expand Down
10 changes: 5 additions & 5 deletions crates/biome_grit_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
28 changes: 28 additions & 0 deletions crates/biome_grit_formatter/src/comments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use biome_formatter::comments::{CommentStyle, Comments};
use biome_grit_syntax::GritLanguage;

pub type GritComments = Comments<GritLanguage>;

#[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<Self::Language>,
) -> biome_formatter::comments::CommentKind {
todo!()
}

fn place_comment(
&self,
comment: biome_formatter::comments::DecoratedComment<Self::Language>,
) -> biome_formatter::comments::CommentPlacement<Self::Language> {
biome_formatter::comments::CommentPlacement::Default(comment)
}
}
147 changes: 147 additions & 0 deletions crates/biome_grit_formatter/src/context.rs
Original file line number Diff line number Diff line change
@@ -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<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!()
}
}
30 changes: 30 additions & 0 deletions crates/biome_grit_formatter/src/cst.rs
Original file line number Diff line number Diff line change
@@ -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<GritSyntaxNode> 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<GritFormatContext> for FormatGritSyntaxNode {
type Format<'a> = FormatRefWithRule<'a, HtmlSyntaxNode, FormatHtmlSyntaxNode>;

fn format(&self) -> Self::Format<'_> {
FormatRefWithRule::new(self, FormatHtmlSyntaxNode)
}
}

impl IntoFormat<HtmlFormatContext> for FormatHtmlSyntaxNode {
type Format = FormatOwnedWithRule<HtmlSyntaxNode, FormatHtmlSyntaxNode>;

fn into_format(self) -> Self::Format {
FormatOwnedWithRule::new(self, FormatHtmlSyntaxNode)
}
}
Loading
Loading