Skip to content

Commit 1dd1058

Browse files
datho7561angelozerr
authored andcommitted
Use computeModelAsyncCompose from quarkus-ls
This method makes it easier to implement go to definition in properties files. Includes the recent bug fix to the method (see redhat-developer/quarkus-ls#679) Closes #257 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 944f548 commit 1dd1058

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/ls/commons/ModelTextDocuments.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515

1616
import java.util.concurrent.CompletableFuture;
1717
import java.util.function.BiFunction;
18+
import java.util.function.Function;
1819

1920
import org.eclipse.lsp4j.TextDocumentIdentifier;
2021
import org.eclipse.lsp4j.TextDocumentItem;
2122
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
2223
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;
24+
import org.eclipse.lsp4j.jsonrpc.CompletableFutures.FutureCancelChecker;
2325

2426
/**
2527
* The cache of {@link TextDocument} linked to a model.
@@ -94,16 +96,16 @@ public T getModel(String uri) {
9496
}
9597
return null;
9698
}
97-
99+
98100
/**
99101
* Get or parse the model and apply the code function which expects the model.
100102
*
101103
* @param <R>
102-
* @param documentIdentifier the document indentifier.
103-
* @param code a bi function that accepts the parsedmodel and
104+
* @param documentIdentifier the document identifier.
105+
* @param code a bi function that accepts the parsed model and
104106
* {@link CancelChecker} and returns the to be
105107
* computed value
106-
* @return the DOM Document for a given uri in a future and then apply the given
108+
* @return the model for a given uri in a future and then apply the given
107109
* function.
108110
*/
109111
public <R> CompletableFuture<R> computeModelAsync(TextDocumentIdentifier documentIdentifier,
@@ -119,4 +121,36 @@ public <R> CompletableFuture<R> computeModelAsync(TextDocumentIdentifier documen
119121
return code.apply(model, cancelChecker);
120122
});
121123
}
124+
125+
/**
126+
* Get or parse the model and apply the code function which expects the model.
127+
*
128+
* @param <R>
129+
* @param documentIdentifier the document identifier.
130+
* @param code a bi function that accepts the parsed model and
131+
* {@link CancelChecker} and returns as future the to
132+
* be computed value
133+
* @return the model for a given uri in a future and then apply the given
134+
* function.
135+
*/
136+
public <R> CompletableFuture<R> computeModelAsyncCompose(TextDocumentIdentifier documentIdentifier,
137+
BiFunction<T, CancelChecker, CompletableFuture<R>> code) {
138+
return computeAsyncCompose(cancelChecker -> {
139+
// Get or parse the model.
140+
T model = getModel(documentIdentifier);
141+
if (model == null) {
142+
return null;
143+
}
144+
cancelChecker.checkCanceled();
145+
// Apply the function code by using the parsed model.{
146+
return code.apply(model, cancelChecker);
147+
});
148+
}
149+
150+
private static <R> CompletableFuture<R> computeAsyncCompose(Function<CancelChecker, CompletableFuture<R>> code) {
151+
CompletableFuture<CancelChecker> start = new CompletableFuture<>();
152+
CompletableFuture<R> result = start.thenComposeAsync(code);
153+
start.complete(new FutureCancelChecker(result));
154+
return result;
155+
}
122156
}

microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/ls/java/JavaTextDocuments.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import java.util.concurrent.CompletableFuture;
2020
import java.util.concurrent.ConcurrentHashMap;
2121
import java.util.function.BiFunction;
22+
import java.util.function.Function;
2223
import java.util.logging.Level;
2324
import java.util.logging.Logger;
2425

2526
import org.eclipse.lsp4j.TextDocumentItem;
2627
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
2728
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;
29+
import org.eclipse.lsp4j.jsonrpc.CompletableFutures.FutureCancelChecker;
2830
import org.eclipse.lsp4j.jsonrpc.messages.Tuple;
2931
import org.eclipse.lsp4mp.commons.JavaFileInfo;
3032
import org.eclipse.lsp4mp.commons.MicroProfileJavaFileInfoParams;
@@ -126,13 +128,9 @@ public void setProjectURI(String projectURI) {
126128
* project.
127129
*/
128130
public <T> CompletableFuture<T> executeIfInMicroProfileProject(
129-
BiFunction<ProjectLabelInfoEntry, CancelChecker, CompletableFuture<T>> code, T defaultValue) {
130-
return CompletableFutures.computeAsync((cancelChecker) -> {
131+
BiFunction<ProjectLabelInfoEntry, CancelChecker, CompletableFuture<T>> code, T defaultValue) {
132+
return computeAsyncCompose(cancelChecker -> {
131133
ProjectLabelInfoEntry projectInfo = getProjectInfo(this).getNow(null);
132-
return Tuple.two(projectInfo, cancelChecker);
133-
}).thenCompose((tuple) -> {
134-
ProjectLabelInfoEntry projectInfo = tuple.getFirst();
135-
CancelChecker cancelChecker = tuple.getSecond();
136134
cancelChecker.checkCanceled();
137135
if (projectInfo == null || !isMicroProfileProject(projectInfo)) {
138136
return CompletableFuture.completedFuture(defaultValue);
@@ -261,4 +259,11 @@ public JavaTextDocumentSnippetRegistry getSnippetRegistry() {
261259
}
262260
return snippetRegistry;
263261
}
262+
263+
private static <R> CompletableFuture<R> computeAsyncCompose(Function<CancelChecker, CompletableFuture<R>> code) {
264+
CompletableFuture<CancelChecker> start = new CompletableFuture<>();
265+
CompletableFuture<R> result = start.thenComposeAsync(code);
266+
start.complete(new FutureCancelChecker(result));
267+
return result;
268+
}
264269
}

microprofile.ls/org.eclipse.lsp4mp.ls/src/main/java/org/eclipse/lsp4mp/ls/properties/PropertiesFileTextDocumentService.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,14 @@ public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> docume
186186
@Override
187187
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(
188188
DefinitionParams params) {
189-
return getPropertiesModel(params.getTextDocument(), (document, cancelChecker) -> {
189+
return getPropertiesModelCompose(params.getTextDocument(), (document, cancelChecker) -> {
190190
MicroProfileProjectInfoParams projectInfoParams = createProjectInfoParams(params.getTextDocument());
191191
MicroProfileProjectInfo projectInfo = getProjectInfoCache().getProjectInfo(projectInfoParams).getNow(null);
192192
if (projectInfo == null || projectInfo.getProperties().isEmpty()) {
193193
return null;
194194
}
195-
return Tuple.two(Tuple.two(document, projectInfo), cancelChecker);
196-
}).thenCompose((modelAndProjectInfoAndCancelChecker) -> {
197-
return getPropertiesFileLanguageService().findDefinition(
198-
modelAndProjectInfoAndCancelChecker.getFirst().getFirst(),
199-
params.getPosition(), modelAndProjectInfoAndCancelChecker.getFirst().getSecond(),
200-
microprofileLanguageServer.getLanguageClient(), isDefinitionLinkSupport(),
201-
modelAndProjectInfoAndCancelChecker.getSecond());
195+
return getPropertiesFileLanguageService().findDefinition(document, params.getPosition(), projectInfo,
196+
microprofileLanguageServer.getLanguageClient(), isDefinitionLinkSupport(), cancelChecker);
202197
});
203198
}
204199

@@ -230,7 +225,8 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
230225
}
231226
return getPropertiesFileLanguageService()
232227
.doCodeActions(params.getContext(), params.getRange(), document, projectInfo,
233-
sharedSettings.getFormattingSettings(), sharedSettings.getCommandCapabilities(), cancelChecker) //
228+
sharedSettings.getFormattingSettings(), sharedSettings.getCommandCapabilities(),
229+
cancelChecker) //
234230
.stream() //
235231
.map(ca -> {
236232
Either<Command, CodeAction> e = Either.forRight(ca);
@@ -243,7 +239,8 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
243239
@Override
244240
public CompletableFuture<List<? extends DocumentHighlight>> documentHighlight(DocumentHighlightParams params) {
245241
return getPropertiesModel(params.getTextDocument(), (document, cancelChecker) -> {
246-
return getPropertiesFileLanguageService().findDocumentHighlight(document, params.getPosition(), cancelChecker);
242+
return getPropertiesFileLanguageService().findDocumentHighlight(document, params.getPosition(),
243+
cancelChecker);
247244
});
248245
}
249246

@@ -405,4 +402,9 @@ public PropertiesModel getPropertiesModel(String documentURI) {
405402
return null;
406403
}
407404

405+
public <R> CompletableFuture<R> getPropertiesModelCompose(TextDocumentIdentifier documentIdentifier,
406+
BiFunction<PropertiesModel, CancelChecker, CompletableFuture<R>> code) {
407+
return documents.computeModelAsyncCompose(documentIdentifier, code);
408+
}
409+
408410
}

0 commit comments

Comments
 (0)