Skip to content

Commit

Permalink
Merge pull request #402 from KavinduZoysa/comment-node
Browse files Browse the repository at this point in the history
Generate comment node from Ballerina comment node
  • Loading branch information
hasithaa authored Sep 18, 2024
2 parents 6422f6c + 6c7560b commit 0fcb841
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.ballerina.compiler.syntax.tree.BreakStatementNode;
import io.ballerina.compiler.syntax.tree.CheckExpressionNode;
import io.ballerina.compiler.syntax.tree.ClientResourceAccessActionNode;
import io.ballerina.compiler.syntax.tree.CommentNode;
import io.ballerina.compiler.syntax.tree.CompoundAssignmentStatementNode;
import io.ballerina.compiler.syntax.tree.ContinueStatementNode;
import io.ballerina.compiler.syntax.tree.DoStatementNode;
Expand All @@ -58,7 +59,6 @@
import io.ballerina.compiler.syntax.tree.MatchClauseNode;
import io.ballerina.compiler.syntax.tree.MatchGuardNode;
import io.ballerina.compiler.syntax.tree.MatchStatementNode;
import io.ballerina.compiler.syntax.tree.Minutiae;
import io.ballerina.compiler.syntax.tree.ModuleVariableDeclarationNode;
import io.ballerina.compiler.syntax.tree.NameReferenceNode;
import io.ballerina.compiler.syntax.tree.NewExpressionNode;
Expand All @@ -78,7 +78,6 @@
import io.ballerina.compiler.syntax.tree.SeparatedNodeList;
import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode;
import io.ballerina.compiler.syntax.tree.StartActionNode;
import io.ballerina.compiler.syntax.tree.StatementNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.Token;
import io.ballerina.compiler.syntax.tree.TransactionStatementNode;
Expand Down Expand Up @@ -175,11 +174,25 @@ public void visit(ObjectFieldNode objectFieldNode) {

@Override
public void visit(FunctionBodyBlockNode functionBodyBlockNode) {
for (StatementNode statementNode : functionBodyBlockNode.statements()) {
genCommentNode(statementNode);
statementNode.accept(this);
for (Node statementOrComment : functionBodyBlockNode.statementsWithComments()) {
statementOrComment.accept(this);
}
genCommentNode(functionBodyBlockNode.closeBraceToken());
}

@Override
public void visit(CommentNode commentNode) {
Node node = commentNode.getCommentAttachedNode();
LinePosition startPos = textDocument.linePositionFrom(node.textRangeWithMinutiae().startOffset());
int offset = 0;
if (!(node instanceof Token)) {
offset = node.textRangeWithMinutiae().startOffset();
}
LinePosition endPos =
textDocument.linePositionFrom(commentNode.getLastMinutiae().textRange().endOffset() + offset);
LineRange commentRange = LineRange.from(node.lineRange().fileName(), startPos, endPos);
CommentMetadata commentMetadata = new CommentMetadata(String.join(System.lineSeparator(),
commentNode.getCommentLines()), commentRange);
genCommentNode(commentMetadata);
}

@Override
Expand Down Expand Up @@ -815,56 +828,11 @@ private void handleDefaultNodeWithBlock(BlockStatementNode bodyNode) {
endNode(bodyNode);
}

private void analyzeBlock(BlockStatementNode blockStatement, Branch.Builder branchBuilder) {
for (StatementNode statement : blockStatement.statements()) {
genCommentNode(statement, branchBuilder);
statement.accept(this);
branchBuilder.node(buildNode());
}
genCommentNode(blockStatement.closeBraceToken(), branchBuilder);
}

private Optional<CommentMetadata> getComment(Node node) {
List<String> commentLines = new ArrayList<>();
Minutiae lastMinutiae = null;
for (Minutiae minutiae : node.leadingMinutiae()) {
String[] splits = minutiae.text().split("// ");
if (splits.length >= 2) {
commentLines.add(splits[1]);
lastMinutiae = minutiae;
} else if (splits.length == 1 && splits[0].contains("//")) {
commentLines.add("");
lastMinutiae = minutiae;
}
}
if (commentLines.isEmpty()) {
return Optional.empty();
}
LinePosition startPos = textDocument.linePositionFrom(node.textRangeWithMinutiae().startOffset());
int offset = 0;
if (!(node instanceof Token)) {
offset = node.textRangeWithMinutiae().startOffset();
}
LinePosition endPos = textDocument.linePositionFrom(lastMinutiae.textRange().endOffset() + offset);
LineRange commentRange = LineRange.from(node.lineRange().fileName(), startPos, endPos);
return Optional.of(new CommentMetadata(String.join(System.lineSeparator(), commentLines), commentRange));
}

private void genCommentNode(Node node, Branch.Builder builder) {
Optional<CommentMetadata> comment = getComment(node);
if (comment.isEmpty()) {
return;
}
genCommentNode(comment.get());
builder.node(buildNode());
}

private void genCommentNode(Node node) {
Optional<CommentMetadata> comment = getComment(node);
if (comment.isEmpty()) {
return;
private void analyzeBlock(BlockStatementNode blockStatement, Branch.Builder thenBranchBuilder) {
for (Node statementOrComment : blockStatement.statementsWithComments()) {
statementOrComment.accept(this);
thenBranchBuilder.node(buildNode());
}
genCommentNode(comment.get());
}

private void genCommentNode(CommentMetadata comment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"offset": 0
},
"end": {
"line": 3,
"line": 5,
"offset": 1
},
"source": "comment.bal",
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org.gradle.caching=true
group=org.ballerinalang
version=1.4.0-SNAPSHOT
ballerinaLangVersion=2201.10.0-20240824-130700-6ec0c001
ballerinaLangVersion=2201.11.0-20240918-131000-55b5a679

ballerinaGradlePluginVersion=2.0.1
checkStyleToolVersion=10.12.1
Expand Down

0 comments on commit 0fcb841

Please sign in to comment.