Skip to content

Commit 7d59a04

Browse files
committed
Fix parse errors for empty build files
Document::rangeBetween would return `null` if the document was empty, but I wasn't checking for that in ToSmithyNode (which creates parse events). The reason the range is `null` is because Document.lineOfIndex returns oob for an index of 0 into an empty document. Makes sense, as an empty document has no lines. I updated a DocumentTest to clarify this behavior.
1 parent b124b3d commit 7d59a04

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/main/java/software/amazon/smithy/lsp/project/ToSmithyNode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package software.amazon.smithy.lsp.project;
77

88
import java.util.List;
9+
import org.eclipse.lsp4j.Range;
910
import software.amazon.smithy.lsp.document.Document;
1011
import software.amazon.smithy.lsp.protocol.LspAdapter;
1112
import software.amazon.smithy.lsp.syntax.Syntax;
@@ -104,6 +105,10 @@ yield switch (value) {
104105
}
105106

106107
private SourceLocation nodeStartSourceLocation(Syntax.Node node) {
107-
return LspAdapter.toSourceLocation(path, document.rangeBetween(node.start(), node.end()));
108+
Range range = document.rangeBetween(node.start(), node.end());
109+
if (range == null) {
110+
range = LspAdapter.origin();
111+
}
112+
return LspAdapter.toSourceLocation(path, range);
108113
}
109114
}

src/test/java/software/amazon/smithy/lsp/document/DocumentTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ public void getsLineOfIndex() {
293293
Document leadingAndTrailingWs = makeDocument("\nabc\n");
294294
Document threeLine = makeDocument("abc\ndef\nhij\n");
295295

296+
assertThat(empty.lineOfIndex(0), is(-1)); // empty has no lines, so oob
296297
assertThat(empty.lineOfIndex(1), is(-1)); // oob
297298
assertThat(single.lineOfIndex(0), is(0)); // start
298299
assertThat(single.lineOfIndex(2), is(0)); // end

src/test/java/software/amazon/smithy/lsp/project/ProjectConfigTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.function.Supplier;
2626
import java.util.stream.Collectors;
27+
import org.eclipse.lsp4j.Position;
2728
import org.junit.jupiter.api.Test;
2829
import software.amazon.smithy.build.model.MavenRepository;
2930
import software.amazon.smithy.cli.dependencies.DependencyResolver;
@@ -32,6 +33,7 @@
3233
import software.amazon.smithy.lsp.ServerState;
3334
import software.amazon.smithy.lsp.TextWithPositions;
3435
import software.amazon.smithy.lsp.document.Document;
36+
import software.amazon.smithy.lsp.protocol.LspAdapter;
3537

3638
public class ProjectConfigTest {
3739
@Test
@@ -78,6 +80,21 @@ public void mergesBuildExts() {
7880
assertThat(config.maven().getDependencies(), containsInAnyOrder("foo"));
7981
}
8082

83+
@Test
84+
public void handlesEmptyFiles() {
85+
var root = Path.of("foo");
86+
var buildFiles = createBuildFiles(root, BuildFileType.SMITHY_BUILD, "");
87+
var result = load(root, buildFiles);
88+
89+
var smithyBuild = buildFiles.getByType(BuildFileType.SMITHY_BUILD);
90+
assertThat(smithyBuild, notNullValue());
91+
assertThat(result.events(), containsInAnyOrder(allOf(
92+
eventWithId(equalTo("Model")),
93+
eventWithMessage(containsString("Error parsing JSON")),
94+
eventWithSourceLocation(equalTo(LspAdapter.toSourceLocation(smithyBuild.path(), new Position(0, 0))))
95+
)));
96+
}
97+
8198
@Test
8299
public void validatesSmithyBuildJson() {
83100
var text = TextWithPositions.from("""

0 commit comments

Comments
 (0)