Skip to content

Commit

Permalink
fix: sniper of local variables (#3412)
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored Jun 12, 2020
1 parent 020800f commit 7186874
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -720,13 +720,14 @@ private void forEachConstantFragment(int start, int end, Consumer<SourceFragment
StringBuilder buff = new StringBuilder();
CharType lastType = null;
int off = start;
// basic tokenization based on spaces
while (off < end) {
char c = sourceCode.charAt(off);
CharType type = CharType.fromChar(c);
if (type != lastType) {
if (lastType != null) {
onCharSequence(lastType, buff, consumer);
buff.setLength(0);
buff.setLength(0); // reset
}
lastType = type;
}
Expand Down Expand Up @@ -766,8 +767,10 @@ private void onCharSequence(CharType type, StringBuilder buff, Consumer<SourceFr
longestMatcher = strMatcher.getLonger(longestMatcher);
}
}
// nothing has matched, best effort token
if (longestMatcher == null) {
throw new SpoonException("Unexpected source text: " + buff.toString());
consumer.accept(new TokenSourceFragment(str.toString(), TokenType.CODE_SNIPPET));
return;
}
consumer.accept(new TokenSourceFragment(longestMatcher.toString(), longestMatcher.getType()));
off += longestMatcher.getLength();
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ public void testPrintReplacementOfInvocation() {
});
}

@Test
public void testPrintLocalVariableDeclaration() {
// contract: joint local declarations can be sniper-printed in whole unmodified method
testSniper(OneLineMultipleVariableDeclaration.class.getName(), type -> {
type.getFields().stream().forEach(x -> {x.delete();});
}, (type, printed) -> {
assertEquals("package spoon.test.prettyprinter.testclasses;\n" +
"\n" +
"public class OneLineMultipleVariableDeclaration {\n" +
"\n" +
"\tvoid foo(int a) {\n" +
"\t\tint b = 0, e = 1;\n" +
"\t\ta = a;\n" +
"\t}\n" +
"}", printed);
});
}

@Test
public void testPrintLocalVariableDeclaration2() {
// contract: joint local declarations can be sniper-printed
testSniper(OneLineMultipleVariableDeclaration.class.getName(), type -> {
type.getElements(new TypeFilter<>(CtLocalVariable.class)).get(0).delete();
}, (type, printed) -> {
assertEquals("package spoon.test.prettyprinter.testclasses;\n" +
"\n" +
"public class OneLineMultipleVariableDeclaration {int a;\n" +
"\n" +
"\tint c;\n" +
"\n" +
"\tvoid foo(int a) {int e = 1;\n" +
"\t\ta = a;\n" +
"\t}\n" +
"}", printed);
});
}

@Test
public void testPrintOneLineMultipleVariableDeclaration() {
// contract: files with joint field declarations can be recompiled after sniper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class OneLineMultipleVariableDeclaration {
int a, c;

void foo(int a) {
int b = 0, e = 1;
a = a;
}
}

0 comments on commit 7186874

Please sign in to comment.