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);
}
});
}
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);
}
});
}
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);
}
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);
}
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();
}
}
}
Aggregations