Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LSP command for getting ELM JSON #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.opencds.cqf.cql.ls.server.command;

import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.cqframework.cql.cql2elm.CqlTranslator;
Expand All @@ -13,6 +15,7 @@

public class ViewElmCommandContribution implements CommandContribution {
private static final String VIEW_ELM_COMMAND = "org.opencds.cqf.cql.ls.viewElm";
private static final String VIEW_ELM_JSON_COMMAND = "org.opencds.cqf.cql.ls.viewElmJson";

private final CqlTranslationManager cqlTranslationManager;

Expand All @@ -22,31 +25,32 @@ public ViewElmCommandContribution(CqlTranslationManager cqlTranslationManager) {

@Override
public Set<String> getCommands() {
return Collections.singleton(VIEW_ELM_COMMAND);
return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(VIEW_ELM_COMMAND, VIEW_ELM_JSON_COMMAND)));
}

@Override
public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
switch (params.getCommand()) {
case VIEW_ELM_COMMAND:
return this.viewElm(params);
return this.viewElm(params, true);
case VIEW_ELM_JSON_COMMAND:
return this.viewElm(params, false);
default:
return CommandContribution.super.executeCommand(params);
}
}

// There's currently not a "show text file" or similar command in the LSP spec,
// So it's not client agnostic. The client has to know that the result of this
// command
// is XML and display it accordingly.
private CompletableFuture<Object> viewElm(ExecuteCommandParams params) {
// command is XML or JSON and display it accordingly.
private CompletableFuture<Object> viewElm(ExecuteCommandParams params, Boolean isXml) {
String uriString = ((JsonElement) params.getArguments().get(0)).getAsString();
try {

URI uri = Uris.parseOrNull(uriString);
CqlTranslator translator = this.cqlTranslationManager.translate(uri);
if (translator != null) {
return CompletableFuture.completedFuture(translator.toXml());
return CompletableFuture.completedFuture(isXml ? translator.toXml() : translator.toJson());
}

return CompletableFuture.completedFuture(null);
Expand Down