Skip to content

Commit

Permalink
Merge pull request #133 from dcSpark/fix-group-comments
Browse files Browse the repository at this point in the history
Fix group comments
  • Loading branch information
anweiss authored Sep 26, 2022
2 parents fb661a4 + 8ca5917 commit 9258948
Show file tree
Hide file tree
Showing 3 changed files with 554 additions and 23 deletions.
30 changes: 29 additions & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,9 +638,11 @@ pub struct Type<'a> {
}

impl<'a> Type<'a> {
/// take all the comments after a type
/// this is useful if the type is consumed to build another type object
#[cfg(feature = "ast-comments")]
#[doc(hidden)]
pub fn comments_after_type(&mut self) -> Option<Comments<'a>> {
pub fn take_comments_after_type(&mut self) -> Option<Comments<'a>> {
if let Some(TypeChoice {
type1: Type1 {
comments_after_type,
Expand All @@ -660,6 +662,32 @@ impl<'a> Type<'a> {
}
}

impl<'a> Type<'a> {
/// leave the first comment after a type as part of its parent
/// and subsequent comments are considered after the type
#[cfg(feature = "ast-comments")]
#[doc(hidden)]
pub fn split_comments_after_type(&mut self) -> Option<Comments<'a>> {
if let Some(TypeChoice {
type1: Type1 {
comments_after_type,
..
},
..
}) = self.type_choices.last_mut()
{
return match comments_after_type.as_mut() {
Some(comments) if comments.any_non_newline() && comments.0.len() > 1 => {
Some(Comments(comments.0.drain(1..).collect()))
}
_ => None,
};
}

None
}
}

/// Type choice
#[cfg_attr(target_arch = "wasm32", derive(Serialize))]
#[derive(Debug, Clone, PartialEq)]
Expand Down
56 changes: 37 additions & 19 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ impl<'a> Parser<'a> {
);

#[cfg(feature = "ast-comments")]
let comments_after_rule = if let Some(comments) = t.comments_after_type() {
let comments_after_rule = if let Some(comments) = t.split_comments_after_type() {
Some(comments)
} else {
self.collect_comments()?
Expand Down Expand Up @@ -795,7 +795,7 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "ast-comments")]
let comments_after_rule = if let Some(comments) = t.comments_after_type() {
let comments_after_rule = if let Some(comments) = t.split_comments_after_type() {
Some(comments)
} else {
self.collect_comments()?
Expand Down Expand Up @@ -1406,13 +1406,24 @@ impl<'a> Parser<'a> {
#[cfg(not(feature = "ast-comments"))]
let group = self.parse_group()?;

// if the group starts with a multi-line comment,
// we take the first comment inside the 1st group to be comments_before_group
#[cfg(feature = "ast-comments")]
let comments_before_group = if let Some(GroupChoice {
comments_before_grpchoice,
..
}) = group.group_choices.first_mut()
{
comments_before_grpchoice.take()
comments_before_grpchoice
.as_mut()
.and_then(|comments| {
if comments.0.len() > 1 {
Some(comments.0.remove(0))
} else {
None
}
})
.map(|comment| Comments(vec![comment]))
} else {
None
};
Expand Down Expand Up @@ -1452,17 +1463,24 @@ impl<'a> Parser<'a> {
#[cfg(not(feature = "ast-comments"))]
let group = self.parse_group()?;

// if the group starts with a multi-line comment,
// we take the first comment inside the 1st group to be comments_before_group
#[cfg(feature = "ast-comments")]
let comments_before_group = if let Some(GroupChoice {
comments_before_grpchoice,
..
}) = group.group_choices.first_mut()
{
if comments_before_grpchoice.is_some() {
comments_before_grpchoice.take()
} else {
None
}
comments_before_grpchoice
.as_mut()
.and_then(|comments| {
if comments.0.len() > 1 {
Some(comments.0.remove(0))
} else {
None
}
})
.map(|comment| Comments(vec![comment]))
} else {
None
};
Expand Down Expand Up @@ -1875,14 +1893,14 @@ impl<'a> Parser<'a> {
{
grpchoice.span.0 = self.lexer_position.range.0;
}
};

#[cfg(feature = "ast-comments")]
{
grpchoice.comments_before_grpchoice = self.collect_comments()?;
}
#[cfg(not(feature = "ast-comments"))]
self.advance_newline()?;
#[cfg(feature = "ast-comments")]
{
grpchoice.comments_before_grpchoice = self.collect_comments()?;
}
#[cfg(not(feature = "ast-comments"))]
self.advance_newline()?;
};

// TODO: The logic in this while loop is quite messy. Need to figure out a
// better way to advance the token when parsing the entries in a group
Expand Down Expand Up @@ -2068,7 +2086,7 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "ast-comments")]
let trailing_comments = entry_type.comments_after_type();
let trailing_comments = entry_type.take_comments_after_type();

#[cfg(feature = "ast-span")]
if let Some((name, generic_args, _)) = entry_type.groupname_entry() {
Expand Down Expand Up @@ -2137,7 +2155,7 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "ast-comments")]
let trailing_comments = if let Some(comments) = entry_type.comments_after_type() {
let trailing_comments = if let Some(comments) = entry_type.split_comments_after_type() {
Some(comments)
} else {
comments_after_type_or_group
Expand Down Expand Up @@ -2221,7 +2239,7 @@ impl<'a> Parser<'a> {
let entry_type = self.parse_type(None)?;

#[cfg(feature = "ast-comments")]
let trailing_comments = entry_type.comments_after_type();
let trailing_comments = entry_type.split_comments_after_type();

#[cfg(feature = "ast-span")]
{
Expand Down Expand Up @@ -2293,7 +2311,7 @@ impl<'a> Parser<'a> {
}

#[cfg(feature = "ast-comments")]
let trailing_comments = if let Some(comments) = entry_type.comments_after_type() {
let trailing_comments = if let Some(comments) = entry_type.take_comments_after_type() {
Some(comments)
} else {
self.collect_comments()?
Expand Down
Loading

0 comments on commit 9258948

Please sign in to comment.