Skip to content

Commit

Permalink
Simplified the Parametric Language contributions to stop using the in…
Browse files Browse the repository at this point in the history
…terruptable futures and use the calls directly
  • Loading branch information
DavyLandman committed Oct 13, 2021
1 parent 98a56e3 commit dda9d48
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import java.util.concurrent.CompletableFuture;
import org.rascalmpl.values.parsetrees.ITree;
import org.rascalmpl.vscode.lsp.util.concurrent.InterruptibleFuture;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
Expand All @@ -39,8 +38,8 @@ public interface ILanguageContributions {
public String getName();
public String getExtension();
public CompletableFuture<ITree> parseSourceFile(ISourceLocation loc, String input);
public InterruptibleFuture<IList> outline(ITree input);
public InterruptibleFuture<IConstructor> summarize(ISourceLocation loc, ITree input);
public InterruptibleFuture<ISet> lenses(ITree input);
public InterruptibleFuture<Void> executeCommand(String command);
public CompletableFuture<IList> outline(ITree input);
public CompletableFuture<IConstructor> summarize(ISourceLocation loc, ITree input);
public CompletableFuture<ISet> lenses(ITree input);
public CompletableFuture<Void> executeCommand(String command);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.rascalmpl.vscode.lsp.parametric.model.ParametricSummaryBridge;
import org.rascalmpl.vscode.lsp.terminal.ITerminalIDEServer.LanguageParameter;
import org.rascalmpl.vscode.lsp.util.EvaluatorUtil;
import org.rascalmpl.vscode.lsp.util.concurrent.InterruptibleFuture;
import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.ISet;
Expand All @@ -69,6 +68,7 @@ public class InterpretedLanguageContributions implements ILanguageContributions
private final String mainModule;

private final CompletableFuture<Evaluator> eval;
private final CompletableFuture<TypeStore> store;
private final CompletableFuture<IFunction> parser;
private final CompletableFuture<@Nullable IFunction> outliner;
private final CompletableFuture<@Nullable IFunction> summarizer;
Expand All @@ -91,6 +91,7 @@ public InterpretedLanguageContributions(LanguageParameter lang, IBaseTextDocumen
e -> loadContributions(e, lang),
ValueFactoryFactory.getValueFactory().set(),
exec).get();
this.store = eval.thenApply(e -> ((ModuleEnvironment)e.getModule(mainModule)).getStore());
this.parser = contributions.thenApply(s -> getFunctionFor(s, "parser"));
this.outliner = contributions.thenApply(s -> getFunctionFor(s, "outliner"));
this.summarizer = contributions.thenApply(s -> getFunctionFor(s, "summarizer"));
Expand All @@ -107,14 +108,14 @@ private static ISet loadContributions(Evaluator eval, LanguageParameter lang) {
.getValue();
}

private IConstructor parseCommand(Evaluator eval, String command) {
TypeStore store = ((ModuleEnvironment) eval.getModule(mainModule)).getStore();

try {
return (IConstructor) new StandardTextReader().read(VF, store, store.lookupAbstractDataType("Command"), new StringReader(command));
} catch (FactTypeUseException | IOException e) {
throw new RuntimeException(e);
}
private CompletableFuture<IConstructor> parseCommand(String command) {
return store.thenApply(commandStore -> {
try {
return (IConstructor) new StandardTextReader().read(VF, commandStore, commandStore.lookupAbstractDataType("Command"), new StringReader(command));
} catch (FactTypeUseException | IOException e) {
throw new RuntimeException(e);
}
});
}

private static @Nullable IFunction getFunctionFor(ISet contributions, String cons) {
Expand Down Expand Up @@ -144,42 +145,46 @@ public CompletableFuture<ITree> parseSourceFile(ISourceLocation loc, String inpu
}

@Override
public InterruptibleFuture<IList> outline(ITree input) {
public CompletableFuture<IList> outline(ITree input) {
return execFunction("outline",outliner, VF.list(), input);
}

@Override
public InterruptibleFuture<IConstructor> summarize(ISourceLocation src, ITree input) {
public CompletableFuture<IConstructor> summarize(ISourceLocation src, ITree input) {
logger.trace("summarize({})", src);
return execFunction("summarize", summarizer,
ParametricSummaryBridge.emptySummary(src), src, input);
}

@Override
public InterruptibleFuture<ISet> lenses(ITree input) {
public CompletableFuture<ISet> lenses(ITree input) {
logger.trace("lensen({})", TreeAdapter.getLocation(input));
return execFunction("lenses", lenses, VF.set(), input);
}

@Override
public InterruptibleFuture<Void> executeCommand(String command) {
public CompletableFuture<Void> executeCommand(String command) {
logger.trace("executeCommand({})", command);
return new InterruptibleFuture<>(eval.thenApply(e -> execFunction("executor", commandExecutor, null, parseCommand(e, command)))
return parseCommand(command)
.thenCombineAsync(commandExecutor, (c,e) -> {
if (e != null) {
e.call(c);
}
return null;
},exec)
.handle((r,e) -> {
logger.catching(e);
return null;
}), () -> {});
});
}

private <T> InterruptibleFuture<T> execFunction(String name, CompletableFuture<@Nullable IFunction> target, T defaultResult, IValue... args) {
return InterruptibleFuture.flatten(
target.thenApply(s -> {
private <T> CompletableFuture<T> execFunction(String name, CompletableFuture<@Nullable IFunction> target, T defaultResult, IValue... args) {
return target.thenApplyAsync(s -> {
if (s == null) {
logger.trace("Not running {} since it's not defined for: {}", name, this.name);
return InterruptibleFuture.completedFuture(defaultResult);
return defaultResult;
}
return EvaluatorUtil.runEvaluator(name, eval,e -> s.call(args), defaultResult, exec);
})
, exec);
return s.call(args);
}, exec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,9 @@ else if (excp != null) {
public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) {
final TextDocumentState file = getFile(params.getTextDocument());
final ILanguageContributions contrib = contributions(params.getTextDocument());
InterruptibleFuture<ISet> result = InterruptibleFuture.flatten(
file.getCurrentTreeAsync()
.thenApply(contrib::lenses), ownExecuter
);

return result.get()
return file.getCurrentTreeAsync()
.thenCompose(contrib::lenses)
.thenApply(s -> s.stream()
.map(e -> locCommandTupleToCodeLense(contrib.getExtension(), e))
.collect(Collectors.toList())
Expand Down Expand Up @@ -392,8 +389,7 @@ public CompletableFuture<SemanticTokens> semanticTokensRange(SemanticTokensRange
final TextDocumentState file = getFile(params.getTextDocument());
ILanguageContributions contrib = contributions(params.getTextDocument());
return file.getCurrentTreeAsync()
.thenApply(contrib::outline) // TODO: store this interruptible future and also replace it
.thenCompose(InterruptibleFuture::get)
.thenCompose(contrib::outline) // TODO: store this interruptible future and also replace it
.thenApply(c -> Outline.buildOutline(c, columns.get(file.getLocation())))
.exceptionally(e -> {
logger.catching(e);
Expand Down Expand Up @@ -475,7 +471,7 @@ public CompletableFuture<Void> executeCommand(String extension, String command)
ILanguageContributions contribs = contributions.get(extension);

if (contribs != null) {
return contribs.executeCommand(command).get();
return contribs.executeCommand(command);
}
else {
logger.warn("ignoring command execution: " + extension + "," + command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ public FileFact(ISourceLocation file) {
summary = new ReplaceableFuture<>(updateSummary(file));
}

private InterruptibleFuture<ParametricSummaryBridge> updateSummary(ISourceLocation file) {
InterruptibleFuture<ParametricSummaryBridge> result = InterruptibleFuture.flatten(
lookupState.apply(file).getCurrentTreeAsync()
.thenApply(t -> contrib.summarize(file, t)), exec)
.thenApply(cons -> new ParametricSummaryBridge(cons, columns));
private CompletableFuture<ParametricSummaryBridge> updateSummary(ISourceLocation file) {
CompletableFuture<ParametricSummaryBridge> result = lookupState.apply(file)
.getCurrentTreeAsync()
.thenComposeAsync(t -> contrib.summarize(file, t), exec)
.thenApply(cons -> new ParametricSummaryBridge(cons, columns));
// also schedule update of error messages
result.thenAccept(p -> reportTypeCheckerMessages(p.getMessages()));
return result;
Expand Down

0 comments on commit dda9d48

Please sign in to comment.