-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1298: added bean name code completion and navigation support for n…
…ame attribute of resource annotation
- Loading branch information
1 parent
6b0bd5d
commit bc5006f
Showing
7 changed files
with
478 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
.../main/java/org/springframework/ide/vscode/boot/java/beans/ResourceCompletionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/******************************************************************************* | ||
* 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.beans; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex; | ||
import org.springframework.ide.vscode.boot.java.annotations.AnnotationAttributeCompletionProvider; | ||
import org.springframework.ide.vscode.commons.java.IJavaProject; | ||
import org.springframework.ide.vscode.commons.protocol.spring.Bean; | ||
|
||
/** | ||
* @author Martin Lippert | ||
*/ | ||
public class ResourceCompletionProvider implements AnnotationAttributeCompletionProvider { | ||
|
||
private final SpringMetamodelIndex springIndex; | ||
|
||
public ResourceCompletionProvider(SpringMetamodelIndex springIndex) { | ||
this.springIndex = springIndex; | ||
} | ||
|
||
@Override | ||
public List<String> getCompletionCandidates(IJavaProject project) { | ||
|
||
Bean[] beans = this.springIndex.getBeansOfProject(project.getElementName()); | ||
|
||
return Arrays.stream(beans).map(bean -> bean.getName()) | ||
.distinct() | ||
.toList(); | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
.../main/java/org/springframework/ide/vscode/boot/java/beans/ResourceDefinitionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/******************************************************************************* | ||
* 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.beans; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.Annotation; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.IAnnotationBinding; | ||
import org.eclipse.jdt.core.dom.StringLiteral; | ||
import org.eclipse.lsp4j.LocationLink; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex; | ||
import org.springframework.ide.vscode.boot.java.Annotations; | ||
import org.springframework.ide.vscode.boot.java.IJavaDefinitionProvider; | ||
import org.springframework.ide.vscode.boot.java.utils.ASTUtils; | ||
import org.springframework.ide.vscode.commons.java.IJavaProject; | ||
import org.springframework.ide.vscode.commons.protocol.spring.Bean; | ||
|
||
/** | ||
* @author Martin Lippert | ||
*/ | ||
public class ResourceDefinitionProvider implements IJavaDefinitionProvider { | ||
|
||
private final SpringMetamodelIndex springIndex; | ||
|
||
public ResourceDefinitionProvider(SpringMetamodelIndex springIndex) { | ||
this.springIndex = springIndex; | ||
} | ||
|
||
@Override | ||
public List<LocationLink> getDefinitions(CancelChecker cancelToken, IJavaProject project, CompilationUnit cu, ASTNode n) { | ||
if (n instanceof StringLiteral) { | ||
StringLiteral valueNode = (StringLiteral) n; | ||
|
||
ASTNode parent = ASTUtils.getNearestAnnotationParent(valueNode); | ||
|
||
if (parent != null && parent instanceof Annotation) { | ||
Annotation a = (Annotation) parent; | ||
IAnnotationBinding binding = a.resolveAnnotationBinding(); | ||
if (binding != null && binding.getAnnotationType() != null | ||
&& (Annotations.RESOURCE_JAVAX.equals(binding.getAnnotationType().getQualifiedName()) | ||
|| Annotations.RESOURCE_JAKARTA.equals(binding.getAnnotationType().getQualifiedName()))) { | ||
String beanName = valueNode.getLiteralValue(); | ||
|
||
if (beanName != null && beanName.length() > 0) { | ||
return findBeansWithName(project, beanName); | ||
} | ||
} | ||
} | ||
} | ||
return Collections.emptyList(); | ||
} | ||
|
||
private List<LocationLink> findBeansWithName(IJavaProject project, String beanName) { | ||
Bean[] beans = this.springIndex.getBeansWithName(project.getElementName(), beanName); | ||
|
||
return Arrays.stream(beans) | ||
.map(bean -> { | ||
return new LocationLink(bean.getLocation().getUri(), bean.getLocation().getRange(), bean.getLocation().getRange()); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
Oops, something went wrong.