Skip to content

Commit

Permalink
Fix source position for lambda parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria authored and akurtakov committed Mar 29, 2024
1 parent f7f13c2 commit 4a2a11f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
Expand Down Expand Up @@ -69,6 +70,7 @@
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.NameQualifiedType;
import org.eclipse.jdt.core.dom.NodeFinder;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.QualifiedName;
Expand Down Expand Up @@ -5463,4 +5465,22 @@ public void testCaptureBinding18() throws CoreException {
assertEquals("bound's type argument is the original type argument", binding, bound.getTypeArguments()[0]);
}

public void testLambdaParameterSourcePosition() throws JavaModelException {
String contents = """
class X {
I lambda = (x, y) -> {};
}
interface I {
public void apply(Integer k, Integer l);
}
""";
this.workingCopy = getWorkingCopy("/Converter11/src/X.java", true/*resolve*/);
ASTNode node = buildAST(contents, this.workingCopy);
Name name = (Name)NodeFinder.perform(node, contents.indexOf('y'), 0);
IVariableBinding variableBinding = (IVariableBinding)name.resolveBinding();
ILocalVariable variableElement = (ILocalVariable)variableBinding.getJavaElement();
assertEquals(26, variableElement.getSourceRange().getOffset());
assertEquals(1, variableElement.getSourceRange().getLength());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,17 @@ private JavaElement getUnresolvedJavaElement() {
SimpleName localName = localVar.getName();
int nameStart = localName.getStartPosition();
int nameLength = localName.getLength();
int sourceStart;
int sourceLength;
int sourceStart = localVar.getStartPosition();
int sourceLength = localVar.getLength();
int modifiers = 0;
if (localVar instanceof SingleVariableDeclaration) {
sourceStart = localVar.getStartPosition();
sourceLength = localVar.getLength();
final SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration) localVar;
if (localVar instanceof SingleVariableDeclaration singleVariableDeclaration) {
modifiers = singleVariableDeclaration.getModifiers();
} else {
ASTNode node = localVar.getParent();
sourceStart = node.getStartPosition();
sourceLength = node.getLength();
VariableDeclarationFragment fragment = (VariableDeclarationFragment) localVar;
} else if (localVar instanceof VariableDeclarationFragment fragment) {
final ASTNode parent = fragment.getParent();
if (!(parent instanceof LambdaExpression)) {
sourceStart = parent.getStartPosition();
sourceLength = parent.getLength();
}
switch (parent.getNodeType()) {
case ASTNode.VARIABLE_DECLARATION_EXPRESSION :
VariableDeclarationExpression expression = (VariableDeclarationExpression) parent;
Expand Down

0 comments on commit 4a2a11f

Please sign in to comment.