Skip to content

Commit

Permalink
Refactor logic in swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
poorna2152 committed Apr 4, 2024
1 parent 6e29d81 commit 304c25d
Showing 1 changed file with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.ballerina.tools.text.LineRange;
import org.apache.commons.lang3.builder.CompareToBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -106,42 +105,43 @@ static void swapLeadingMinutiae(ImportDeclarationNode firstImportNode, List<Impo
if (prevFirstImportIndex == 0) {
return;
}

// remove comments from the previous first import
ImportDeclarationNode prevFirstImportNode = importNodes.get(prevFirstImportIndex);
MinutiaeList prevLeadingMinutiae = prevFirstImportNode.leadingMinutiae();
List<Minutiae> leadingMinutiae = new ArrayList<>();
Minutiae prevFirstMinutiae = prevLeadingMinutiae.get(0);
MinutiaeList prevFirstLeadingMinutiae = prevFirstImportNode.leadingMinutiae();
MinutiaeList newPrevFirstLeadingMinutiae = NodeFactory.createEmptyMinutiaeList();
Minutiae prevFirstMinutiae = prevFirstLeadingMinutiae.get(0);
if (prevFirstMinutiae.kind() != SyntaxKind.COMMENT_MINUTIAE) {
leadingMinutiae.add(prevFirstMinutiae);
prevLeadingMinutiae = prevLeadingMinutiae.remove(0);
// if the prevFirstImport now is the first of a group of imports, handle the added leading newline
newPrevFirstLeadingMinutiae = newPrevFirstLeadingMinutiae.add(prevFirstMinutiae);
prevFirstLeadingMinutiae = prevFirstLeadingMinutiae.remove(0);
}

Token prevFirstImportToken = prevFirstImportNode.importKeyword();
Token modifiedPrevFirstImportToken =
prevFirstImportToken.modify(NodeFactory.createMinutiaeList(leadingMinutiae),
prevFirstImportToken.trailingMinutiae());
importNodes.set(prevFirstImportIndex,
prevFirstImportNode.modify().withImportKeyword(modifiedPrevFirstImportToken).apply());
modifyImportDeclLeadingMinutiae(prevFirstImportNode, newPrevFirstLeadingMinutiae));

// add leading comments from the previous first import
if (!hasEmptyLineAtEnd(prevLeadingMinutiae)) {
prevLeadingMinutiae =
prevLeadingMinutiae.add(NodeFactory.createEndOfLineMinutiae(System.lineSeparator()));
if (!hasEmptyLineAtEnd(prevFirstLeadingMinutiae)) {
// adds a new line to after prevFirstImport's leading minutiae if not present
prevFirstLeadingMinutiae =
prevFirstLeadingMinutiae.add(NodeFactory.createEndOfLineMinutiae(System.lineSeparator()));
}
ImportDeclarationNode sortedFirstImportNode = importNodes.get(0);
Token sortedFirstImportToken = sortedFirstImportNode.importKeyword();
MinutiaeList sortedLeadingMinutiae = sortedFirstImportToken.leadingMinutiae();
ImportDeclarationNode newFirstImportNode = importNodes.get(0);
MinutiaeList sortedLeadingMinutiae = newFirstImportNode.importKeyword().leadingMinutiae();
for (int i = 0; i < sortedLeadingMinutiae.size(); i++) {
Minutiae minutiae = sortedLeadingMinutiae.get(i);
if (i == 0 && minutiae.kind() == SyntaxKind.END_OF_LINE_MINUTIAE) {
// since we added a new line after prevFirstImport's leading minutiae we can skip th newline here
continue;
}
prevLeadingMinutiae = prevLeadingMinutiae.add(minutiae);
prevFirstLeadingMinutiae = prevFirstLeadingMinutiae.add(minutiae);
}
Token modifiedSortedFirstImportToken =
sortedFirstImportToken.modify(prevLeadingMinutiae, sortedFirstImportToken.trailingMinutiae());
importNodes.set(0,
sortedFirstImportNode.modify().withImportKeyword(modifiedSortedFirstImportToken).apply());
importNodes.set(0, modifyImportDeclLeadingMinutiae(newFirstImportNode, prevFirstLeadingMinutiae));
}

private static ImportDeclarationNode modifyImportDeclLeadingMinutiae(ImportDeclarationNode importDecl,
MinutiaeList leadingMinutiae) {
Token importToken = importDecl.importKeyword();
Token modifiedImportToken = importToken.modify(leadingMinutiae, importToken.trailingMinutiae());
return importDecl.modify().withImportKeyword(modifiedImportToken).apply();
}

private static boolean doesImportMatch(String orgName, String moduleName, ImportDeclarationNode importDeclNode) {
Expand Down

0 comments on commit 304c25d

Please sign in to comment.