This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_formatter): add support to format comments (#4718)
* feat: 🎸 allow comments in json file * chore: 🤖 fmt * chore: 🤖 update schema * chore: 🤖 lint * chore: 🤖 update codegen * chore: 🤖 pass all tests * chore: 🤖 basic finish parse with comments * chore: 🤖 format * chore: 🤖 lint * fix: 🐛 compile erorr * chore: 🤖 add matcher * chore: 🤖 add extra test * chore: 🤖 update snap * chore: 🤖 update snap * chore: 🤖 basic finish * fix Cargo.toml * chore: rebase * chore: rebase * just ready * feat(rome_formatter): add support to format comments * chore: add proper comments * update changelog * refactor * update playground * regression --------- Co-authored-by: IWANABETHATGUY <iwanabethatguy@qq.com>
- Loading branch information
1 parent
22bca8e
commit a65c626
Showing
32 changed files
with
878 additions
and
381 deletions.
There are no files selected for viewing
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
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
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
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,85 @@ | ||
use crate::prelude::*; | ||
use rome_diagnostics::category; | ||
use rome_formatter::comments::{ | ||
is_doc_comment, CommentKind, CommentStyle, Comments, SourceComment, | ||
}; | ||
use rome_formatter::formatter::Formatter; | ||
use rome_formatter::{write, FormatResult, FormatRule}; | ||
use rome_json_syntax::{JsonLanguage, TextLen}; | ||
use rome_rowan::SyntaxTriviaPieceComments; | ||
use rome_suppression::parse_suppression_comment; | ||
|
||
pub type JsonComments = Comments<JsonLanguage>; | ||
|
||
#[derive(Default)] | ||
pub struct FormatJsonLeadingComment; | ||
|
||
impl FormatRule<SourceComment<JsonLanguage>> for FormatJsonLeadingComment { | ||
type Context = JsonFormatContext; | ||
|
||
fn fmt( | ||
&self, | ||
comment: &SourceComment<JsonLanguage>, | ||
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()]) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Eq, PartialEq, Copy, Clone, Debug, Default)] | ||
pub struct JsonCommentStyle; | ||
|
||
impl CommentStyle for JsonCommentStyle { | ||
type Language = JsonLanguage; | ||
|
||
fn is_suppression(text: &str) -> bool { | ||
parse_suppression_comment(text) | ||
.filter_map(Result::ok) | ||
.flat_map(|suppression| suppression.categories) | ||
.any(|(key, _)| key == category!("format")) | ||
} | ||
|
||
fn get_comment_kind(comment: &SyntaxTriviaPieceComments<Self::Language>) -> CommentKind { | ||
if comment.text().starts_with("/*") { | ||
if comment.has_newline() { | ||
CommentKind::Block | ||
} else { | ||
CommentKind::InlineBlock | ||
} | ||
} else { | ||
CommentKind::Line | ||
} | ||
} | ||
} |
Oops, something went wrong.