Bean completion invocation for this. prefix
This commit is contained in:
@@ -19,11 +19,14 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.eclipse.jdt.core.dom.ASTNode;
|
||||
import org.eclipse.jdt.core.dom.Annotation;
|
||||
import org.eclipse.jdt.core.dom.Assignment;
|
||||
import org.eclipse.jdt.core.dom.Block;
|
||||
import org.eclipse.jdt.core.dom.CompilationUnit;
|
||||
import org.eclipse.jdt.core.dom.FieldAccess;
|
||||
import org.eclipse.jdt.core.dom.IAnnotationBinding;
|
||||
import org.eclipse.jdt.core.dom.ITypeBinding;
|
||||
import org.eclipse.jdt.core.dom.SimpleName;
|
||||
import org.eclipse.jdt.core.dom.ThisExpression;
|
||||
import org.eclipse.jdt.core.dom.TypeDeclaration;
|
||||
import org.eclipse.jdt.core.dom.VariableDeclaration;
|
||||
import org.slf4j.Logger;
|
||||
@@ -81,6 +84,14 @@ public class BeanCompletionProvider implements CompletionProvider {
|
||||
return;
|
||||
}
|
||||
|
||||
// Empty SimpleName usually comes from unresolved FieldAccess, i.e. `this.owner` where `owner` field is not defined
|
||||
if (node instanceof SimpleName se && se.getLength() == 0
|
||||
&& node.getParent() instanceof Assignment assign
|
||||
&& assign.getLeftHandSide() instanceof FieldAccess fa
|
||||
&& fa.getExpression() instanceof ThisExpression) {
|
||||
node = fa.getName();
|
||||
}
|
||||
|
||||
if (isSpringComponent(topLevelClass)) {
|
||||
String className = getFullyQualifiedName(topLevelClass);
|
||||
Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());
|
||||
|
||||
@@ -482,6 +482,43 @@ public class TestBeanCompletionClass {
|
||||
""");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanCompletionWithThis() throws Exception {
|
||||
String content = """
|
||||
package org.sample.test;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
@Controller
|
||||
public class TestBeanCompletionClass {
|
||||
public void test() {
|
||||
this.owner<*>
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
|
||||
assertCompletions(content, new String[] {"ownerRepository", "ownerService"}, 0,
|
||||
"""
|
||||
package org.sample.test;
|
||||
|
||||
import org.springframework.samples.petclinic.owner.OwnerRepository;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
@Controller
|
||||
public class TestBeanCompletionClass {
|
||||
|
||||
private final OwnerRepository ownerRepository;
|
||||
|
||||
TestBeanCompletionClass(OwnerRepository ownerRepository) {
|
||||
this.ownerRepository = ownerRepository;
|
||||
}
|
||||
public void test() {
|
||||
this.ownerRepository<*>
|
||||
}
|
||||
}
|
||||
""");
|
||||
}
|
||||
|
||||
|
||||
private void assertCompletions(String completionLine, String[] expectedCompletions, int chosenCompletion, String expectedResult) throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user