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

Removing last statement with LexicalPreservingPrinter results in loss of indendation #3700

Closed
steve-thousand opened this issue Sep 19, 2022 · 1 comment · Fixed by #3704
Closed

Comments

@steve-thousand
Copy link

I am on javaparser-core 3.24.4 and am attempting to remove the last statement from a method, and it is resulting in a loss of indentation when using the LexicalPreservingPrinter.

@Test
void testRemoveLastStatement() {
  CompilationUnit cu = LexicalPreservingPrinter.setup(StaticJavaParser.parse(
      "class X {" + LineSeparator.SYSTEM +
          "  @Test" + LineSeparator.SYSTEM +
          "  public void testCase() {" + LineSeparator.SYSTEM +
          "    int x = 0;" + LineSeparator.SYSTEM +
          "    int y = 1;" + LineSeparator.SYSTEM +
          "  }" + LineSeparator.SYSTEM +
          "}" + LineSeparator.SYSTEM
  ));

  cu.getType(0).getMethods().get(0).getBody().get().getStatements().removeLast();

  assertEquals(
      "class X {" + LineSeparator.SYSTEM +
          "  @Test" + LineSeparator.SYSTEM +
          "  public void testCase() {" + LineSeparator.SYSTEM +
          "    int x = 0;" + LineSeparator.SYSTEM +
          "  }" + LineSeparator.SYSTEM +
          "}" + LineSeparator.SYSTEM, LexicalPreservingPrinter.print(cu.findCompilationUnit().get()));
}

This test fails, the actual output is

class X {
  @Test
  public void testCase() {
    int x = 0;
}
}

The same loss of indentation occurs when I attempt to replace the last statement

@Test
void testReplaceLastStatement() {
  CompilationUnit cu = LexicalPreservingPrinter.setup(StaticJavaParser.parse(
      "class X {" + LineSeparator.SYSTEM +
          "  @Test" + LineSeparator.SYSTEM +
          "  public void testCase() {" + LineSeparator.SYSTEM +
          "    int x = 0;" + LineSeparator.SYSTEM +
          "    int y = 1;" + LineSeparator.SYSTEM +
          "  }" + LineSeparator.SYSTEM +
          "}" + LineSeparator.SYSTEM
  ));

  cu.getType(0).getMethods().get(0).getBody().get().getStatements().removeLast();
  cu.getType(0).getMethods().get(0).getBody().get().getStatements().add(new ExpressionStmt(new VariableDeclarationExpr(
      PrimitiveType.intType(), "z")));

  assertEquals(
      "class X {" + LineSeparator.SYSTEM +
          "  @Test" + LineSeparator.SYSTEM +
          "  public void testCase() {" + LineSeparator.SYSTEM +
          "    int x = 0;" + LineSeparator.SYSTEM +
          "    int z;" + LineSeparator.SYSTEM +
          "  }" + LineSeparator.SYSTEM +
          "}" + LineSeparator.SYSTEM, LexicalPreservingPrinter.print(cu.findCompilationUnit().get()));
}

The above test fails, producing the actual output:

class X {
  @Test
  public void testCase() {
    int x = 0;
    int z;
}
}

Note that the same does not happen if I remove the first statement. The following test passes

@Test
void testRemoveFirstStatement() {
  CompilationUnit cu = LexicalPreservingPrinter.setup(StaticJavaParser.parse(
      "class X {" + LineSeparator.SYSTEM +
          "  @Test" + LineSeparator.SYSTEM +
          "  public void testCase() {" + LineSeparator.SYSTEM +
          "    int x = 0;" + LineSeparator.SYSTEM +
          "    int y = 1;" + LineSeparator.SYSTEM +
          "  }" + LineSeparator.SYSTEM +
          "}" + LineSeparator.SYSTEM
  ));

  cu.getType(0).getMethods().get(0).getBody().get().getStatements().removeFirst();

  assertEquals(
      "class X {" + LineSeparator.SYSTEM +
          "  @Test" + LineSeparator.SYSTEM +
          "  public void testCase() {" + LineSeparator.SYSTEM +
          "    int y = 1;" + LineSeparator.SYSTEM +
          "  }" + LineSeparator.SYSTEM +
          "}" + LineSeparator.SYSTEM, LexicalPreservingPrinter.print(cu.findCompilationUnit().get()));
}
@jlerbsc
Copy link
Collaborator

jlerbsc commented Sep 19, 2022

Hi @steve-thousand Thank for reporting this bug.

jlerbsc added a commit that referenced this issue Sep 20, 2022
Fix issue #3700 Removing last statement with LexicalPreservingPrinter results in loss of indendation
wadoon added a commit to jmltoolkit/jmlparser that referenced this issue Oct 5, 2022
* upstream/master:
  Accept final in instanceof pattern
  Avoid test failure due to line separator differences on windows host
  chore(deps): update dependency org.apache.maven.plugins:maven-jar-plugin to v3.3.0
  chore(deps): update dependency org.codehaus.mojo:versions-maven-plugin to v2.12.0
  chore(deps): update codecov/codecov-action action to v3.1.1
  chore(deps): update junit5 monorepo to v5.9.1
  Bug fix on windows system: compare results with ignoring end of line character
  Fix issue javaparser#3700 Removing last statement with LexicalPreservingPrinter results in loss of indendation
  chore(deps): update dependency com.github.valfirst:jbehave-junit-runner to v2.3.2
  fix(deps): update dependency org.javassist:javassist to v3.29.2-ga
  Refactoring - use of existing methods
  Refactoring adding convenient methods to know if a DifferenceElement is added, removed or kept
  Fix issue javaparser#3678 Function accepts a configuration but it does not do anything
  [maven-release-plugin] prepare for next development iteration
  [maven-release-plugin] update readme
  Update changelog
  [maven-release-plugin] prepare for next development iteration
  [maven-release-plugin] update readme
  Update changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants