Search in sources :

Example 1 with JsonRequest

use of org.eclipse.lsp4j.jsonrpc.services.JsonRequest in project rascal-language-servers by usethesource.

the class IRascalFileSystemServices method readFile.

@JsonRequest("rascal/filesystem/readFile")
default CompletableFuture<LocationContent> readFile(URIParameter uri) {
    // has to be divisibly by 3
    final int BUFFER_SIZE = 3 * 1024;
    return CompletableFuture.supplyAsync(() -> {
        try (InputStream source = reg.getInputStream(uri.getLocation())) {
            // there is no streaming base64 encoder, but we also do not want to have the
            // whole file in memory
            // just to base64 encode it. So we stream it in chunks that will not cause
            // padding characters in
            // base 64
            Encoder encoder = Base64.getEncoder();
            StringBuilder result = new StringBuilder();
            byte[] buffer = new byte[BUFFER_SIZE];
            int read;
            while ((read = source.read(buffer, 0, BUFFER_SIZE)) == BUFFER_SIZE) {
                result.append(encoder.encodeToString(buffer));
            }
            if (read > 0) {
                // last part needs to be a truncated part of the buffer
                buffer = Arrays.copyOf(buffer, read);
                result.append(encoder.encodeToString(buffer));
            }
            return new LocationContent(result.toString());
        } catch (IOException | URISyntaxException e) {
            throw new CompletionException(e);
        }
    });
}
Also used : InputStream(java.io.InputStream) Encoder(java.util.Base64.Encoder) CompletionException(java.util.concurrent.CompletionException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JsonRequest(org.eclipse.lsp4j.jsonrpc.services.JsonRequest)

Example 2 with JsonRequest

use of org.eclipse.lsp4j.jsonrpc.services.JsonRequest in project rascal-language-servers by usethesource.

the class IRascalFileSystemServices method rename.

@JsonRequest("rascal/filesystem/rename")
default CompletableFuture<Void> rename(RenameParameters params) {
    return CompletableFuture.runAsync(() -> {
        try {
            ISourceLocation oldLoc = params.getOldLocation();
            ISourceLocation newLoc = params.getNewLocation();
            reg.rename(oldLoc, newLoc, params.isOverwrite());
        } catch (IOException | URISyntaxException e) {
            throw new CompletionException(e);
        }
    });
}
Also used : CompletionException(java.util.concurrent.CompletionException) ISourceLocation(io.usethesource.vallang.ISourceLocation) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JsonRequest(org.eclipse.lsp4j.jsonrpc.services.JsonRequest)

Example 3 with JsonRequest

use of org.eclipse.lsp4j.jsonrpc.services.JsonRequest in project org.alloytools.alloy by AlloyTools.

the class AlloyTextDocumentService method ExecuteAlloyCommand.

@JsonRequest
public CompletableFuture<Void> ExecuteAlloyCommand(com.google.gson.JsonArray params) {
    log("ExecuteAlloyCommand() called with " + params + ", " + params.getClass());
    String uri = params.get(0).getAsString();
    int ind = params.get(1).getAsInt();
    int line = params.get(2).getAsInt(), character = params.get(3).getAsInt();
    Position position = new Position(line, character);
    Pos pos = positionToPos(position);
    String fileString = fileContents.get(uri);
    // Another ugly hack to make things work if invoked through links (which makes the uri look different)
    if (fileString == null) {
        String uriPath = fileUriToPath(uri);
        for (Map.Entry<String, String> entry : fileContents.entrySet()) {
            if (fileUriToPath(entry.getKey()).equals(uriPath)) {
                fileString = entry.getValue();
                uri = entry.getKey();
                break;
            }
        }
    }
    if (fileString == null) {
        System.err.println("Error in ExecuteAlloyCommand: failed to retrieve file contents for " + uri);
        return CompletableFuture.completedFuture(null);
    }
    CompModule module = CompUtil.parseOneModule(fileString);
    ConstList<edu.mit.csail.sdg.ast.Command> commands = module.getAllCommands();
    edu.mit.csail.sdg.ast.Command command = commands.stream().filter(comm -> comm.pos().y == pos.y && comm.pos.x == pos.x).findFirst().orElse(null);
    if (command != null || ind == -1) {
        String uriToShutUpStupidJava = uri;
        CompletableFuture.runAsync(() -> doRun(uriToShutUpStupidJava, ind));
    } else {
        System.err.println("no matching command found");
    }
    return CompletableFuture.completedFuture(null);
}
Also used : AlloyLanguageServerUtil.posToPosition(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.posToPosition) Position(org.eclipse.lsp4j.Position) CompModule(edu.mit.csail.sdg.parser.CompModule) AlloyLanguageServerUtil.createRangeFromPos(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.createRangeFromPos) AlloyLanguageServerUtil.positionToPos(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.positionToPos) Pos(edu.mit.csail.sdg.alloy4.Pos) Command(org.eclipse.lsp4j.Command) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) JsonRequest(org.eclipse.lsp4j.jsonrpc.services.JsonRequest)

Example 4 with JsonRequest

use of org.eclipse.lsp4j.jsonrpc.services.JsonRequest in project org.alloytools.alloy by AlloyTools.

the class AlloyTextDocumentService method ListAlloyCommands.

@JsonRequest
public CompletableFuture<Void> ListAlloyCommands(JsonPrimitive documentUri) {
    log("ListAlloyCommands called with " + documentUri + ", of type " + documentUri.getClass().getName());
    List<CommandsListResultItem> res = getCommands(documentUri.getAsString()).stream().map(pair -> new CommandsListResultItem(pair.a.toString(), pair.b)).collect(Collectors.toList());
    client.commandsListResult(new CommandsListResult(res));
    return CompletableFuture.completedFuture(null);
}
Also used : CoreMinimization(edu.mit.csail.sdg.alloy4.A4Preferences.CoreMinimization) Arrays(java.util.Arrays) ExprConstant(edu.mit.csail.sdg.ast.ExprConstant) InferPartialInstance(edu.mit.csail.sdg.alloy4.A4Preferences.InferPartialInstance) ExprVar(edu.mit.csail.sdg.ast.ExprVar) DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) WorkspaceSymbolParams(org.eclipse.lsp4j.WorkspaceSymbolParams) Macro(edu.mit.csail.sdg.parser.Macro) Pair(edu.mit.csail.sdg.alloy4.Pair) Sig(edu.mit.csail.sdg.ast.Sig) DidSaveTextDocumentParams(org.eclipse.lsp4j.DidSaveTextDocumentParams) CommandsListResultItem(org.alloytools.alloy.lsp.provider.AlloyLanguageClient.CommandsListResultItem) SimpleTask2(edu.mit.csail.sdg.alloy4whole.SimpleReporter.SimpleTask2) AlloyLanguageServerUtil.posToPosition(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.posToPosition) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) Map(java.util.Map) AlloyLanguageServerUtil.filePathToUri(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.filePathToUri) NoOverflow(edu.mit.csail.sdg.alloy4.A4Preferences.NoOverflow) RenameParams(org.eclipse.lsp4j.RenameParams) TextDocumentService(org.eclipse.lsp4j.services.TextDocumentService) WorkerCallback(edu.mit.csail.sdg.alloy4.WorkerEngine.WorkerCallback) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) JsonRequest(org.eclipse.lsp4j.jsonrpc.services.JsonRequest) Lsp4jUtil.newDiagnostic(org.alloytools.alloy.lsp.provider.Lsp4jUtil.newDiagnostic) Set(java.util.Set) DocumentOnTypeFormattingParams(org.eclipse.lsp4j.DocumentOnTypeFormattingParams) WorkspaceService(org.eclipse.lsp4j.services.WorkspaceService) WorkerEngine(edu.mit.csail.sdg.alloy4.WorkerEngine) Unrolls(edu.mit.csail.sdg.alloy4.A4Preferences.Unrolls) Stream(java.util.stream.Stream) Computer(edu.mit.csail.sdg.alloy4.Computer) DocumentHighlightKind(org.eclipse.lsp4j.DocumentHighlightKind) A4Options(edu.mit.csail.sdg.translator.A4Options) XMLNode(edu.mit.csail.sdg.alloy4.XMLNode) LanguageClientAware(org.eclipse.lsp4j.services.LanguageClientAware) SymbolKind(org.eclipse.lsp4j.SymbolKind) Diagnostic(org.eclipse.lsp4j.Diagnostic) SkolemDepth(edu.mit.csail.sdg.alloy4.A4Preferences.SkolemDepth) Version(edu.mit.csail.sdg.alloy4.Version) DocumentRangeFormattingParams(org.eclipse.lsp4j.DocumentRangeFormattingParams) Hover(org.eclipse.lsp4j.Hover) AlloyLanguageServerUtil.fileUriToPath(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.fileUriToPath) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) AlloyLanguageServerUtil.createRange(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.createRange) TextEdit(org.eclipse.lsp4j.TextEdit) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) AlloyLanguageServerUtil.createRangeFromPos(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.createRangeFromPos) AlloyLanguageServerUtil.positionToPos(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.positionToPos) Assert(edu.mit.csail.sdg.ast.Assert) RunCompleted(org.alloytools.alloy.lsp.provider.AlloyLSMessageType.RunCompleted) DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) ConstList(edu.mit.csail.sdg.alloy4.ConstList) ExprBad(edu.mit.csail.sdg.ast.ExprBad) AlloyLanguageServerUtil.getResult(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.getResult) DidChangeWatchedFilesParams(org.eclipse.lsp4j.DidChangeWatchedFilesParams) IOException(java.io.IOException) Expr(edu.mit.csail.sdg.ast.Expr) A4SolutionReader(edu.mit.csail.sdg.translator.A4SolutionReader) File(java.io.File) SignatureHelp(org.eclipse.lsp4j.SignatureHelp) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Paths(java.nio.file.Paths) BufferedReader(java.io.BufferedReader) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) SimInstance(edu.mit.csail.sdg.sim.SimInstance) RunResult(org.alloytools.alloy.lsp.provider.AlloyLSMessageType.RunResult) Lsp4jUtil.newPublishDiagnosticsParams(org.alloytools.alloy.lsp.provider.Lsp4jUtil.newPublishDiagnosticsParams) CommandsListResult(org.alloytools.alloy.lsp.provider.AlloyLanguageClient.CommandsListResult) URISyntaxException(java.net.URISyntaxException) ObjectInputStream(java.io.ObjectInputStream) Scanner(java.util.Scanner) MessageType(org.eclipse.lsp4j.MessageType) VisitQueryOnce(edu.mit.csail.sdg.ast.VisitQueryOnce) Clause(edu.mit.csail.sdg.ast.Clause) Verbosity(edu.mit.csail.sdg.alloy4.A4Preferences.Verbosity) URIUtil(org.eclipse.core.runtime.URIUtil) CodeLens(org.eclipse.lsp4j.CodeLens) Gson(com.google.gson.Gson) Field(edu.mit.csail.sdg.ast.Sig.Field) Location(org.eclipse.lsp4j.Location) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ErrorSyntax(edu.mit.csail.sdg.alloy4.ErrorSyntax) URI(java.net.URI) DidCloseTextDocumentParams(org.eclipse.lsp4j.DidCloseTextDocumentParams) Lsp4jUtil.newSymbolInformation(org.alloytools.alloy.lsp.provider.Lsp4jUtil.newSymbolInformation) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) MarkupContent(org.eclipse.lsp4j.MarkupContent) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) SubStack(edu.mit.csail.sdg.alloy4.A4Preferences.SubStack) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) CompletionItem(org.eclipse.lsp4j.CompletionItem) List(java.util.List) Command(org.eclipse.lsp4j.Command) DidOpenTextDocumentParams(org.eclipse.lsp4j.DidOpenTextDocumentParams) DocumentLink(org.eclipse.lsp4j.DocumentLink) Pos(edu.mit.csail.sdg.alloy4.Pos) Func(edu.mit.csail.sdg.ast.Func) VerbosityPref(edu.mit.csail.sdg.alloy4.A4Preferences.VerbosityPref) CompUtil(edu.mit.csail.sdg.parser.CompUtil) FilenameUtils(org.apache.commons.io.FilenameUtils) Solver(edu.mit.csail.sdg.alloy4.A4Preferences.Solver) CompletionParams(org.eclipse.lsp4j.CompletionParams) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) DocumentSymbolParams(org.eclipse.lsp4j.DocumentSymbolParams) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ImplicitThis(edu.mit.csail.sdg.alloy4.A4Preferences.ImplicitThis) ErrorWarning(edu.mit.csail.sdg.alloy4.ErrorWarning) VizGUI(edu.mit.csail.sdg.alloy4viz.VizGUI) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) HashSet(java.util.HashSet) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Util(edu.mit.csail.sdg.alloy4.Util) Lsp4jUtil.newMessageParams(org.alloytools.alloy.lsp.provider.Lsp4jUtil.newMessageParams) Module(edu.mit.csail.sdg.ast.Module) Err(edu.mit.csail.sdg.alloy4.Err) Position(org.eclipse.lsp4j.Position) CodeLensParams(org.eclipse.lsp4j.CodeLensParams) CompletionList(org.eclipse.lsp4j.CompletionList) JsonPrimitive(com.google.gson.JsonPrimitive) DidChangeConfigurationParams(org.eclipse.lsp4j.DidChangeConfigurationParams) CoreGranularity(edu.mit.csail.sdg.alloy4.A4Preferences.CoreGranularity) OurDialog(edu.mit.csail.sdg.alloy4.OurDialog) AlloyLanguageServerUtil.filePathResolved(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.filePathResolved) AlloyLanguageServerUtil.posToLocation(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.posToLocation) AlloyCore(org.alloytools.alloy.core.AlloyCore) A4Solution(edu.mit.csail.sdg.translator.A4Solution) FileInputStream(java.io.FileInputStream) SimpleTask1(edu.mit.csail.sdg.alloy4whole.SimpleReporter.SimpleTask1) WarningNonfatal(edu.mit.csail.sdg.alloy4.A4Preferences.WarningNonfatal) SimpleGUI(edu.mit.csail.sdg.alloy4whole.SimpleGUI) DocumentLinkParams(org.eclipse.lsp4j.DocumentLinkParams) RecordKodkod(edu.mit.csail.sdg.alloy4.A4Preferences.RecordKodkod) SubMemory(edu.mit.csail.sdg.alloy4.A4Preferences.SubMemory) AlloyLanguageServerUtil.fs(org.alloytools.alloy.lsp.provider.AlloyLanguageServerUtil.fs) FileReader(java.io.FileReader) InputStream(java.io.InputStream) ReferenceParams(org.eclipse.lsp4j.ReferenceParams) CompModule(edu.mit.csail.sdg.parser.CompModule) CommandsListResult(org.alloytools.alloy.lsp.provider.AlloyLanguageClient.CommandsListResult) CommandsListResultItem(org.alloytools.alloy.lsp.provider.AlloyLanguageClient.CommandsListResultItem) JsonRequest(org.eclipse.lsp4j.jsonrpc.services.JsonRequest)

Example 5 with JsonRequest

use of org.eclipse.lsp4j.jsonrpc.services.JsonRequest in project lsp4j by eclipse.

the class GenericEndpointTest method testMultiParams.

protected void testMultiParams(Object params, String expectedString, Integer expectedInt, Predicate<String> predicate) throws Exception {
    LogMessageAccumulator logMessages = null;
    try {
        if (predicate != null) {
            logMessages = new LogMessageAccumulator();
            logMessages.registerTo(GenericEndpoint.class);
        }
        GenericEndpoint endpoint = new GenericEndpoint(new Object() {

            String stringValue;

            Integer intValue;

            @JsonRequest
            public CompletableFuture<String> getStringValue() {
                return CompletableFuture.completedFuture(stringValue);
            }

            @JsonRequest
            public CompletableFuture<Integer> getIntValue() {
                return CompletableFuture.completedFuture(intValue);
            }

            @JsonNotification
            public void myNotification(String stringValue, Integer intValue) {
                this.stringValue = stringValue;
                this.intValue = intValue;
            }
        });
        endpoint.notify("myNotification", params);
        if (predicate != null) {
            logMessages.await(r -> Level.WARNING == r.getLevel() && predicate.test(r.getMessage()));
        }
        Assert.assertEquals(expectedString, endpoint.request("getStringValue", null).get());
        Assert.assertEquals(expectedInt, endpoint.request("getIntValue", null).get());
    } finally {
        if (logMessages != null) {
            logMessages.unregister();
        }
    }
}
Also used : GenericEndpoint(org.eclipse.lsp4j.jsonrpc.services.GenericEndpoint) CompletableFuture(java.util.concurrent.CompletableFuture) JsonNotification(org.eclipse.lsp4j.jsonrpc.services.JsonNotification) JsonRequest(org.eclipse.lsp4j.jsonrpc.services.JsonRequest) LogMessageAccumulator(org.eclipse.lsp4j.jsonrpc.test.LogMessageAccumulator)

Aggregations

JsonRequest (org.eclipse.lsp4j.jsonrpc.services.JsonRequest)14 IOException (java.io.IOException)8 URISyntaxException (java.net.URISyntaxException)8 ISourceLocation (io.usethesource.vallang.ISourceLocation)6 CompletionException (java.util.concurrent.CompletionException)6 CompletableFuture (java.util.concurrent.CompletableFuture)4 JsonPrimitive (com.google.gson.JsonPrimitive)2 AddTerminologyCommmand (com.nedap.openehr.lsp.commands.AddTerminologyCommmand)2 ConvertToOptCommand (com.nedap.openehr.lsp.commands.ConvertToOptCommand)2 GenerateExampleCommand (com.nedap.openehr.lsp.commands.GenerateExampleCommand)2 Pos (edu.mit.csail.sdg.alloy4.Pos)2 CompModule (edu.mit.csail.sdg.parser.CompModule)2 File (java.io.File)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2