Skip to content

Commit

Permalink
feat(ast): add Comment::real_span (#5764)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Sep 13, 2024
1 parent 5bace1a commit e8bf30a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
4 changes: 4 additions & 0 deletions crates/oxc_ast/src/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ impl Comment {
Self { kind, span }
}

pub fn real_span(&self) -> Span {
Span::new(self.real_span_start(), self.real_span_end())
}

pub fn real_span_end(&self) -> u32 {
match self.kind {
CommentKind::SingleLine => self.span.end,
Expand Down
10 changes: 4 additions & 6 deletions crates/oxc_parser/examples/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ fn main() -> Result<(), String> {
println!("{}", serde_json::to_string_pretty(&ret.program).unwrap());

println!("Comments:");
let comments = ret
.trivias
.comments()
.map(|comment| comment.span.source_text(&source_text))
.collect::<Vec<_>>();
println!("{comments:?}");
for comment in ret.trivias.comments() {
let s = comment.real_span().source_text(&source_text);
println!("{s}");
}

if ret.errors.is_empty() {
println!("Parsed Successfully.");
Expand Down
18 changes: 10 additions & 8 deletions crates/oxc_prettier/src/comments/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
mod print;

use bitflags::bitflags;
use oxc_ast::CommentKind;

#[derive(Debug, Clone, Copy)]
pub struct Comment {
/// Span start including `//` and `/*`
pub start: u32,
/// Span end including `*?`
pub end: u32,
pub is_block: bool,
pub has_line_suffix: bool,
}

impl Comment {
pub fn new(start: u32, end: u32, kind: CommentKind) -> Self {
// The comment span is for the comment value
// -2 for `//` and `/*`
let start = start - 2;
// +2 for `/*`
let end = if kind.is_multi_line() { end + 2 } else { end };
Self { start, end, is_block: kind.is_multi_line(), has_line_suffix: false }
pub fn new(comment: oxc_ast::Comment) -> Self {
let span = comment.real_span();
Self {
start: span.start,
end: span.end,
is_block: comment.kind.is_multi_line(),
has_line_suffix: false,
}
}

pub fn with_line_suffix(mut self, yes: bool) -> Self {
Expand Down
22 changes: 6 additions & 16 deletions crates/oxc_prettier/src/comments/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ impl<'a> Prettier<'a> {
let mut peekable_trivias = self.trivias.clone();

while let Some(comment) = peekable_trivias.peek().copied() {
let start = comment.span.start;
let end = comment.span.end;
let mut should_break = true;
let comment = Comment::new(start, end, comment.kind);
let comment = Comment::new(comment);

if range.end < comment.start
&& self.source_text[range.end as usize..comment.start as usize]
Expand Down Expand Up @@ -71,11 +69,9 @@ impl<'a> Prettier<'a> {
pub(crate) fn print_leading_comments(&mut self, range: Span) -> Option<Doc<'a>> {
let mut parts = self.vec();
while let Some(comment) = self.trivias.peek().copied() {
let start = comment.span.start;
let end = comment.span.end;
let comment = Comment::new(start, end, comment.kind);
let comment = Comment::new(comment);
// Comment before the span
if end <= range.start {
if comment.end <= range.start {
self.trivias.next();
self.print_leading_comment(&mut parts, comment);
} else {
Expand Down Expand Up @@ -120,9 +116,7 @@ impl<'a> Prettier<'a> {
let mut parts = self.vec();
let mut previous_comment: Option<Comment> = None;
while let Some(comment) = self.trivias.peek().copied() {
let start = comment.span.start;
let end = comment.span.end;
let comment = Comment::new(start, end, comment.kind);
let comment = Comment::new(comment);
// Trailing comment if there is nothing in between.
if range.end < comment.start
&& self.source_text[range.end as usize..comment.start as usize]
Expand Down Expand Up @@ -187,9 +181,7 @@ impl<'a> Prettier<'a> {
pub(crate) fn print_inner_comment(&mut self, range: Span) -> Vec<'a, Doc<'a>> {
let mut parts = self.vec();
while let Some(comment) = self.trivias.peek().copied() {
let start = comment.span.start;
let end = comment.span.end;
let comment = Comment::new(start, end, comment.kind);
let comment = Comment::new(comment);
// Comment within the span
if comment.start >= range.start && comment.end <= range.end {
self.trivias.next();
Expand All @@ -210,9 +202,7 @@ impl<'a> Prettier<'a> {
) -> Option<Doc<'a>> {
let mut parts = vec![];
while let Some(comment) = self.trivias.peek().copied() {
let start = comment.span.start;
let end = comment.span.end;
let comment = Comment::new(start, end, comment.kind);
let comment = Comment::new(comment);
// Comment within the span
if comment.end <= range.end {
parts.push(self.print_comment(comment));
Expand Down

0 comments on commit e8bf30a

Please sign in to comment.