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

Fix for edge case in which comments are dropped #1924

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Changes from all 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
43 changes: 32 additions & 11 deletions core/src/main/java/org/lflang/ast/ToLf.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -117,14 +116,23 @@ public MalleableString doSwitch(EObject eObject) {
Predicate<INode> doesNotBelongToPrevious =
doesNotBelongToAncestor.and(
previous == null ? n -> true : ASTUtils.sameLine(previous).negate());
Stream<String> precedingComments =
ASTUtils.getPrecedingComments(node, doesNotBelongToPrevious).map(String::strip);
Collection<String> allComments = new ArrayList<>();
precedingComments.forEachOrdered(allComments::add);
getContainedComments(node).stream()
.filter(doesNotBelongToAncestor)
.map(INode::getText)
.forEachOrdered(allComments::add);
var allComments = new ArrayList<String>();
if (eObject.eContents().isEmpty() && !(eObject instanceof Code)) {
getContainedComments(node).stream()
.filter(doesNotBelongToAncestor)
.filter(doesNotBelongToPrevious)
.map(INode::getText)
.forEach(allComments::add);
} else {
Stream<String> precedingComments =
ASTUtils.getPrecedingComments(node, doesNotBelongToPrevious.and(doesNotBelongToAncestor))
.map(String::strip);
precedingComments.forEachOrdered(allComments::add);
getContainedCodeComments(node).stream()
.filter(doesNotBelongToAncestor)
.map(INode::getText)
.forEach(allComments::add);
}
allComments.addAll(followingComments);
if (allComments.stream().anyMatch(s -> KEEP_FORMAT_COMMENT.matcher(s).matches())) {
return MalleableString.anyOf(StringUtil.trimCodeBlock(node.getText(), 0))
Expand All @@ -140,8 +148,9 @@ static Set<INode> getAncestorComments(INode node) {
for (ICompositeNode ancestor = node.getParent();
ancestor != null;
ancestor = ancestor.getParent()) {
ancestorComments.addAll(getContainedComments(ancestor));
ancestorComments.addAll(getContainedCodeComments(ancestor));
ASTUtils.getPrecedingCommentNodes(ancestor, u -> true).forEachOrdered(ancestorComments::add);
ancestorComments.addAll(getContainedCodeComments(ancestor));
}
return ancestorComments;
}
Expand Down Expand Up @@ -192,7 +201,7 @@ private static Stream<String> getFollowingComments(
* Return comments contained by {@code node} that logically belong to this node (and not to any of
* its children).
*/
private static List<INode> getContainedComments(INode node) {
private static List<INode> getContainedCodeComments(INode node) {
ArrayList<INode> ret = new ArrayList<>();
boolean inSemanticallyInsignificantLeadingRubbish = true;
for (INode child : node.getAsTreeIterable()) {
Expand All @@ -212,6 +221,18 @@ private static List<INode> getContainedComments(INode node) {
return ret;
}

/**
* Return all comments that are part of {@code node}, regardless of where they appear relative to
* the main content of the node.
*/
private static List<INode> getContainedComments(INode node) {
var ret = new ArrayList<INode>();
for (INode child : node.getAsTreeIterable()) {
if (ASTUtils.isComment(child)) ret.add(child);
}
return ret;
}

@Override
public MalleableString caseCodeExpr(CodeExpr object) {
return caseCode(object.getCode());
Expand Down