Skip to content

Commit b7847fb

Browse files
committed
Adjust AOT CodeLense for Refresh metadata
1 parent 6b40c3c commit b7847fb

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private List<CodeLens> createCodeLenses(IJavaProject project, MethodDeclaration
145145
if (mb != null && hierarchyAnnot != null) {
146146

147147
Optional<DataRepositoryAotMetadata> optMetadata = getMetadata(repositoryMetadataService, project, mb);
148-
optMetadata.ifPresent(metadata -> metadata.findMethod(mb).ifPresent(methodMetadata -> {
148+
optMetadata.ifPresentOrElse(metadata -> metadata.findMethod(mb).ifPresent(methodMetadata -> {
149149
boolean isQueryAnnotated = hierarchyAnnot.isAnnotatedWith(mb, Annotations.DATA_JPA_QUERY)
150150
|| hierarchyAnnot.isAnnotatedWith(mb, Annotations.DATA_MONGODB_QUERY);
151151

@@ -170,21 +170,28 @@ private List<CodeLens> createCodeLenses(IJavaProject project, MethodDeclaration
170170
codeLenses.add(new CodeLens(range, queryTitle, null));
171171
}
172172
}
173-
}));
173+
174+
createRefreshCodeLens(project, "Refresh AOT Metadata", range).ifPresent(codeLenses::add);
175+
}), () -> createRefreshCodeLens(project, "Show AOT-generated Implementation, Query, etc...", range)
176+
.ifPresent(codeLenses::add)
177+
);
174178

175-
if (ProjectBuild.MAVEN_PROJECT_TYPE.equals(project.getProjectBuild().getType())) {
176-
String refreshCmdTitle = optMetadata.map(m -> "Refresh").orElse("Show AOT-generated Implementation, Query, etc...");
177-
Command refreshCmd = repositoryMetadataService.regenerateMetadataCommand(project);
178-
refreshCmd.setTitle(refreshCmdTitle);
179-
codeLenses.add(new CodeLens(range, refreshCmd, null));
180-
}
181179

182180
}
183181
} catch (BadLocationException e) {
184182
log.error("bad location while calculating code lens for data repository query method", e);
185183
}
186184
return codeLenses;
187185
}
186+
187+
private Optional<CodeLens> createRefreshCodeLens(IJavaProject project, String title, Range range) {
188+
if (ProjectBuild.MAVEN_PROJECT_TYPE.equals(project.getProjectBuild().getType())) {
189+
Command refreshCmd = repositoryMetadataService.regenerateMetadataCommand(project);
190+
refreshCmd.setTitle(title);
191+
return Optional.of(new CodeLens(range, refreshCmd, null));
192+
}
193+
return Optional.empty();
194+
}
188195

189196
static FixDescriptor createFixDescriptor(IMethodBinding mb, String docUri, DataRepositoryModule module, IDataRepositoryAotMethodMetadata methodMetadata) {
190197
return new FixDescriptor(AddAnnotationOverMethod.class.getName(), List.of(docUri), "Turn into `@Query`")

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void noCodeLensOverMethodWithQueryAnnotation() throws Exception {
9191
assertEquals(2, cls.size());
9292
assertEquals("Implementation", cls.get(0).getCommand().getTitle());
9393
assertEquals(1, cls.get(0).getCommand().getArguments().size());
94-
assertEquals("Refresh", cls.get(1).getCommand().getTitle());
94+
assertEquals("Refresh AOT Metadata", cls.get(1).getCommand().getTitle());
9595
assertEquals(2, cls.get(1).getCommand().getArguments().size());
9696
}
9797
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void noCodeLensOverMethodWithQueryAnnotation() throws Exception {
103103
assertEquals(2, cls.size());
104104
assertEquals("Implementation", cls.get(0).getCommand().getTitle());
105105
assertEquals(1, cls.get(0).getCommand().getArguments().size());
106-
assertEquals("Refresh", cls.get(1).getCommand().getTitle());
106+
assertEquals("Refresh AOT Metadata", cls.get(1).getCommand().getTitle());
107107
assertEquals(2, cls.get(1).getCommand().getArguments().size());
108108
}
109109
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ void generateMetadataCodeLens() throws Exception {
8282
assertEquals(1, cls.size());
8383
Command cmd = cls.get(0).getCommand();
8484
assertEquals("Show AOT-generated Implementation, Query, etc...", cmd.getTitle());
85+
cls = editor.getCodeLenses("findAll", 1);
86+
assertEquals(1, cls.size());
87+
cmd = cls.get(0).getCommand();
88+
assertEquals("Show AOT-generated Implementation, Query, etc...", cmd.getTitle());
8589

8690
harness.getServer().getWorkspaceService().executeCommand(new ExecuteCommandParams(cmd.getCommand(), cmd.getArguments())).get();
8791

@@ -97,7 +101,10 @@ void generateMetadataCodeLens() throws Exception {
97101
assertEquals("Turn into @Query", cls.get(0).getCommand().getTitle());
98102
assertEquals("Implementation", cls.get(1).getCommand().getTitle());
99103
assertEquals("SELECT u FROM users u WHERE u.username = :username", cls.get(2).getCommand().getTitle());
100-
assertEquals("Refresh", cls.get(3).getCommand().getTitle());
104+
assertEquals("Refresh AOT Metadata", cls.get(3).getCommand().getTitle());
105+
106+
cls = editor.getCodeLenses("findAll", 1);
107+
assertTrue(cls.isEmpty(), "Node CodeLens expected. Not even `Refresh AOT Metadata'");
101108
}
102109

103110
}

headless-services/spring-boot-language-server/src/test/resources/test-projects/data-repositories-jpa-4/src/main/java/example/springdata/aot/UserRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,6 @@ public interface UserRepository extends CrudRepository<User, String>, QuerydslPr
5454
@Query("SELECT u FROM example.springdata.aot.User u WHERE u.username LIKE ?1%")
5555
List<User> usersWithUsernamesStartingWith(String username);
5656

57+
List<User> findAll();
58+
5759
}

0 commit comments

Comments
 (0)