Skip to content

Commit

Permalink
Merge pull request oracle#112 from hpi-swa-lab/bugfix/99-use-nested-c…
Browse files Browse the repository at this point in the history
…ontext-per-example

Bugfix: use nested context for each example
  • Loading branch information
Kolpa authored Jan 30, 2020
2 parents e53d7ab + 94da06b commit c7e9d5f
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand All @@ -47,6 +42,7 @@
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.oracle.truffle.api.CallTarget;
import org.graalvm.collections.Pair;
Expand Down Expand Up @@ -154,7 +150,7 @@ public void connect(@SuppressWarnings("hiding") LanguageClient client) {
@Override
public CompletableFuture<CompletionList> completion(CompletionParams position) {
Future<CompletionList> futureCompletionList = truffleAdapter.completion(URI.create(position.getTextDocument().getUri()), position.getPosition().getLine(),
position.getPosition().getCharacter(), position.getContext());
position.getPosition().getCharacter(), position.getContext());
return CompletableFuture.supplyAsync(() -> waitForResultAndHandleExceptions(futureCompletionList, truffleAdapter.completionHandler.emptyList));
}

Expand Down Expand Up @@ -189,7 +185,7 @@ public CompletableFuture<List<? extends Location>> references(ReferenceParams pa
@Override
public CompletableFuture<List<? extends DocumentHighlight>> documentHighlight(TextDocumentPositionParams position) {
Future<List<? extends DocumentHighlight>> future = truffleAdapter.documentHighlight(URI.create(position.getTextDocument().getUri()), position.getPosition().getLine(),
position.getPosition().getCharacter());
position.getPosition().getCharacter());
Supplier<List<? extends DocumentHighlight>> supplier = () -> waitForResultAndHandleExceptions(future, Collections.emptyList());
return CompletableFuture.supplyAsync(supplier);
}
Expand Down Expand Up @@ -284,7 +280,7 @@ public void didChange(DidChangeTextDocumentParams params) {
}

private void processChanges(final String documentUri,
final List<? extends TextDocumentContentChangeEvent> list) {
final List<? extends TextDocumentContentChangeEvent> list) {
String langId = openedFileUri2LangId.get(URI.create(documentUri));
if (langId == null) {
LOG.warning("Changed document that was not opened: " + documentUri);
Expand Down Expand Up @@ -337,41 +333,48 @@ public CompletableFuture<List<? extends SymbolInformation>> symbol(WorkspaceSymb
return CompletableFuture.completedFuture(Collections.emptyList());
}

private List<Decoration> getListOfDecorationsForEvaluatedExample(ExampleDefinition example) {
ArrayList<Decoration> decorations = new ArrayList<>();
for (ProbeDefinition probe : example.getProbes()) {
decorations.add(Decoration.create(
Range.create(
Position.create(probe.getLine(), probe.getStartColumn()),
Position.create(probe.getLine(), probe.getEndColumn())
),
probe.getResult().toString(),
"probeResult"
));
}
decorations.add(Decoration.create(
Range.create(
Position.create(example.getExampleDefinitionLine(), 0),
Position.create(example.getExampleDefinitionLine(), example.getExampleDefinitionEndColumn())
),
example.getExampleResult().toString(),
"exampleResult"
));
return decorations;
}

private void extractAndEvaluateExamples(URI uri, String sourceCode) {
Future<List<ExampleDefinition>> exampleDefinitionsFuture = truffleAdapter.exampleDefinitions(uri, sourceCode);
Supplier<List<ExampleDefinition>> supplier = () -> waitForResultAndHandleExceptions(exampleDefinitionsFuture, new ArrayList<>());
List<ExampleDefinition> exampleDefinitions = supplier.get();

openedFileUri2Examples.put(uri, exampleDefinitions);

Future<List<ExampleDefinition>> evaluatedExamplesFuture = truffleAdapter.evaluateExamplesAndProbes(uri, exampleDefinitions);
Supplier<List<ExampleDefinition>> evaluatedExamplesSupplier = () -> waitForResultAndHandleExceptions(evaluatedExamplesFuture, new ArrayList<>());
List<ExampleDefinition> evaluatedExamples = evaluatedExamplesSupplier.get();
List<CompletableFuture<ExampleDefinition>> futures = new LinkedList<>();

ArrayList<Decoration> decorations = new ArrayList<>();

for (ExampleDefinition example : evaluatedExamples) {
for (ProbeDefinition probe : example.getProbes()) {
decorations.add(Decoration.create(
Range.create(
Position.create(probe.getLine(), probe.getStartColumn()),
Position.create(probe.getLine(), probe.getEndColumn())
),
probe.getResult().toString(),
"probeResult"
));
}
decorations.add(Decoration.create(
Range.create(
Position.create(example.getExampleDefinitionLine(), 0),
Position.create(example.getExampleDefinitionLine(), example.getExampleDefinitionEndColumn())
),
example.getExampleResult().toString(),
"exampleResult"
));
for (ExampleDefinition exampleDefinition : exampleDefinitions) {
futures.add(CompletableFuture.supplyAsync(() -> waitForResultAndHandleExceptions(truffleAdapter.evaluateExampleAndProbes(uri, exampleDefinition))));
}

client.publishDecorations(PublishDecorationsParams.create(uri.toString(), decorations));
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply(ignored -> futures.stream()
.map(CompletableFuture::join)
.flatMap(example -> getListOfDecorationsForEvaluatedExample(example).stream()).collect(Collectors.toList())
)
.thenAccept(decorations -> client.publishDecorations(PublishDecorationsParams.create(uri.toString(), decorations)));
}

private String buildExampleStringFromArgs(Boolean functionAlreadyHasExamples, int indexOfNewlyCreatedExamples, JSONObject inputMappingObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ public Future<List<ExampleDefinition>> evaluateExamplesAndProbes(URI uri, List<E
return contextAwareExecutor.executeWithNestedContext(() -> sourceCodeEvaluator.evaluateExamplesAndProbes(uri, examples));
}

public Future<ExampleDefinition> evaluateExampleAndProbes(URI uri, ExampleDefinition example) {
return contextAwareExecutor.executeWithNestedContext(() -> sourceCodeEvaluator.evaluateExampleAndProbes(uri, example));
}

/**
* Special handling needed, because some LSP clients send a MIME type as langId.
*
Expand Down
Loading

0 comments on commit c7e9d5f

Please sign in to comment.