-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(format/grit): grit formatter initial configuration (#3885)
- Loading branch information
Showing
9 changed files
with
635 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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, | ||
}; | ||
use biome_grit_syntax::GritLanguage; | ||
use biome_rowan::TextLen; | ||
|
||
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) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct FormatGritLeadingComment; | ||
|
||
impl FormatRule<SourceComment<GritLanguage>> 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<GritLanguage>, | ||
f: &mut Formatter<Self::Context>, | ||
) -> 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()]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
use crate::comments::{FormatGritLeadingComment, GritCommentStyle, GritComments}; | ||
use biome_formatter::{ | ||
CstFormatContext, FormatContext, FormatOptions, IndentStyle, IndentWidth, LineEnding, | ||
LineWidth, QuoteStyle, TransformSourceMap, | ||
}; | ||
use biome_grit_syntax::GritLanguage; | ||
use std::rc::Rc; | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug, Clone)] | ||
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 = FormatGritLeadingComment; | ||
|
||
fn comments(&self) -> &biome_formatter::comments::Comments<Self::Language> { | ||
todo!() | ||
} | ||
} | ||
|
||
#[derive(Debug, Default, Clone, PartialEq)] | ||
|
||
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!() | ||
} | ||
} |
Oops, something went wrong.