Skip to content

Commit 3a6dd8b

Browse files
committed
Handle duplicates for bean completion proposals
1 parent 3df5d8e commit 3a6dd8b

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/beans/BeanCompletionProposal.java

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.Objects;
1516
import java.util.Optional;
1617

1718
import org.apache.commons.text.similarity.JaroWinklerSimilarity;
@@ -201,4 +202,23 @@ private boolean isInsideConstructor(ASTNode node) {
201202
return false;
202203
}
203204

205+
@Override
206+
public int hashCode() {
207+
return Objects.hash(beanId, beanType);
208+
}
209+
210+
@Override
211+
public boolean equals(Object obj) {
212+
if (this == obj)
213+
return true;
214+
if (obj == null)
215+
return false;
216+
if (getClass() != obj.getClass())
217+
return false;
218+
BeanCompletionProposal other = (BeanCompletionProposal) obj;
219+
return Objects.equals(beanId, other.beanId) && Objects.equals(beanType, other.beanType);
220+
}
221+
222+
223+
204224
}

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/beans/BeanCompletionProvider.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public void provideCompletions(ASTNode node, int offset, TextDocument doc, Colle
109109
fieldNames.add(vd.getName());
110110
fieldTypes.add(vd.getType().getQualifiedName());
111111
}
112+
// Collect bean completions in a set to exclude possible bean symbol duplications
113+
Set<BeanCompletionProposal> beanCompletions = new HashSet<>();
112114
for (Bean bean : beans) {
113115
// If current class is a bean - ignore it
114116
if (className.equals(bean.getType())) {
@@ -127,9 +129,10 @@ public void provideCompletions(ASTNode node, int offset, TextDocument doc, Colle
127129
bean.getType(), fieldName, className, rewriteRefactorings);
128130

129131
if (proposal.getScore() > 0) {
130-
completions.add(proposal);
132+
beanCompletions.add(proposal);
131133
}
132134
}
135+
completions.addAll(beanCompletions);
133136
}
134137
} catch (Exception e) {
135138
log.error("problem while looking for bean completions", e);

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/java/beans/test/BeanCompletionProviderTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class BeanCompletionProviderTest {
7171
private Bean bean5;
7272
private Bean bean6;
7373
private Bean bean7;
74+
private Bean bean8;
7475

7576
@BeforeEach
7677
public void setup() throws Exception {
@@ -97,8 +98,9 @@ public void setup() throws Exception {
9798
bean5 = new Bean("petService", "org.springframework.samples.petclinic.pet.Inner.PetService", new Location(tempJavaDocUri, new Range(new Position(1,1), new Position(1, 20))), null, null, null, false, "symbolLabel");
9899
bean6 = new Bean("testBeanCompletionClass", "org.sample.test.TestBeanCompletionClass", new Location(tempJavaDocUri, new Range(new Position(1,1), new Position(1, 20))), null, null, null, false, "symbolLabel");
99100
bean7 = new Bean("testIntBean", "java.lang.Integer", new Location(tempJavaDocUri, new Range(new Position(1,1), new Position(1, 20))), null, null, null, false, "symbolLabel");
101+
bean8 = new Bean("testIntBean", "java.lang.Integer", new Location(tempJavaDocUri, new Range(new Position(1,1), new Position(1, 20))), null, null, null, false, "symbolLabelDuplicate");
100102

101-
springIndex.updateBeans(project.getElementName(), new Bean[] {bean1, bean2, bean3, bean4, bean5, bean6, bean7});
103+
springIndex.updateBeans(project.getElementName(), new Bean[] {bean1, bean2, bean3, bean4, bean5, bean6, bean7, bean8});
102104
}
103105

104106
@AfterEach

0 commit comments

Comments
 (0)