Skip to content

Commit d5b21a7

Browse files
committed
No bean completion proposals in type declaration block
1 parent cc3aead commit d5b21a7

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ public void provideCompletions(ASTNode node, int offset, TextDocument doc, Colle
6969
&& (node instanceof SimpleName || node instanceof Block || node instanceof FieldAccess || node instanceof ThisExpression)) {
7070
try {
7171

72+
// Should be inside the block but not inside type declaration block.
73+
ASTNode block = node;
74+
for (; block != null && !(block instanceof Block); block = block.getParent()) {
75+
}
76+
// Not inside the block? Or inside the Type Declaration block? Bail out!
77+
if (block == null || block instanceof Block && block.getParent() instanceof TypeDeclaration) {
78+
return;
79+
}
80+
7281
if (node instanceof SimpleName) {
7382
if (node.getParent() instanceof FieldAccess fa && !(fa.getExpression() instanceof ThisExpression)) {
7483
return;

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

+43-1
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,48 @@ public TestBeanCompletionClass(String s) {
12201220
assertCompletions(content, new String[0], 0, null);
12211221
}
12221222

1223+
@Test
1224+
public void constructorNoCompletions_3() throws Exception {
1225+
String content = """
1226+
package org.sample.test;
1227+
1228+
import org.springframework.stereotype.Component;
1229+
1230+
@Component
1231+
public class Comp1 {
1232+
1233+
ow<*>
1234+
1235+
public Comp1() {
1236+
1237+
}
1238+
}
1239+
""";
1240+
1241+
assertCompletions(content, new String[0], 0, null);
1242+
}
1243+
1244+
@Test
1245+
public void constructorNoCompletions_4() throws Exception {
1246+
String content = """
1247+
package org.sample.test;
1248+
1249+
import org.springframework.stereotype.Component;
1250+
1251+
@Component
1252+
public class Comp1 {
1253+
1254+
ow<*>
1255+
1256+
Comp1() {
1257+
1258+
}
1259+
}
1260+
""";
1261+
1262+
assertCompletions(content, new String[0], 0, null);
1263+
}
1264+
12231265
private void assertCompletions(String completionLine, String[] expectedCompletions, int chosenCompletion, String expectedResult) throws Exception {
12241266
Editor editor = harness.newEditor(LanguageId.JAVA, completionLine, tempJavaDocUri);
12251267

@@ -1230,7 +1272,7 @@ private void assertCompletions(String completionLine, String[] expectedCompletio
12301272
if (expectedCompletions != null) {
12311273
String[] completionItems = completions.stream()
12321274
.map(item -> item.getLabel())
1233-
.limit(expectedCompletions.length)
1275+
.limit(expectedCompletions.length == 0 ? Integer.MAX_VALUE : expectedCompletions.length)
12341276
.toArray(size -> new String[size]);
12351277

12361278
assertArrayEquals(expectedCompletions, completionItems);

0 commit comments

Comments
 (0)