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

fix(PositionBuilder): fix lambda parameters position #3137

Merged
merged 2 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/spoon/support/compiler/jdt/PositionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ SourcePosition buildPositionCtElement(CtElement e, ASTNode node) {

// Handle lambda parameters without explicit type
if (variableDeclaration instanceof Argument && variableDeclaration.type == null) {
declarationSourceStart = findPrevNonWhitespace(contents, 0, declarationSourceStart - 1);
declarationSourceEnd = findNextNonWhitespace(contents, contents.length - 1, declarationSourceEnd + 1);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pvojtechovsky was there a reason for declarationSourceStart - 1 and declarationSourceEnd + 1 that I am missing?

declarationSourceStart = findPrevNonWhitespace(contents, 0, declarationSourceStart);
declarationSourceEnd = findNextNonWhitespace(contents, contents.length - 1, declarationSourceEnd);
}

if (modifiersSourceStart <= 0) {
Expand Down
35 changes: 33 additions & 2 deletions src/test/java/spoon/test/position/PositionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@
import spoon.support.compiler.VirtualFile;
import spoon.support.reflect.CtExtendedModifier;
import spoon.test.comment.testclasses.BlockComment;
import spoon.test.comment.testclasses.Comment1;
import spoon.test.position.testclasses.AnnonymousClassNewIface;
import spoon.test.comment.testclasses.Comment1;import spoon.test.position.testclasses.AnnonymousClassNewIface;
import spoon.test.position.testclasses.ArrayArgParameter;
import spoon.test.position.testclasses.CatchPosition;
import spoon.test.position.testclasses.CompilationUnitComments;
Expand All @@ -88,6 +87,7 @@
import spoon.test.position.testclasses.FooGeneric;
import spoon.test.position.testclasses.FooInterface;
import spoon.test.position.testclasses.FooLabel;
import spoon.test.position.testclasses.FooLambda;
import spoon.test.position.testclasses.FooMethod;
import spoon.test.position.testclasses.FooStatement;
import spoon.test.position.testclasses.FooSwitch;
Expand All @@ -97,6 +97,7 @@
import spoon.test.position.testclasses.PositionTry;
import spoon.test.position.testclasses.SomeEnum;
import spoon.test.position.testclasses.TypeParameter;
import spoon.test.position.testclasses.MoreLambda;
import spoon.test.query_function.testclasses.VariableReferencesModelTest;
import spoon.testing.utils.ModelUtils;

Expand Down Expand Up @@ -1396,4 +1397,34 @@ public void testLinePositionOkWithOneLineClassCode() {
assertTrue("Some Spoon elements have an invalid line position",
listOfBadPositionElements.stream().allMatch(elt -> elt.getPosition().getLine() == 1));
}

@Test
public void testLambdaParameterPosition() {
// contract: position of lambda parameter is correct
final Factory build = build(new File("src/test/java/spoon/test/position/testclasses/FooLambda.java"));
final CtType<?> foo = build.Type().get(FooLambda.class);

String classContent = getClassContent(foo);

CtReturn<?> retStmt = (CtReturn<?>) foo.getMethodsByName("m").get(0).getBody().getStatement(0);
CtLambda<?> lambdaExpr = (CtLambda<?>) retStmt.getReturnedExpression();
CtParameter<?> param = lambdaExpr.getParameters().get(0);
assertEquals("i", contentAtPosition(classContent, param.getPosition()));
}

@Test
public void testLambdaParameterPosition1() {
// contract: position of lambda parameter is correct
final Factory build = build(new File("src/test/java/spoon/test/position/testclasses/MoreLambda.java"));
final CtType<?> foo = build.Type().get(MoreLambda.class);

String classContent = getClassContent(foo);

List<CtLambda> lambdas = foo.getElements(new TypeFilter(CtLambda.class));
lambdas.stream().forEach(
l -> l.getParameters().stream().forEach(
p -> assertEquals(((CtParameter) p).getSimpleName(), contentAtPosition(classContent, ((CtParameter) p).getPosition()))
)
);
}
}
43 changes: 43 additions & 0 deletions src/test/java/spoon/test/position/testclasses/MoreLambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package spoon.test.position.testclasses;

import java.util.function.BiPredicate;
import java.util.function.Predicate;

public class MoreLambda {

public static Predicate<Integer> m0() {
return integer -> integer.compareTo(7) > 0;
}

public static Predicate<Integer> m1() {
return in -> in.compareTo(7) > 0;
}

public static BiPredicate<Integer, Integer> m2() {
return (i,j) -> i.compareTo(j) > 0;
}

public static BiPredicate<Integer, Integer> m3() {
return (i ,j) -> i.compareTo(j) > 0;
}

public static BiPredicate<Integer, Integer> m4() {
return (i , j) -> i.compareTo(j) > 0;
}

public static BiPredicate<Integer, Integer> m5() {
return (in , jn) -> in.compareTo(jn) > 0;
}

public static BiPredicate<Integer, Integer> m6() {
return (integer , jnteger) -> integer.compareTo(jnteger) > 0;
}

public static BiPredicate<Integer, Integer> m7() {
return (in, jn) -> in.compareTo(jn) > 0;
}

public static BiPredicate<Integer, Integer> m8() {
return ( integer ,jnteger) -> integer.compareTo(jnteger) > 0;
}
}