Skip to content

Commit

Permalink
refactored annotation attribute completion proecessors to allow more …
Browse files Browse the repository at this point in the history
…specific proposals to be created
  • Loading branch information
martinlippert committed Oct 24, 2024
1 parent 54df530 commit 4aeaf05
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -134,21 +133,22 @@ private void createCompletionProposals(IJavaProject project, TextDocument doc, A
AnnotationAttributeCompletionProvider completionProvider = this.completionProviders.get(attributeName);
if (completionProvider != null) {

Map<String, String> proposals = completionProvider.getCompletionCandidates(project, node);
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, (u, v) -> u, LinkedHashMap::new));
List<AnnotationAttributeProposal> proposals = completionProvider.getCompletionCandidates(project, node);

List<AnnotationAttributeProposal> filteredProposals = proposals.stream()
.filter(proposal -> proposal.getLabel().toLowerCase().contains(filterPrefix.toLowerCase()))
.filter(proposal -> !alreadyMentionedValues.contains(proposal.getLabel()))
.collect(Collectors.toList());

double score = filteredProposals.size();
for (Map.Entry<String, String> entry : filteredProposals.entrySet()) {
String candidate = entry.getKey();
for (AnnotationAttributeProposal candidate : filteredProposals) {
DocumentEdits edits = new DocumentEdits(doc, false);
edits.replace(startOffset, endOffset, createReplacementText.apply(candidate));
edits.replace(startOffset, endOffset, createReplacementText.apply(candidate.getLabel()));

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

completions.add(proposal);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,26 @@
*/
public class AnnotationAttributeCompletionProposal extends ScoreableProposal {

private static final String EMPTY_DETAIL = "";
private final AnnotationAttributeProposal coreProposal;

private final DocumentEdits edits;
private final String label;
private final String detail;
private final Renderable documentation;
private final double score;

public AnnotationAttributeCompletionProposal(DocumentEdits edits, String label, String detail, Renderable documentation, double score) {
public AnnotationAttributeCompletionProposal(DocumentEdits edits, AnnotationAttributeProposal coreProposal, Renderable documentation, double score) {
this.edits = edits;
this.label = label;
this.detail = detail == null ? EMPTY_DETAIL : detail;
this.coreProposal = coreProposal;
this.documentation = documentation;
this.score = score;
}

// public AnnotationAttributeCompletionProposal(DocumentEdits edits, String label, String detail, Renderable documentation, double score) {
// this(edits, new AnnotationAttributeProposal(label, detail), documentation, score);
// }

@Override
public String getLabel() {
return this.label;
return this.coreProposal.getLabel();
}

@Override
Expand All @@ -53,7 +54,7 @@ public DocumentEdits getTextEdit() {

@Override
public String getDetail() {
return this.detail;
return this.coreProposal.getDetail();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.annotations;

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

import org.eclipse.jdt.core.dom.ASTNode;
import org.springframework.ide.vscode.commons.java.IJavaProject;

public interface AnnotationAttributeCompletionProvider {

Map<String, String> getCompletionCandidates(IJavaProject project, ASTNode node);
List<AnnotationAttributeProposal> getCompletionCandidates(IJavaProject project, ASTNode node);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2024 Broadcom
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Broadcom - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.annotations;

/**
* @author Martin Lippert
*/
public class AnnotationAttributeProposal {

private static final String EMPTY_DETAIL = "";

private final String filterText;
private final String label;
private final String detail;

public AnnotationAttributeProposal(String label) {
this(label, label, label);
}

public AnnotationAttributeProposal(String label, String detail) {
this(label, detail, label);
}

public AnnotationAttributeProposal(String label, String detail, String filterText) {
this.label = label;
this.detail = detail == null ? EMPTY_DETAIL : detail;
this.filterText = filterText;
}

public String getLabel() {
return label;
}

public String getDetail() {
return detail;
}

public String getFilterText() {
return filterText;
}

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

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

import org.eclipse.jdt.core.dom.ASTNode;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeProposal;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

Expand All @@ -33,11 +33,12 @@ public BeanNamesCompletionProcessor(SpringMetamodelIndex springIndex) {
}

@Override
public Map<String, String> getCompletionCandidates(IJavaProject project, ASTNode node) {
public List<AnnotationAttributeProposal> getCompletionCandidates(IJavaProject project, ASTNode node) {
Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());
return Arrays.stream(beans)
.map(Bean::getName)
.distinct()
.collect(Collectors.toMap(key -> key, value -> value, (u, v) -> u, LinkedHashMap::new));
.map(beanName -> new AnnotationAttributeProposal(beanName))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
*******************************************************************************/
package org.springframework.ide.vscode.boot.java.beans;

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

import org.eclipse.jdt.core.dom.ASTNode;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeProposal;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

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

/**
* @author Karthik Sankaranarayanan
*/
Expand All @@ -33,11 +33,12 @@ public BeanTypesCompletionProcessor(SpringMetamodelIndex springIndex) {
}

@Override
public Map<String, String> getCompletionCandidates(IJavaProject project, ASTNode node) {
public List<AnnotationAttributeProposal> getCompletionCandidates(IJavaProject project, ASTNode node) {
Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName());
return Arrays.stream(beans)
.map(Bean::getType)
.distinct()
.collect(Collectors.toMap(key -> key, value -> value, (u, v) -> u, LinkedHashMap::new));
.map(beanType -> new AnnotationAttributeProposal(beanType))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
package org.springframework.ide.vscode.boot.java.beans;

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

import org.eclipse.jdt.core.dom.ASTNode;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeProposal;
import org.springframework.ide.vscode.commons.java.IJavaProject;

/**
Expand Down Expand Up @@ -238,11 +238,12 @@ public DependsOnCompletionProcessor(SpringMetamodelIndex springIndex) {
// }

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


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

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

import org.eclipse.jdt.core.dom.ASTNode;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeProposal;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

Expand All @@ -35,15 +35,16 @@ public NamedCompletionProvider(SpringMetamodelIndex springIndex) {
}

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

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

return Stream.concat(
findAllNamedValues(beans),
Arrays.stream(beans).map(bean -> bean.getName()))
.distinct()
.collect(Collectors.toMap(key -> key, value -> value, (u, v) -> u, LinkedHashMap::new));
.map(beanName -> new AnnotationAttributeProposal(beanName))
.collect(Collectors.toList());
}

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

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

import org.eclipse.jdt.core.dom.ASTNode;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeProposal;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

Expand All @@ -35,13 +35,14 @@ public ProfileCompletionProvider(SpringMetamodelIndex springIndex) {
}

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

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

return findAllProfiles(beans)
.distinct()
.collect(Collectors.toMap(key -> key, value -> value, (u, v) -> u, LinkedHashMap::new));
.map(profile -> new AnnotationAttributeProposal(profile))
.collect(Collectors.toList());
}

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

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

Expand All @@ -25,6 +23,7 @@
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.Annotations;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeProposal;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

Expand All @@ -40,7 +39,7 @@ public QualifierCompletionProvider(SpringMetamodelIndex springIndex) {
}

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

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

Expand All @@ -54,7 +53,8 @@ public Map<String, String> getCompletionCandidates(IJavaProject project, ASTNode

return candidates
.distinct()
.collect(Collectors.toMap(key -> key, value -> value, (u, v) -> u, LinkedHashMap::new));
.map(qualifier -> new AnnotationAttributeProposal(qualifier))
.collect(Collectors.toList());
}

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

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

import org.eclipse.jdt.core.dom.ASTNode;
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider;
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeProposal;
import org.springframework.ide.vscode.commons.java.IJavaProject;
import org.springframework.ide.vscode.commons.protocol.spring.Bean;

Expand All @@ -33,13 +33,14 @@ public ResourceCompletionProvider(SpringMetamodelIndex springIndex) {
}

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

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

return Arrays.stream(beans).map(bean -> bean.getName())
.distinct()
.collect(Collectors.toMap(key -> key, value -> value, (u, v) -> u, LinkedHashMap::new));
.map(beanName -> new AnnotationAttributeProposal(beanName))
.collect(Collectors.toList());
}

}
Loading

0 comments on commit 4aeaf05

Please sign in to comment.