Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.opencds.cqf.cql.ls</groupId>
<artifactId>cql-ls</artifactId>
<version>4.1.2</version>
<version>4.1.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion debug/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.opencds.cqf.cql.ls</groupId>
<artifactId>cql-ls</artifactId>
<version>4.1.2</version>
<version>4.1.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion debug/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.opencds.cqf.cql.ls</groupId>
<artifactId>cql-ls</artifactId>
<version>4.1.2</version>
<version>4.1.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion ls/server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.opencds.cqf.cql.ls</groupId>
<artifactId>cql-ls</artifactId>
<version>4.1.2</version>
<version>4.1.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.cqframework.cql.cql2elm.CqlCompiler;
import org.cqframework.cql.elm.serializing.ElmJsonLibraryWriter;
import org.cqframework.cql.elm.serializing.ElmXmlLibraryWriter;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.hl7.elm.r1.Library;
Expand Down Expand Up @@ -43,17 +45,37 @@ public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
// command
// is XML and display it accordingly.
private CompletableFuture<Object> viewElm(ExecuteCommandParams params) {
String uriString = ((JsonElement) params.getArguments().get(0)).getAsString();
try {
List<Object> args = params.getArguments();

// Defensive check: ensure we have at least the URI
if (args == null || args.isEmpty()) {
return CompletableFuture.completedFuture(null);
}

String uriString = ((JsonElement) args.get(0)).getAsString();

// Handle missing or null elmType by defaulting to "xml"
String elmType = "xml";
if (args.size() > 1 && args.get(1) != null) {
elmType = ((JsonElement) args.get(1)).getAsString();
}

try {
URI uri = Uris.parseOrNull(uriString);
CqlCompiler compiler = this.cqlCompilationManager.compile(uri);

if (compiler != null) {
return CompletableFuture.completedFuture(convertToXml(compiler.getLibrary()));
// Use .equalsIgnoreCase for better robustness
if ("xml".equalsIgnoreCase(elmType)) {
return CompletableFuture.completedFuture(convertToXml(compiler.getLibrary()));
} else {
return CompletableFuture.completedFuture(convertToJson(compiler.getLibrary()));
}
}

return CompletableFuture.completedFuture(null);
} catch (Exception e) {
// Log the error here if possible to avoid "silent" failures
return CompletableFuture.completedFuture(null);
}
}
Expand All @@ -62,4 +84,9 @@ private static String convertToXml(Library library) throws IOException {
ElmXmlLibraryWriter writer = new ElmXmlLibraryWriter();
return writer.writeAsString(library);
}

private static String convertToJson(Library library) throws IOException {
ElmJsonLibraryWriter writer = new ElmJsonLibraryWriter();
return writer.writeAsString(library);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -19,6 +21,7 @@
class ViewElmCommandContributionTest {

private static ViewElmCommandContribution viewElmCommandContribution;
private Object expectedJson;

@BeforeAll
static void beforeAll() {
Expand All @@ -42,15 +45,71 @@ void executeCommand() {
params.setCommand("org.opencds.cqf.cql.ls.viewElm");
params.setArguments(Collections.singletonList(
JsonParser.parseString("\"\\/org\\/opencds\\/cqf\\/cql\\/ls\\/server\\/One.cql\"")));
viewElmCommandContribution.executeCommand(params).thenAccept(result -> {
try {
String expectedXml = new String(Files.readAllBytes(Paths.get("src/test/resources/One.xml")))
.trim()
.replaceAll("\\s+", "");
assertEquals(expectedXml, result.toString().trim().replaceAll("\\s+", ""));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
CompletableFuture<Void> future = viewElmCommandContribution
.executeCommand(params)
.thenAccept(result -> {
try {
String expectedXml = new String(Files.readAllBytes(
Paths.get("src/test/resources/org/opencds/cqf/cql/ls/server/One.xml")))
.trim()
.replaceAll("\\s+", "");
assertEquals(expectedXml, result.toString().trim().replaceAll("\\s+", ""));
} catch (IOException e) {
throw new RuntimeException(e);
}
});

// This ensures the test waits and fails if an exception occurs
future.join();
}

@Test
void executeCommandWithXmlElmType() {
ExecuteCommandParams params = new ExecuteCommandParams();
params.setCommand("org.opencds.cqf.cql.ls.viewElm");
params.setArguments(List.of(
JsonParser.parseString("\"\\/org\\/opencds\\/cqf\\/cql\\/ls\\/server\\/One.cql\""),
JsonParser.parseString("\"xml\"")));
CompletableFuture<Void> future = viewElmCommandContribution
.executeCommand(params)
.thenAccept(result -> {
try {
String expectedXml = new String(Files.readAllBytes(
Paths.get("src/test/resources/org/opencds/cqf/cql/ls/server/One.xml")))
.trim()
.replaceAll("\\s+", "");
assertEquals(expectedXml, result.toString().trim().replaceAll("\\s+", ""));
} catch (IOException e) {
throw new RuntimeException(e);
}
});

// This ensures the test waits and fails if an exception occurs
future.join();
}

@Test
void executeCommandWithJsonElmType() {
ExecuteCommandParams params = new ExecuteCommandParams();
params.setCommand("org.opencds.cqf.cql.ls.viewElm");
params.setArguments(List.of(
JsonParser.parseString("\"\\/org\\/opencds\\/cqf\\/cql\\/ls\\/server\\/One.cql\""),
JsonParser.parseString("\"json\"")));
CompletableFuture<Void> future = viewElmCommandContribution
.executeCommand(params)
.thenAccept(result -> {
try {
String expectedJson = new String(Files.readAllBytes(
Paths.get("src/test/resources/org/opencds/cqf/cql/ls/server/One.json")))
.trim()
.replaceAll("\\s+", "");
assertEquals(expectedJson, result.toString().trim().replaceAll("\\s+", ""));
} catch (IOException e) {
throw new RuntimeException(e);
}
});

// This ensures the test waits and fails if an exception occurs
future.join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"library": {
"localId": "0",
"annotation": [
{
"type": "CqlToElmInfo",
"translatorVersion": "4.1.0",
"translatorOptions": "EnableAnnotations,EnableLocators,DisableListDemotion,DisableListPromotion,EnableResultTypes",
"signatureLevel": "All"
},
{
"type": "Annotation",
"t": [],
"s": {
"r": "208",
"s": [
{
"value": [
"",
"library One"
]
}
]
}
}
],
"identifier": {
"id": "One"
},
"schemaIdentifier": {
"id": "urn:hl7-org:elm",
"version": "r1"
},
"usings": {
"def": [
{
"localId": "1",
"localIdentifier": "System",
"uri": "urn:hl7-org:elm-types:r1",
"annotation": []
}
]
},
"statements": {
"def": [
{
"localId": "208",
"locator": "3:1-4:5",
"resultTypeName": "{urn:hl7-org:elm-types:r1}Integer",
"name": "One",
"context": "Unfiltered",
"accessLevel": "Public",
"annotation": [
{
"type": "Annotation",
"t": [],
"s": {
"r": "208",
"s": [
{
"r": "209",
"value": [
"",
"define ",
"\"One\"",
":\n ",
"1"
]
}
]
}
}
],
"expression": {
"type": "Literal",
"localId": "209",
"locator": "4:5",
"resultTypeName": "{urn:hl7-org:elm-types:r1}Integer",
"valueType": "{urn:hl7-org:elm-types:r1}Integer",
"value": "1",
"annotation": []
}
}
]
}
}
}
19 changes: 11 additions & 8 deletions ls/server/src/test/resources/org/opencds/cqf/cql/ls/server/One.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<library xmlns="urn:hl7-org:elm:r1" xmlns:t="urn:hl7-org:elm-types:r1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:fhir="http://hl7.org/fhir" xmlns:qdm43="urn:healthit-gov:qdm:v4_3" xmlns:qdm53="urn:healthit-gov:qdm:v5_3" xmlns:a="urn:hl7-org:cql-annotations:r1">
<annotation translatorVersion="3.3.2" translatorOptions="EnableAnnotations,EnableLocators,DisableListDemotion,DisableListPromotion" signatureLevel="None" xsi:type="a:CqlToElmInfo"/>
<library xmlns="urn:hl7-org:elm:r1"
xmlns:a="urn:hl7-org:cql-annotations:r1"
xmlns:t="urn:hl7-org:elm-types:r1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" localId="0">
<annotation signatureLevel="All" translatorOptions="EnableAnnotations,EnableLocators,DisableListDemotion,DisableListPromotion,EnableResultTypes" translatorVersion="4.1.0" xsi:type="a:CqlToElmInfo"/>
<annotation xsi:type="a:Annotation">
<a:s r="2">
<a:s r="208">
<a:s>library One</a:s>
</a:s>
</annotation>
<identifier id="One"/>
<schemaIdentifier id="urn:hl7-org:elm" version="r1"/>
<usings>
<def localIdentifier="System" uri="urn:hl7-org:elm-types:r1"/>
<def localId="1" localIdentifier="System" uri="urn:hl7-org:elm-types:r1"/>
</usings>
<statements>
<def localId="2" locator="3:1-4:5" name="One" context="Unfiltered" accessLevel="Public">
<def accessLevel="Public" context="Unfiltered" localId="208" locator="3:1-4:5" name="One" resultTypeName="t:Integer">
<annotation xsi:type="a:Annotation">
<a:s r="2">
<a:s r="1">define &quot;One&quot;:
<a:s r="208">
<a:s r="209">define "One":
1</a:s>
</a:s>
</annotation>
<expression localId="1" locator="4:5" valueType="t:Integer" value="1" xsi:type="Literal"/>
<expression localId="209" locator="4:5" resultTypeName="t:Integer" value="1" valueType="t:Integer" xsi:type="Literal"/>
</def>
</statements>
</library>
2 changes: 1 addition & 1 deletion ls/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.opencds.cqf.cql.ls</groupId>
<artifactId>cql-ls</artifactId>
<version>4.1.2</version>
<version>4.1.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion plugin/debug/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.opencds.cqf.cql.ls</groupId>
<artifactId>cql-ls</artifactId>
<version>4.1.2</version>
<version>4.1.2-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>org.opencds.cqf.cql.ls</groupId>
<artifactId>cql-ls</artifactId>
<packaging>pom</packaging>
<version>4.1.2</version>
<version>4.1.2-SNAPSHOT</version>

<name>CQL Language Server</name>
<description>A Language Server for CQL implementing the LSP</description>
Expand Down