Skip to content

Commit 08b2437

Browse files
committed
proper implementation of basic vs smart completion of bean references, fixes #1160
1 parent 006c814 commit 08b2437

5 files changed

Lines changed: 30 additions & 20 deletions

File tree

src/main/java/com/github/cameltooling/idea/completion/contributor/CamelJavaReferenceContributor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public CamelJavaReferenceContributor() {
4949
addCompletionExtension(new CamelEndpointNameCompletionExtension());
5050
addCompletionExtension(new CamelEndpointSmartCompletionExtension(false));
5151
addCompletionExtension(new CamelPropertyPlaceholderSmartCompletionExtension());
52-
extend(CompletionType.BASIC, psiElement().and(psiElement().inside(PsiFile.class).inFile(matchFileType("java"))),
52+
extend(null, psiElement().and(psiElement().inside(PsiFile.class).inFile(matchFileType("java"))),
5353
new CompositeCompletionProvider(getCamelCompletionExtensions())
5454
);
5555
extend(CompletionType.BASIC, psiElement(PsiJavaToken.class).with(new PatternCondition<>("CamelJavaBeanReferenceSmartCompletion") {

src/main/java/com/github/cameltooling/idea/completion/contributor/CamelPropertiesFileReferenceContributor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public CamelPropertiesFileReferenceContributor() {
3636
addCompletionExtension(new CamelEndpointSmartCompletionExtension(false));
3737
// To match with file with "properties" as extension
3838
final PsiFilePattern.Capture<PsiFile> filePattern = matchFileType("properties");
39-
extend(CompletionType.BASIC,
39+
extend(null,
4040
psiElement().and(psiElement().inside(PsiFile.class).inFile(filePattern)),
4141
new CompositeCompletionProvider(getCamelCompletionExtensions())
4242
);

src/main/java/com/github/cameltooling/idea/completion/contributor/CamelYamlFileReferenceContributor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public CamelYamlFileReferenceContributor() {
8888
addCompletionExtension(new CamelEndpointNameCompletionExtension());
8989
addCompletionExtension(new CamelEndpointSmartCompletionExtension(false));
9090
addCompletionExtension(new CamelPropertyPlaceholderSmartCompletionExtension());
91-
extend(CompletionType.BASIC,
91+
extend(null,
9292
psiElement().and(psiElement().inside(PsiFile.class).inFile(matchFileType("yaml", "yml"))),
9393
new CompositeCompletionProvider(getCamelCompletionExtensions())
9494
);

src/main/java/com/github/cameltooling/idea/completion/extension/BeanReferenceCompletionExtension.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.github.cameltooling.idea.service.CamelPreferenceService;
2626
import com.github.cameltooling.idea.util.BeanUtils;
2727
import com.intellij.codeInsight.completion.CompletionParameters;
28+
import com.intellij.codeInsight.completion.CompletionType;
2829
import com.intellij.codeInsight.lookup.AutoCompletionPolicy;
2930
import com.intellij.codeInsight.lookup.LookupElement;
3031
import com.intellij.openapi.module.Module;
@@ -43,9 +44,14 @@ public BeanReferenceCompletionExtension() {
4344
super(BeanReference.class);
4445
}
4546

47+
@Override
48+
public boolean supportsSmartCompletion() {
49+
return true;
50+
}
51+
4652
@Override
4753
protected List<LookupElement> findResults(@NotNull CompletionParameters parameters, @NotNull PsiElement element, @NotNull String query) {
48-
List<ReferenceableBeanId> targets = findTargets(element, query);
54+
List<ReferenceableBeanId> targets = findTargets(parameters, element, query);
4955
return targets.stream()
5056
.map(bean -> createLookupElementBuilder(bean.getId(), bean.getElement())
5157
.withTypeText(getReferencedClassName(bean))
@@ -58,20 +64,20 @@ private String getReferencedClassName(ReferenceableBeanId bean) {
5864
return psiClass != null ? psiClass.getName() : null;
5965
}
6066

61-
private List<ReferenceableBeanId> findTargets(PsiElement element, String query) {
67+
private List<ReferenceableBeanId> findTargets(@NotNull CompletionParameters parameters, PsiElement element, String query) {
6268
Module module = ModuleUtilCore.findModuleForPsiElement(element);
6369
if (module == null) {
6470
return Collections.emptyList();
6571
}
6672

67-
PsiType expectedType = getExpectedType(element);
68-
6973
Predicate<String> beanIdPredicate = b -> b.startsWith(query);
70-
if (expectedType != null) {
71-
return BeanUtils.getService().findReferenceableBeanIdsByType(module, beanIdPredicate, expectedType);
72-
} else {
73-
return BeanUtils.getService().findReferenceableBeanIds(module, beanIdPredicate);
74+
if (CompletionType.SMART.equals(parameters.getCompletionType())) {
75+
PsiType expectedType = getExpectedType(element);
76+
if (expectedType != null) {
77+
return BeanUtils.getService().findReferenceableBeanIdsByType(module, beanIdPredicate, expectedType);
78+
}
7479
}
80+
return BeanUtils.getService().findReferenceableBeanIds(module, beanIdPredicate);
7581
}
7682

7783
private PsiType getExpectedType(PsiElement element) {

src/test/java/com/github/cameltooling/idea/completion/extension/BeanReferenceCompletionExtensionIT.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,38 @@ protected String getTestDataPath() {
9292
" </camelContext>" +
9393
"</blueprint>";
9494

95-
public void testBeanInjectValue() {
95+
public void testBeanInjectValueBasic() {
9696
myFixture.configureByFiles("TestClass1.java", "TestClass2.java", "TestClass3.java", "beans.xml");
9797
myFixture.complete(CompletionType.BASIC);
9898
List<String> strings = myFixture.getLookupElementStrings();
9999
assertNotNull(strings);
100-
assertEquals(2, strings.size());
101-
assertContainsElements(strings, "testClass2Bean", "testClass2Bean2");
100+
assertSameElements(strings, "testClass1Bean", "testClass2Bean", "testClass2Bean2");
101+
}
102+
103+
public void testBeanInjectValueSmart() {
104+
myFixture.configureByFiles("TestClass1.java", "TestClass2.java", "TestClass3.java", "beans.xml");
105+
myFixture.complete(CompletionType.SMART);
106+
List<String> strings = myFixture.getLookupElementStrings();
107+
assertNotNull(strings);
108+
assertSameElements(strings, "testClass2Bean", "testClass2Bean2");
102109
}
103110

104111
public void testPropertyReference() {
105112
List<String> strings = doTestCompletionAtCaret(PROPERTY_REFERENCE);
106113
assertNotNull(strings);
107-
assertEquals(3, strings.size());
108-
assertContainsElements(strings, "bean1", "bean2", "boo");
114+
assertSameElements(strings, "bean1", "bean2", "boo");
109115
}
110116

111117
public void testRouteReference() {
112118
List<String> strings = doTestCompletionAtCaret(ROUTE_REFERENCE);
113119
assertNotNull(strings);
114-
assertEquals(2, strings.size());
115-
assertContainsElements(strings, "myBean", "myBean2");
120+
assertSameElements(strings, "myBean", "myBean2");
116121
}
117122

118123
public void testEndpointReference() {
119124
List<String> strings = doTestCompletionAtCaret(ENDPOINT_REFERENCE);
120125
assertNotNull(strings);
121-
assertEquals(2, strings.size());
122-
assertContainsElements(strings, "myBean", "myEndpoint");
126+
assertSameElements(strings, "myBean", "myEndpoint");
123127
}
124128

125129
public void testNoResultsWhenUsedAsEndpoint() {

0 commit comments

Comments
 (0)