From 304c25d2d3df3c1c5001a93af7d0aba53a235012 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Thu, 4 Apr 2024 07:18:30 +0530 Subject: [PATCH] Refactor logic in swapping --- .../formatter/core/FormatterUtils.java | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormatterUtils.java b/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormatterUtils.java index c2dffae09273..e1f02c630792 100644 --- a/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormatterUtils.java +++ b/misc/formatter/modules/formatter-core/src/main/java/org/ballerinalang/formatter/core/FormatterUtils.java @@ -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; @@ -106,42 +105,43 @@ static void swapLeadingMinutiae(ImportDeclarationNode firstImportNode, List 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) {