Skip to content

Commit

Permalink
Bean completion invocation for this. prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Feb 26, 2025
1 parent 2512212 commit b47096e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

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;
Expand Down Expand Up @@ -81,6 +84,14 @@ public void provideCompletions(ASTNode node, int offset, TextDocument doc,
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,43 @@ public void test() {
""");
}

@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 {
Expand Down

0 comments on commit b47096e

Please sign in to comment.