Skip to content

Commit

Permalink
refactor AnnotationAttributeCompletionProviders to also return labels
Browse files Browse the repository at this point in the history
  • Loading branch information
vudayani authored and martinlippert committed Sep 26, 2024
1 parent 46778a7 commit 4a63ffd
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.springframework.ide.vscode.boot.java.cron.CronExpressionCompletionProvider;
import org.springframework.ide.vscode.boot.java.handlers.CompletionProvider;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits;
Expand Down Expand Up @@ -123,40 +122,22 @@ private void createCompletionProposals(IJavaProject project, TextDocument doc, A

AnnotationAttributeCompletionProvider completionProvider = this.completionProviders.get(attributeName);
if (completionProvider != null) {
List<String> candidates = completionProvider.getCompletionCandidates(project);

if (completionProvider instanceof CronExpressionCompletionProvider) {
Map<String, String> proposals = completionProvider.getCompletionCandidatesWithLabels(project);
Map<String, String> filteredProposals = proposals.entrySet().stream()
.filter(candidate -> candidate.getKey().toLowerCase().contains(filterPrefix.toLowerCase()))
.filter(candidate -> !alreadyMentionedValues.contains(candidate.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
double score = filteredProposals.size();
for (Map.Entry<String, String> entry : filteredProposals.entrySet()) {
String candidate = entry.getKey();
DocumentEdits edits = new DocumentEdits(doc, false);
edits.replace(startOffset, endOffset, createReplacementText.apply(candidate));
AnnotationAttributeCompletionProposal proposal = new AnnotationAttributeCompletionProposal(edits,
candidate, entry.getValue(), null, score--);
completions.add(proposal);

}
} else {

List<String> filteredCandidates = candidates.stream()
// .filter(candidate -> candidate.toLowerCase().startsWith(filterPrefix.toLowerCase()))
.filter(candidate -> candidate.toLowerCase().contains(filterPrefix.toLowerCase()))
.filter(candidate -> !alreadyMentionedValues.contains(candidate)).collect(Collectors.toList());
double score = filteredCandidates.size();
for (String candidate : filteredCandidates) {
Map<String, String> proposals = completionProvider.getCompletionCandidates(project);
Map<String, String> filteredProposals = proposals.entrySet().stream()
.filter(candidate -> candidate.getKey().toLowerCase().contains(filterPrefix.toLowerCase()))
.filter(candidate -> !alreadyMentionedValues.contains(candidate.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
double score = filteredProposals.size();
for (Map.Entry<String, String> entry : filteredProposals.entrySet()) {
String candidate = entry.getKey();
DocumentEdits edits = new DocumentEdits(doc, false);
edits.replace(startOffset, endOffset, createReplacementText.apply(candidate));

AnnotationAttributeCompletionProposal proposal = new AnnotationAttributeCompletionProposal(edits,
candidate, entry.getValue(), null, score--);
completions.add(proposal);

DocumentEdits edits = new DocumentEdits(doc, false);
edits.replace(startOffset, endOffset, createReplacementText.apply(candidate));

AnnotationAttributeCompletionProposal proposal = new AnnotationAttributeCompletionProposal(edits,
candidate, candidate, null, score--);
completions.add(proposal);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,12 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.annotations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ide.vscode.commons.java.IJavaProject;

public interface AnnotationAttributeCompletionProvider {

default List<String> getCompletionCandidates(IJavaProject project) {
return new ArrayList<>();
}


default Map<String, String> getCompletionCandidatesWithLabels(IJavaProject project) {
return new HashMap<>();
}
Map<String, String> getCompletionCandidates(IJavaProject project);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package org.springframework.ide.vscode.boot.java.beans;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
Expand Down Expand Up @@ -235,11 +236,11 @@ public DependsOnCompletionProcessor(SpringMetamodelIndex springIndex) {
// }

@Override
public List<String> getCompletionCandidates(IJavaProject project) {
public Map<String, String> getCompletionCandidates(IJavaProject project) {
return Arrays.stream(this.springIndex.getBeansOfProject(project.getElementName()))
.map(bean -> bean.getName())
.distinct()
.toList();
.collect(Collectors.toMap(key -> key, value -> value));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package org.springframework.ide.vscode.boot.java.beans;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
Expand All @@ -32,15 +33,15 @@ public NamedCompletionProvider(SpringMetamodelIndex springIndex) {
}

@Override
public List<String> getCompletionCandidates(IJavaProject project) {
public Map<String, String> getCompletionCandidates(IJavaProject project) {

Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());

return Stream.concat(
findAllNamedValues(beans),
Arrays.stream(beans).map(bean -> bean.getName()))
.distinct()
.toList();
.collect(Collectors.toMap(key -> key, value -> value));
}

private Stream<String> findAllNamedValues(Bean[] beans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package org.springframework.ide.vscode.boot.java.beans;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
Expand All @@ -32,13 +33,13 @@ public ProfileCompletionProvider(SpringMetamodelIndex springIndex) {
}

@Override
public List<String> getCompletionCandidates(IJavaProject project) {
public Map<String, String> getCompletionCandidates(IJavaProject project) {

Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());

return findAllProfiles(beans)
.distinct()
.toList();
.collect(Collectors.toMap(key -> key, value -> value));
}

private Stream<String> findAllProfiles(Bean[] beans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package org.springframework.ide.vscode.boot.java.beans;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
Expand All @@ -32,15 +33,15 @@ public QualifierCompletionProvider(SpringMetamodelIndex springIndex) {
}

@Override
public List<String> getCompletionCandidates(IJavaProject project) {
public Map<String, String> getCompletionCandidates(IJavaProject project) {

Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());

return Stream.concat(
findAllQualifiers(beans),
Arrays.stream(beans).map(bean -> bean.getName()))
.distinct()
.toList();
.collect(Collectors.toMap(key -> key, value -> value));
}

private Stream<String> findAllQualifiers(Bean[] beans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
package org.springframework.ide.vscode.boot.java.beans;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
Expand All @@ -30,13 +31,13 @@ public ResourceCompletionProvider(SpringMetamodelIndex springIndex) {
}

@Override
public List<String> getCompletionCandidates(IJavaProject project) {
public Map<String, String> getCompletionCandidates(IJavaProject project) {

Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());

return Arrays.stream(beans).map(bean -> bean.getName())
.distinct()
.toList();
.collect(Collectors.toMap(key -> key, value -> value));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import java.nio.file.Paths;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.commons.java.IClasspathUtil;
Expand All @@ -23,8 +24,8 @@
*/
public class ConditionalOnResourceCompletionProcessor implements AnnotationAttributeCompletionProvider {

private List<String> findResources(IJavaProject project) {
List<String> resources = IClasspathUtil.getClasspathResources(project.getClasspath()).stream()
private Map<String, String> findResources(IJavaProject project) {
Map<String, String> resources = IClasspathUtil.getClasspathResources(project.getClasspath()).stream()
.distinct()
.sorted(new Comparator<String>() {
@Override
Expand All @@ -34,13 +35,13 @@ public int compare(String o1, String o2) {
})
.map(r -> r.replaceAll("\\\\", "/"))
.map(r -> "classpath:" + r)
.toList();
.collect(Collectors.toMap(key -> key, value -> value));

return resources;
}

@Override
public List<String> getCompletionCandidates(IJavaProject project) {
public Map<String, String> getCompletionCandidates(IJavaProject project) {
return findResources(project);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class CronExpressionCompletionProvider implements AnnotationAttributeComp


@Override
public Map<String, String> getCompletionCandidatesWithLabels(IJavaProject project) {
public Map<String, String> getCompletionCandidates(IJavaProject project) {
return CRON_EXPRESSIONS_MAP;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.scope;

import java.util.List;
import java.util.Map;

import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.commons.java.IJavaProject;
Expand All @@ -20,18 +20,18 @@
*/
public class ScopeCompletionProcessor implements AnnotationAttributeCompletionProvider {

private static final List<String> SCOPE_COMPLETIONS = List.of(
"application",
"globalSession",
"prototype",
"request",
"session",
"singleton",
"websocket"
);
private static final Map<String, String> SCOPE_COMPLETIONS = Map.of(
"application", "application",
"globalSession", "globalSession",
"prototype", "prototype",
"request", "request",
"session", "session",
"singleton", "singleton",
"websocket", "websocket"
);

@Override
public List<String> getCompletionCandidates(IJavaProject project) {
public Map<String, String> getCompletionCandidates(IJavaProject project) {
return SCOPE_COMPLETIONS;
}

Expand Down

0 comments on commit 4a63ffd

Please sign in to comment.