Skip to content

Commit 99d703a

Browse files
committed
added non-working test cases for future reference and some code polish
1 parent 8544efa commit 99d703a

File tree

3 files changed

+75
-33
lines changed

3 files changed

+75
-33
lines changed

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

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.jdt.core.dom.Annotation;
2525
import org.eclipse.jdt.core.dom.ITypeBinding;
2626
import org.eclipse.jdt.core.dom.MemberValuePair;
27+
import org.eclipse.jdt.core.dom.Name;
2728
import org.eclipse.jdt.core.dom.QualifiedName;
2829
import org.eclipse.jdt.core.dom.SimpleName;
2930
import org.eclipse.jdt.core.dom.StringLiteral;
@@ -75,6 +76,11 @@ public void provideCompletions(ASTNode node, Annotation annotation, ITypeBinding
7576

7677
IJavaProject project = optionalProject.get();
7778

79+
// in case the node is embedded in an qualified name, e.g. "file.txt", use the fully qualified node instead just a part
80+
if (node instanceof Name && node.getParent() instanceof QualifiedName) {
81+
node = node.getParent();
82+
}
83+
7884
// case: @Value(<*>)
7985
if (node == annotation && doc.get(offset - 1, 2).endsWith("()")) {
8086
List<Match<PropertyInfo>> matches = findMatches("", doc);
@@ -95,20 +101,16 @@ public void provideCompletions(ASTNode node, Annotation annotation, ITypeBinding
95101
addClasspathResourceProposals(project, doc, offset, offset, "", true, completions);
96102
}
97103
// case: @Value(prefix<*>)
98-
else if (node instanceof SimpleName && node.getParent() instanceof Annotation) {
104+
else if (node instanceof Name && node.getParent() instanceof Annotation) {
99105
computeProposalsForSimpleName(project, node, completions, offset, doc);
100106
}
101-
// case: @Value(file.ext<*>) - the "." causes a QualifierNode to be generated
102-
else if (node instanceof SimpleName && node.getParent() instanceof QualifiedName && node.getParent().getParent() instanceof Annotation) {
103-
computeProposalsForSimpleName(project, node.getParent(), completions, offset, doc);
104-
}
105107
// case: @Value(value=<*>)
106-
else if (node instanceof SimpleName && node.getParent() instanceof MemberValuePair
108+
else if (node instanceof Name && node.getParent() instanceof MemberValuePair
107109
&& "value".equals(((MemberValuePair)node.getParent()).getName().toString())) {
108110
computeProposalsForSimpleName(project, node, completions, offset, doc);
109111
}
110112
// case: @Value(value=<*>)
111-
else if (node instanceof SimpleName && node.getParent() instanceof QualifiedName && node.getParent().getParent() instanceof MemberValuePair
113+
else if (node instanceof Name && node.getParent() instanceof QualifiedName && node.getParent().getParent() instanceof MemberValuePair
112114
&& "value".equals(((MemberValuePair)node.getParent().getParent()).getName().toString())) {
113115
computeProposalsForSimpleName(project, node.getParent(), completions, offset, doc);
114116
}
@@ -131,29 +133,6 @@ else if (node instanceof StringLiteral && node.getParent() instanceof MemberValu
131133
}
132134
}
133135

134-
private void addClasspathResourceProposals(IJavaProject project, TextDocument doc, int startOffset, int endOffset, String prefix, boolean includeQuotes, Collection<ICompletionProposal> completions) {
135-
String[] resources = findResources(project, prefix);
136-
137-
double score = resources.length + 1000;
138-
for (String resource : resources) {
139-
140-
DocumentEdits edits = new DocumentEdits(doc, false);
141-
142-
if (includeQuotes) {
143-
edits.replace(startOffset, endOffset, "\"classpath:" + resource + "\"");
144-
}
145-
else {
146-
edits.replace(startOffset, endOffset, "classpath:" + resource);
147-
}
148-
149-
String label = "classpath:" + resource;
150-
151-
ICompletionProposal proposal = new AnnotationAttributeCompletionProposal(edits, label, label, null, score--);
152-
completions.add(proposal);
153-
}
154-
155-
}
156-
157136
private void computeProposalsForSimpleName(IJavaProject project, ASTNode node, Collection<ICompletionProposal> completions, int offset, TextDocument doc) {
158137
String prefix = identifyPropertyPrefix(node.toString(), offset - node.getStartPosition());
159138

@@ -253,15 +232,15 @@ public String identifyPropertyPrefix(String nodeContent, int offset) {
253232

254233
private List<Match<PropertyInfo>> findMatches(String prefix, IDocument doc) {
255234
FuzzyMap<PropertyInfo> index = indexProvider.getIndex(doc).getProperties();
256-
List<Match<PropertyInfo>> matches =index.find(camelCaseToHyphens(prefix));
235+
List<Match<PropertyInfo>> matches = index.find(camelCaseToHyphens(prefix));
257236

258-
//First the 'real' properties.
237+
// First the 'real' properties.
259238
Set<String> suggestedKeys = new HashSet<>();
260239
for (Match<PropertyInfo> m : matches) {
261240
suggestedKeys.add(m.data.getId());
262241
}
263242

264-
//Then also add 'ad-hoc' properties (see https://www.pivotaltracker.com/story/show/153107266).
243+
// Then also add 'ad-hoc' properties
265244
Optional<IJavaProject> p = projectFinder.find(new TextDocumentIdentifier(doc.getUri()));
266245
if (p.isPresent()) {
267246
index = adHocIndexProvider.getIndex(p.get());
@@ -274,6 +253,29 @@ private List<Match<PropertyInfo>> findMatches(String prefix, IDocument doc) {
274253
return matches;
275254
}
276255

256+
private void addClasspathResourceProposals(IJavaProject project, TextDocument doc, int startOffset, int endOffset, String prefix, boolean includeQuotes, Collection<ICompletionProposal> completions) {
257+
String[] resources = findResources(project, prefix);
258+
259+
double score = resources.length + 1000;
260+
for (String resource : resources) {
261+
262+
DocumentEdits edits = new DocumentEdits(doc, false);
263+
264+
if (includeQuotes) {
265+
edits.replace(startOffset, endOffset, "\"classpath:" + resource + "\"");
266+
}
267+
else {
268+
edits.replace(startOffset, endOffset, "classpath:" + resource);
269+
}
270+
271+
String label = "classpath:" + resource;
272+
273+
ICompletionProposal proposal = new AnnotationAttributeCompletionProposal(edits, label, label, null, score--);
274+
completions.add(proposal);
275+
}
276+
277+
}
278+
277279
private String[] findResources(IJavaProject project, String prefix) {
278280
String[] resources = IClasspathUtil.getClasspathResources(project.getClasspath()).stream()
279281
.distinct()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
public class ValuePropertyKeyProposal extends ScoreableProposal {
2525

2626
private static final String EMPTY_DETAIL = "";
27+
2728
private DocumentEdits edits;
2829
private String label;
2930
private String detail;

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ void testPlainPrefixCompletion() throws Exception {
294294
assertClasspathCompletions();
295295
}
296296

297+
// The parser removes the (spring.) piece from the AST in this case, so there is no way
298+
// to clearly identify this case
299+
//
300+
// @Test
301+
// void testComplexPrefixCompletionParamNameCursorRightAfterDot() throws Exception {
302+
// prepareCase("@Value(\"onField\")", "@Value(spring.<*>)");
303+
// prepareDefaultIndexData();
304+
//
305+
// assertPropertyCompletions(
306+
// "@Value(\"${spring.prop1}\"<*>)");
307+
//
308+
// assertClasspathCompletions();
309+
// }
310+
297311
@Test
298312
void testComplexPrefixCompletion() throws Exception {
299313
prepareCase("@Value(\"onField\")", "@Value(spring.pr<*>)");
@@ -316,6 +330,20 @@ void testPrefixCompletionWithParamName() throws Exception {
316330
assertClasspathCompletions();
317331
}
318332

333+
// The parser removes the (spring.) piece from the AST in this case, so there is no way
334+
// to clearly identify this case
335+
//
336+
// @Test
337+
// void testPrefixCompletionWithParamNameCursorRightAfterDot() throws Exception {
338+
// prepareCase("@Value(\"onField\")", "@Value(value=spring.<*>)");
339+
// prepareDefaultIndexData();
340+
//
341+
// assertPropertyCompletions(
342+
// "@Value(value=\"${spring.prop1}\"<*>)");
343+
//
344+
// assertClasspathCompletions();
345+
// }
346+
319347
@Test
320348
void testComplexPrefixCompletionWithParamName() throws Exception {
321349
prepareCase("@Value(\"onField\")", "@Value(value=spring.pr<*>)");
@@ -419,6 +447,17 @@ void testQoutedPrefixCompletion() throws Exception {
419447
assertClasspathCompletions();
420448
}
421449

450+
@Test
451+
void testComplexPrefixCompletionWithQuotesAndDotRightAfterPrefix() throws Exception {
452+
prepareCase("@Value(\"onField\")", "@Value(\"spring.<*>\")");
453+
prepareDefaultIndexData();
454+
455+
assertPropertyCompletions(
456+
"@Value(\"${spring.prop1}<*>\")");
457+
458+
assertClasspathCompletions();
459+
}
460+
422461
@Test
423462
void testComplexPrefixCompletionWithQuotes() throws Exception {
424463
prepareCase("@Value(\"onField\")", "@Value(\"spring.pr<*>\")");

0 commit comments

Comments
 (0)