-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1491: index query methods and show them as child nodes for reposit…
…ory document symbols, incl. query strings when defined
- Loading branch information
1 parent
dc29abe
commit e7eb27c
Showing
8 changed files
with
322 additions
and
15 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
63 changes: 63 additions & 0 deletions
63
.../src/main/java/org/springframework/ide/vscode/boot/java/data/QueryMethodIndexElement.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,63 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2025 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.data; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lsp4j.DocumentSymbol; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4j.SymbolKind; | ||
import org.springframework.ide.vscode.commons.protocol.spring.AbstractSpringIndexElement; | ||
import org.springframework.ide.vscode.commons.protocol.spring.SymbolElement; | ||
|
||
public class QueryMethodIndexElement extends AbstractSpringIndexElement implements SymbolElement { | ||
|
||
private final String methodName; | ||
private final String queryString; | ||
private final Range range; | ||
|
||
public QueryMethodIndexElement(String methodName, String queryString, Range range) { | ||
this.methodName = methodName; | ||
this.queryString = queryString; | ||
this.range = range; | ||
} | ||
|
||
public String getMethodName() { | ||
return methodName; | ||
} | ||
|
||
public String getQueryString() { | ||
return queryString; | ||
} | ||
|
||
@Override | ||
public DocumentSymbol getDocumentSymbol() { | ||
DocumentSymbol symbol = new DocumentSymbol(); | ||
|
||
symbol.setName(methodName); | ||
symbol.setKind(SymbolKind.Method); | ||
symbol.setRange(range); | ||
symbol.setSelectionRange(range); | ||
|
||
if (queryString != null) { | ||
DocumentSymbol querySymbol = new DocumentSymbol(); | ||
querySymbol.setName(queryString); | ||
querySymbol.setKind(SymbolKind.Constant); | ||
querySymbol.setRange(range); | ||
querySymbol.setSelectionRange(range); | ||
|
||
symbol.setChildren(List.of(querySymbol)); | ||
} | ||
|
||
return symbol; | ||
} | ||
|
||
} |
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
121 changes: 121 additions & 0 deletions
121
...a/org/springframework/ide/vscode/boot/java/data/test/DataRepositoryIndexElementsTest.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,121 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2025 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.data.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.eclipse.lsp4j.TextDocumentIdentifier; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.ide.vscode.boot.app.SpringSymbolIndex; | ||
import org.springframework.ide.vscode.boot.bootiful.BootLanguageServerTest; | ||
import org.springframework.ide.vscode.boot.bootiful.SymbolProviderTestConf; | ||
import org.springframework.ide.vscode.boot.index.SpringMetamodelIndex; | ||
import org.springframework.ide.vscode.boot.java.data.QueryMethodIndexElement; | ||
import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; | ||
import org.springframework.ide.vscode.commons.protocol.spring.Bean; | ||
import org.springframework.ide.vscode.commons.protocol.spring.DocumentElement; | ||
import org.springframework.ide.vscode.commons.protocol.spring.SpringIndexElement; | ||
import org.springframework.ide.vscode.project.harness.BootLanguageServerHarness; | ||
import org.springframework.ide.vscode.project.harness.ProjectsHarness; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
/** | ||
* @author Martin Lippert | ||
*/ | ||
@ExtendWith(SpringExtension.class) | ||
@BootLanguageServerTest | ||
@Import(SymbolProviderTestConf.class) | ||
public class DataRepositoryIndexElementsTest { | ||
|
||
@Autowired private BootLanguageServerHarness harness; | ||
@Autowired private JavaProjectFinder projectFinder; | ||
@Autowired private SpringSymbolIndex indexer; | ||
@Autowired private SpringMetamodelIndex springIndex; | ||
|
||
private File directory; | ||
|
||
@BeforeEach | ||
public void setup() throws Exception { | ||
harness.intialize(null); | ||
|
||
directory = new File(ProjectsHarness.class.getResource("/test-projects/test-spring-data-symbols/").toURI()); | ||
String projectDir = directory.toURI().toString(); | ||
|
||
// trigger project creation | ||
projectFinder.find(new TextDocumentIdentifier(projectDir)).get(); | ||
|
||
CompletableFuture<Void> initProject = indexer.waitOperation(); | ||
initProject.get(5, TimeUnit.SECONDS); | ||
} | ||
|
||
@Test | ||
void testSimpleRepositoryElements() throws Exception { | ||
String docUri = directory.toPath().resolve("src/main/java/org/test/CustomerRepository.java").toUri().toString(); | ||
|
||
DocumentElement document = springIndex.getDocument(docUri); | ||
List<SpringIndexElement> children = document.getChildren(); | ||
Bean repositoryElement = (Bean) children.get(0); | ||
assertEquals("customerRepository", repositoryElement.getName()); | ||
assertEquals(1, children.size()); | ||
|
||
Bean[] repoBean = this.springIndex.getBeansWithName("test-spring-data-symbols", "customerRepository"); | ||
assertEquals(1, repoBean.length); | ||
assertEquals("customerRepository", repoBean[0].getName()); | ||
assertEquals("org.test.CustomerRepository", repoBean[0].getType()); | ||
|
||
Bean[] matchingBeans = springIndex.getMatchingBeans("test-spring-data-symbols", "org.springframework.data.repository.CrudRepository"); | ||
assertEquals(3, matchingBeans.length); | ||
ArrayUtils.contains(matchingBeans, repoBean[0]); | ||
} | ||
|
||
@Test | ||
void testSimpleQueryMethodElements() throws Exception { | ||
String docUri = directory.toPath().resolve("src/main/java/org/test/CustomerRepository.java").toUri().toString(); | ||
|
||
DocumentElement document = springIndex.getDocument(docUri); | ||
List<SpringIndexElement> children = document.getChildren(); | ||
Bean repositoryElement = (Bean) children.get(0); | ||
|
||
List<SpringIndexElement> queryMethods = repositoryElement.getChildren(); | ||
assertEquals(1, queryMethods.size()); | ||
|
||
QueryMethodIndexElement queryMethod = (QueryMethodIndexElement) queryMethods.get(0); | ||
assertEquals("findByLastName", queryMethod.getMethodName()); | ||
} | ||
|
||
@Test | ||
void testQueryMethodElementWithQueryString() throws Exception { | ||
String docUri = directory.toPath().resolve("src/main/java/org/test/CustomerRepositoryWithQuery.java").toUri().toString(); | ||
|
||
DocumentElement document = springIndex.getDocument(docUri); | ||
List<SpringIndexElement> children = document.getChildren(); | ||
Bean repositoryElement = (Bean) children.get(0); | ||
|
||
List<SpringIndexElement> queryMethods = repositoryElement.getChildren(); | ||
assertEquals(1, queryMethods.size()); | ||
|
||
QueryMethodIndexElement queryMethod = (QueryMethodIndexElement) queryMethods.get(0); | ||
assertEquals("findPetTypes", queryMethod.getMethodName()); | ||
assertEquals("SELECT ptype FROM PetType ptype ORDER BY ptype.name", queryMethod.getQueryString()); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.