|
10 | 10 | *******************************************************************************/ |
11 | 11 | package org.springframework.ide.vscode.boot.java.data; |
12 | 12 |
|
| 13 | +import java.util.ArrayList; |
13 | 14 | import java.util.Arrays; |
14 | 15 | import java.util.List; |
15 | 16 | import java.util.Map; |
|
25 | 26 | import org.eclipse.jdt.core.dom.MethodDeclaration; |
26 | 27 | import org.eclipse.lsp4j.CodeLens; |
27 | 28 | import org.eclipse.lsp4j.Command; |
| 29 | +import org.eclipse.lsp4j.Range; |
28 | 30 | import org.eclipse.lsp4j.jsonrpc.CancelChecker; |
29 | 31 | import org.slf4j.Logger; |
30 | 32 | import org.slf4j.LoggerFactory; |
|
44 | 46 | */ |
45 | 47 | public class DataRepositoryAotMetadataCodeLensProvider implements CodeLensProvider { |
46 | 48 |
|
| 49 | + private static final String COVERT_TO_QUERY_LABEL = "Add @Query"; |
| 50 | + |
47 | 51 | private static final Logger log = LoggerFactory.getLogger(DataRepositoryAotMetadataCodeLensProvider.class); |
48 | 52 |
|
49 | 53 | private final DataRepositoryAotMetadataService repositoryMetadataService; |
@@ -113,31 +117,29 @@ protected void provideCodeLens(CancelChecker cancelToken, MethodDeclaration node |
113 | 117 |
|
114 | 118 | if (isDataQuaryNonAnnotatedMethodCandidate(methodBinding)) { |
115 | 119 | cancelToken.checkCanceled(); |
116 | | - getDataQuery(repositoryMetadataService, project, methodBinding).map(queryStatement -> createCodeLens(node, document, queryStatement)).ifPresent(resultAccumulator::add); |
| 120 | + getDataQuery(repositoryMetadataService, project, methodBinding) |
| 121 | + .map(queryStatement -> createCodeLenses(node, document, queryStatement)) |
| 122 | + .ifPresent(cls -> cls.forEach(resultAccumulator::add)); |
117 | 123 | } |
118 | 124 | } |
119 | | - |
120 | | - private CodeLens createCodeLens(MethodDeclaration node, TextDocument document, String queryStatement) { |
| 125 | + |
| 126 | + private List<CodeLens> createCodeLenses(MethodDeclaration node, TextDocument document, String queryStatement) { |
| 127 | + List<CodeLens> codeLenses = new ArrayList<>(2); |
121 | 128 | try { |
122 | 129 | IMethodBinding mb = node.resolveBinding(); |
123 | | - Command cmd = new Command(); |
124 | | - cmd.setTitle(queryStatement); |
| 130 | + Range range = document.toRange(node.getName().getStartPosition(), node.getName().getLength()); |
| 131 | + Command queryTitle = new Command(); |
| 132 | + queryTitle.setTitle(queryStatement); |
| 133 | + codeLenses.add(new CodeLens(range, queryTitle, null)); |
125 | 134 | if (mb != null) { |
126 | | - cmd = refactorings.createFixCommand(queryStatement, createFixDescriptor(mb, document.getUri(), queryStatement)); |
| 135 | + codeLenses.add(new CodeLens(range, refactorings.createFixCommand(COVERT_TO_QUERY_LABEL, createFixDescriptor(mb, document.getUri(), queryStatement)), null)); |
127 | 136 | } |
128 | | - |
129 | | - CodeLens codeLens = new CodeLens(); |
130 | | - codeLens.setRange(document.toRange(node.getName().getStartPosition(), node.getName().getLength())); |
131 | | - codeLens.setCommand(cmd); |
132 | | - |
133 | | - return codeLens; |
134 | | - |
135 | 137 | } catch (BadLocationException e) { |
136 | 138 | log.error("bad location while calculating code lens for data repository query method", e); |
137 | | - return null; |
138 | 139 | } |
| 140 | + return codeLenses; |
139 | 141 | } |
140 | | - |
| 142 | + |
141 | 143 | static FixDescriptor createFixDescriptor(IMethodBinding mb, String docUri, String queryStatement) { |
142 | 144 | return new FixDescriptor(AddAnnotationOverMethod.class.getName(), List.of(docUri), "Convert into `@Query`") |
143 | 145 | .withRecipeScope(RecipeScope.FILE) |
|
0 commit comments