Skip to content

Commit

Permalink
Added unit test for multi lines and its fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
subyssurendran666 committed Feb 12, 2024
1 parent 3819e0f commit 2c09616
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public class ASTRewritingStringTemplateTest extends ASTRewritingTest {

static {
TESTS_NAMES = new String[] {"test006"};
TESTS_NAMES = new String[] {"test007_c"};
}

public ASTRewritingStringTemplateTest(String name, int apiLevel) {
Expand Down Expand Up @@ -83,6 +83,7 @@ public void test001() throws Exception {
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
Block block= methodDecl.getBody();
List blockStatements = block.statements();
//add component to first fragment
assertEquals("Incorrect number of statements", 1, blockStatements.size());
{
VariableDeclarationFragment varFragment = ast.newVariableDeclarationFragment();
Expand Down Expand Up @@ -499,6 +500,7 @@ public void test005_b() throws Exception {

assertEqualString(preview, buf.toString());
}
//SINGLE LINE to MULTI LINE with Component
@SuppressWarnings({ "rawtypes", "deprecation" })
public void test006_a() throws Exception {
if (this.apiLevel != 21) {
Expand Down Expand Up @@ -548,6 +550,7 @@ public void test006_a() throws Exception {
}

@SuppressWarnings({ "rawtypes", "deprecation" })
//SINGLE LINE to MULTI LINE without Component
public void test006_b() throws Exception {
if (this.apiLevel != 21) {
System.err.println("Test "+getName()+" requires a JRE 21");
Expand Down Expand Up @@ -594,7 +597,76 @@ public void test006_b() throws Exception {

assertEqualString(preview, buf.toString());
}

@SuppressWarnings({ "rawtypes", "deprecation" })
//SINGLE LINE to MULTI LINE with Multiple Components
public void test006_c() throws Exception {
if (this.apiLevel != 21) {
System.err.println("Test "+getName()+" requires a JRE 21");
return;
}
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf= new StringBuilder();
buf.append("package test1;\n");
buf.append("public class X {\n");
buf.append(" void foo(Object o) {\n");
buf.append(" String name = \"Jay\";\n");
buf.append(" String s = STR.\"Hello \\{name} \";\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);

CompilationUnit astRoot= createAST(cu);
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
AST ast= astRoot.getAST();

assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
TypeDeclaration type= findTypeDeclaration(astRoot, "X");
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
Block block= methodDecl.getBody();
List blockStatements = block.statements();
assertEquals("Incorrect number of statements", 2, blockStatements.size());
{
VariableDeclarationStatement varStmt = (VariableDeclarationStatement) blockStatements.get(1);
assertEquals("Incorrect number of fragents", 1, varStmt.fragments().size());
VariableDeclarationFragment varFragment = (VariableDeclarationFragment) varStmt.fragments().get(0);
StringTemplateExpression templateExp = (StringTemplateExpression) varFragment.getInitializer();

StringTemplateComponent component = ast.newStringTemplateComponent();
SimpleName name = ast.newSimpleName("os");
StringFragment fragment = ast.newStringFragment();
fragment.setEscapedValue(" is your OS. ");
component.setStringFragment(fragment);
component.setEmbeddedExpression(name);

StringTemplateComponent component1 = ast.newStringTemplateComponent();
SimpleName name1 = ast.newSimpleName("xyz");
StringFragment fragment1 = ast.newStringFragment();
fragment1.setEscapedValue(" is xyz.");
component1.setStringFragment(fragment1);
component1.setEmbeddedExpression(name1);

rewrite.getListRewrite(templateExp, StringTemplateExpression.STRING_TEMPLATE_COMPONENTS).insertLast(component, null);
rewrite.getListRewrite(templateExp, StringTemplateExpression.STRING_TEMPLATE_COMPONENTS).insertLast(component1, null);
rewrite.set(templateExp, StringTemplateExpression.MULTI_LINE, Boolean.TRUE, null);
}

String preview = evaluateRewrite(cu, rewrite);

buf= new StringBuilder();
buf.append("package test1;\n");
buf.append("public class X {\n");
buf.append(" void foo(Object o) {\n");
buf.append(" String name = \"Jay\";\n");
buf.append(" String s = STR.\"\"\"\nHello \\{name} \\{os} is your OS. \\{xyz} is xyz.\n\"\"\";\n");
buf.append(" }\n");
buf.append("}\n");

assertEqualString(preview, buf.toString());
}

@SuppressWarnings({ "rawtypes", "deprecation" })
//MULTI LINE to SINGLE LINE -> with Component
public void test007_a() throws Exception {
if (this.apiLevel != 21) {
System.err.println("Test "+getName()+" requires a JRE 21");
Expand All @@ -606,7 +678,7 @@ public void test007_a() throws Exception {
buf.append("public class X {\n");
buf.append(" void foo(Object o) {\n");
buf.append(" String name = \"Jay\";\n");
buf.append(" String s = STR.\"\"\"\nHello \\{name}!\n\"\"\";\n");
buf.append(" String s = STR.\"\"\"\nHello \\{name}\n\"\"\";\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);
Expand Down Expand Up @@ -635,14 +707,14 @@ public void test007_a() throws Exception {
buf.append("public class X {\n");
buf.append(" void foo(Object o) {\n");
buf.append(" String name = \"Jay\";\n");
buf.append(" String s = STR.\"Hello \\{name}!\";\n");
buf.append(" String s = STR.\"Hello \\{name}\";\n");
buf.append(" }\n");
buf.append("}\n");

assertEqualString(preview, buf.toString());
}

@SuppressWarnings({ "rawtypes", "deprecation" })
//MULTI LINE to SINGLE LINE -> without Component
public void test007_b() throws Exception {
if (this.apiLevel != 21) {
System.err.println("Test "+getName()+" requires a JRE 21");
Expand Down Expand Up @@ -689,4 +761,72 @@ public void test007_b() throws Exception {

assertEqualString(preview, buf.toString());
}

@SuppressWarnings({ "rawtypes", "deprecation" })
//MULTI LINE to SINGLE LINE -> with multiple Component
public void test007_c() throws Exception {
if (this.apiLevel != 21) {
System.err.println("Test "+getName()+" requires a JRE 21");
return;
}
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf= new StringBuilder();
buf.append("package test1;\n");
buf.append("public class X {\n");
buf.append(" void foo(Object o) {\n");
buf.append(" String name = \"Jay\";\n");
//buf.append(" String s = STR.\"\"\"\n\"Hello \\{name} \n\"\"\"\";\n");
buf.append(" String s = STR.\"\"\"\nHello \\{name} \n\"\"\";\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null);

CompilationUnit astRoot= createAST(cu);
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
AST ast= astRoot.getAST();

assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
TypeDeclaration type= findTypeDeclaration(astRoot, "X");
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
Block block= methodDecl.getBody();
List blockStatements = block.statements();
assertEquals("Incorrect number of statements", 2, blockStatements.size());
{
VariableDeclarationStatement varStmt = (VariableDeclarationStatement) blockStatements.get(1);
assertEquals("Incorrect number of fragents", 1, varStmt.fragments().size());
VariableDeclarationFragment varFragment = (VariableDeclarationFragment) varStmt.fragments().get(0);
StringTemplateExpression templateExp = (StringTemplateExpression) varFragment.getInitializer();

StringTemplateComponent component = ast.newStringTemplateComponent();
SimpleName name = ast.newSimpleName("os");
StringFragment fragment = ast.newStringFragment();
fragment.setEscapedValue(" is your OS. ");
component.setStringFragment(fragment);
component.setEmbeddedExpression(name);

// StringTemplateComponent component1 = ast.newStringTemplateComponent();
// SimpleName name1 = ast.newSimpleName("xyz");
// StringFragment fragment1 = ast.newStringFragment();
// fragment1.setEscapedValue(" is xyz.");
// component1.setStringFragment(fragment1);
// component1.setEmbeddedExpression(name1);

rewrite.getListRewrite(templateExp, StringTemplateExpression.STRING_TEMPLATE_COMPONENTS).insertLast(component, null);
//rewrite.getListRewrite(templateExp, StringTemplateExpression.STRING_TEMPLATE_COMPONENTS).insertLast(component1, null);
rewrite.set(templateExp, StringTemplateExpression.MULTI_LINE, Boolean.FALSE, null);
}

String preview = evaluateRewrite(cu, rewrite);

buf= new StringBuilder();
buf.append("package test1;\n");
buf.append("public class X {\n");
buf.append(" void foo(Object o) {\n");
buf.append(" String name = \"Jay\";\n");
buf.append(" String s = STR.\"Hello \\{name} \\{os} is your OS. \";\n");
buf.append(" }\n");
buf.append("}\n");

assertEqualString(preview, buf.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -604,12 +604,7 @@ private int rewriteList(
int offset) {
this.startPos= offset;
this.list= getEvent(parent, property).getChildren();
int total;

if(this.list == null)
total = 0;
else
total= this.list.length;
int total= this.list.length;

if (total == 0) {
return this.startPos;
Expand Down Expand Up @@ -670,8 +665,13 @@ private int rewriteList(
if (separatorState == EXISTING) {
updateIndent(prevMark, currPos, i, editGroup);
}

doTextInsert(currPos, node, getNodeIndent(i), true, editGroup); // insert node
doTextInsert(
(parent instanceof StringTemplateExpression && ((StringTemplateExpression) parent).isMultiline()) ? currPos-4 : currPos,
//currPos,
node,
getNodeIndent(i),
true,
editGroup);

separatorState= NEW;
if (i != lastNonDelete) {
Expand Down Expand Up @@ -918,6 +918,42 @@ private int rewriteJavadoc(ASTNode node, StructuralPropertyDescriptor property)
return pos;
}

private void rewriteStringTemplateNode(StringTemplateExpression node, int pos) {
RewriteEvent event= getEvent(node, StringTemplateExpression.MULTI_LINE);

if (event != null) {
switch (event.getChangeKind()) {
case RewriteEvent.REPLACED: {
boolean originalNode= ((Boolean) event.getOriginalValue()).booleanValue();
boolean newValue= ((Boolean) event.getNewValue()).booleanValue();

int pos1 = rewriteRequiredNode(node, StringTemplateExpression.TEMPLATE_PROCESSOR);
int pos2 = rewriteRequiredNode(node, StringTemplateExpression.STRING_TEMPLATE_COMPONENTS);

if(newValue && !originalNode) {// SINGLE LINE to MULTI LINE
if(pos2 == 0 ) { // without component
doTextReplace(pos1 +1, 1, "\"\"\"\n", getEditGroup(event)); //$NON-NLS-1$
doTextReplace(pos -1, 1, "\n\"\"\"", getEditGroup(event)); //$NON-NLS-1$
} else { // with Component
doTextReplace(pos1 +1, 1, "\"\"\"\n", getEditGroup(event)); //$NON-NLS-1$
doTextReplace(pos2, 1, "\n\"\"\"", getEditGroup(event)); //$NON-NLS-1$
}

} else if(!newValue && originalNode) {// MULTI LINE to SINGLE LINE
if(pos2 == 0) { // without Component
doTextReplace(pos1 +1, 4, "\"", getEditGroup(event)); //$NON-NLS-1$
doTextReplace(pos-4, 4, "\"", getEditGroup(event)); //$NON-NLS-1$
} else { // with Component
doTextReplace(pos1 +1, 4, "\"", getEditGroup(event)); //$NON-NLS-1$
doTextReplace(pos2-4, 4, "\"", getEditGroup(event)); //$NON-NLS-1$
}
}
break;
}
}
}
}


/*
* endpos can be -1 -> use the end pos of the body
Expand Down Expand Up @@ -1430,8 +1466,7 @@ private int getPosAfterLeftBrace(int pos) {
* Next token is a right parenthesis. Returns the offset after the parenthesis. For incomplete code, return the start offset.
*/
private int getPosAfterRightParenthesis(int pos) {
try {
return getPosAfterToken(pos, TerminalTokens.TokenNameRPAREN);
try { return getPosAfterToken(pos, TerminalTokens.TokenNameRPAREN);
} catch (IllegalArgumentException e) {
return pos;
}
Expand Down Expand Up @@ -1526,7 +1561,6 @@ final void doTextInsert(int insertOffset, ASTNode node, int initialIndentLevel,
doTextInsert(insertOffset, insertStr, editGroup);
}
}

private boolean needsNewLineForLineComment(ASTNode node, String formatted, int offset) {
if (!this.lineCommentEndOffsets.isEndOfLineComment(getExtendedEnd(node), this.content)) {
return false;
Expand Down Expand Up @@ -4684,38 +4718,8 @@ public boolean visit(StringTemplateExpression node) {
if (!hasChildrenChanges(node)) {
return doVisitUnchangedChildren(node);
}
RewriteEvent event= getEvent(node, StringTemplateExpression.MULTI_LINE);
int pos1 = rewriteRequiredNode(node, StringTemplateExpression.TEMPLATE_PROCESSOR);
int pos = rewriteRequiredNode(node, StringTemplateExpression.FIRST_STRING_FRAGMENT);
int pos2 = rewriteRequiredNode(node, StringTemplateExpression.STRING_TEMPLATE_COMPONENTS);
if (event != null) {
switch (event.getChangeKind()) {
case RewriteEvent.REPLACED: {
boolean originalNode= ((Boolean) event.getOriginalValue()).booleanValue();
boolean newValue= ((Boolean) event.getNewValue()).booleanValue();

if(newValue && !originalNode) {// SINGLE LINE to MULTI LINE
if(pos2 == 0 ) { // without component
doTextReplace(pos1 +1, 1, "\"\"\"\n", getEditGroup(event)); //$NON-NLS-1$
doTextReplace(pos -1, 1, "\n\"\"\"", getEditGroup(event)); //$NON-NLS-1$
} else { // with Component
doTextReplace(pos1 +1, 1, "\"\"\"\n", getEditGroup(event)); //$NON-NLS-1$
doTextReplace(pos2, 1, "\n\"\"\"", getEditGroup(event)); //$NON-NLS-1$
}

} else if(!newValue && originalNode) {// MULTI LINE to SINGLE LINE
if(pos2 == 0) { // without Component
doTextReplace(pos1 +1, 4, "\"", getEditGroup(event)); //$NON-NLS-1$
doTextReplace(pos-4, 4, "\"", getEditGroup(event)); //$NON-NLS-1$
} else { // with Component
doTextReplace(pos1 +1, 4, "\"", getEditGroup(event)); //$NON-NLS-1$
doTextReplace(pos2-4, 4, "\"", getEditGroup(event)); //$NON-NLS-1$
}
}
break;
}
}
}
if (node.getAST().isPreviewEnabled()) {
rewriteNodeList(
node,
Expand All @@ -4725,6 +4729,7 @@ public boolean visit(StringTemplateExpression node) {
"", //$NON-NLS-1$
"", //$NON-NLS-1$
""); //$NON-NLS-1$
rewriteStringTemplateNode(node, pos);//handles SINGLE LINE to MULTI LINE and vice versa
}

return false;
Expand Down

0 comments on commit 2c09616

Please sign in to comment.