aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2025-01-01 10:56:32 +0000
committerMichael Williamson <mike@zwobble.org>2025-01-01 10:56:32 +0000
commit7ff404bab52df237606f2018ccab722b05beb955 (patch)
treeca5f4e25d5ba63aae1e28c97420f6da692ca6d30 /src
parent26f165d9c04ecd8bd57a39641e9df4f01112de8d (diff)
Highlight ranges in source description
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/sources/FileFragmentSource.java9
-rw-r--r--src/test/java/org/shedlang/compiler/config/BinConfigTests.java2
-rw-r--r--src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java6
-rw-r--r--src/test/java/org/shedlang/compiler/config/json/Json5ObjectWithSourceTests.java18
-rw-r--r--src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java73
5 files changed, 92 insertions, 16 deletions
diff --git a/src/main/java/org/shedlang/compiler/sources/FileFragmentSource.java b/src/main/java/org/shedlang/compiler/sources/FileFragmentSource.java
index fe0c6271..0fc05672 100644
--- a/src/main/java/org/shedlang/compiler/sources/FileFragmentSource.java
+++ b/src/main/java/org/shedlang/compiler/sources/FileFragmentSource.java
@@ -22,7 +22,10 @@ public record FileFragmentSource(
return context(
line,
lineIndex,
- characterIndexStart - position
+ characterIndexStart - position,
+ nextLinePosition <= characterIndexEnd
+ ? 1
+ : Math.max(characterIndexEnd - characterIndexStart, 1)
);
}
position = nextLinePosition;
@@ -64,9 +67,9 @@ public record FileFragmentSource(
return at(length, length);
}
- private String context(String line, int lineIndex, int columnIndex) {
+ private String context(String line, int lineIndex, int columnIndex, int length) {
var lineNumber = lineIndex + 1;
var columnNumber = columnIndex + 1;
- return filename + ":" + lineNumber + ":" + columnNumber + "\n" + line + "\n" + " ".repeat(columnIndex) + "^";
+ return filename + ":" + lineNumber + ":" + columnNumber + "\n" + line + "\n" + " ".repeat(columnIndex) + "^".repeat(length);
}
}
diff --git a/src/test/java/org/shedlang/compiler/config/BinConfigTests.java b/src/test/java/org/shedlang/compiler/config/BinConfigTests.java
index d909bb84..eb6514d1 100644
--- a/src/test/java/org/shedlang/compiler/config/BinConfigTests.java
+++ b/src/test/java/org/shedlang/compiler/config/BinConfigTests.java
@@ -52,6 +52,6 @@ public class BinConfigTests {
assertThat(error.source().describe(), equalTo("""
<string>:3:20
mainNamespace: "A.B",
- ^"""));
+ ^^^^^"""));
}
}
diff --git a/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java b/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java
index f0deac1e..e710dcc6 100644
--- a/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java
+++ b/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java
@@ -95,7 +95,7 @@ public class ProjectConfigTests {
assertThat(error.source().describe(), equalTo("""
<string>:1:8
{bins: true}
- ^"""
+ ^^^^"""
));
}
@@ -161,7 +161,7 @@ public class ProjectConfigTests {
assertThat(error.source().describe(), equalTo("""
<string>:1:12
{examples: true}
- ^"""
+ ^^^^"""
));
}
@@ -227,7 +227,7 @@ public class ProjectConfigTests {
assertThat(error.source().describe(), equalTo("""
<string>:1:12
{profiles: true}
- ^"""
+ ^^^^"""
));
}
diff --git a/src/test/java/org/shedlang/compiler/config/json/Json5ObjectWithSourceTests.java b/src/test/java/org/shedlang/compiler/config/json/Json5ObjectWithSourceTests.java
index ad7f7c1c..4dc1a117 100644
--- a/src/test/java/org/shedlang/compiler/config/json/Json5ObjectWithSourceTests.java
+++ b/src/test/java/org/shedlang/compiler/config/json/Json5ObjectWithSourceTests.java
@@ -41,7 +41,7 @@ public class Json5ObjectWithSourceTests {
assertThat(error.source().describe(), equalTo("""
<string>:5:13
{},
- ^"""
+ ^^"""
));
}
@@ -74,7 +74,7 @@ public class Json5ObjectWithSourceTests {
assertThat(error.source().describe(), equalTo("""
<string>:5:17
{x: true},
- ^"""
+ ^^^^"""
));
}
@@ -107,7 +107,7 @@ public class Json5ObjectWithSourceTests {
assertThat(error.source().describe(), equalTo("""
<string>:5:17
{x: 1.2},
- ^"""
+ ^^^"""
));
}
@@ -189,7 +189,7 @@ public class Json5ObjectWithSourceTests {
assertThat(error.source().describe(), equalTo("""
<string>:5:17
{x: true},
- ^"""
+ ^^^^"""
));
}
@@ -247,7 +247,7 @@ public class Json5ObjectWithSourceTests {
assertThat(error.source().describe(), equalTo("""
<string>:5:13
{},
- ^"""
+ ^^"""
));
}
@@ -280,7 +280,7 @@ public class Json5ObjectWithSourceTests {
assertThat(error.source().describe(), equalTo("""
<string>:5:20
{name: true},
- ^"""
+ ^^^^"""
));
}
@@ -311,7 +311,7 @@ public class Json5ObjectWithSourceTests {
"""
<string>:5:20
{name: "two"},
- ^"""
+ ^^^^^"""
));
}
@@ -369,7 +369,7 @@ public class Json5ObjectWithSourceTests {
assertThat(error.source().describe(), equalTo("""
<string>:5:20
{name: true},
- ^"""
+ ^^^^"""
));
}
@@ -400,7 +400,7 @@ public class Json5ObjectWithSourceTests {
"""
<string>:5:20
{name: "two"},
- ^"""
+ ^^^^^"""
));
}
}
diff --git a/src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java b/src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java
index c0d7684a..0c1df6ac 100644
--- a/src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java
+++ b/src/test/java/org/shedlang/compiler/sources/FileFragmentSourceTests.java
@@ -111,6 +111,79 @@ public class FileFragmentSourceTests {
}
@Test
+ public void whenSourceIsZeroLengthThenPointerPointsToNextCharacter() {
+ var source = new FileFragmentSource(
+ "<filename>",
+ """
+ abcdef
+ ghijkl
+ mnopqr""",
+ 9,
+ 9
+ );
+
+ assertThat(source.describe(), equalTo("""
+ <filename>:2:3
+ ghijkl
+ ^"""));
+ }
+
+ @Test
+ public void whenSourceIsOneCharacterThenPointerPointsToCharacter() {
+ var source = new FileFragmentSource(
+ "<filename>",
+ """
+ abcdef
+ ghijkl
+ mnopqr""",
+ 9,
+ 9
+ );
+
+ assertThat(source.describe(), equalTo("""
+ <filename>:2:3
+ ghijkl
+ ^"""));
+ }
+
+ @Test
+ public void whenSourceIsMultipleCharactersOnSameLineThenPointerPointsToAllCharacters() {
+ var source = new FileFragmentSource(
+ "<filename>",
+ """
+ abcdef
+ ghijkl
+ mnopqr""",
+ 9,
+ 12
+ );
+
+ assertThat(source.describe(), equalTo("""
+ <filename>:2:3
+ ghijkl
+ ^^^"""));
+ }
+
+ @Test
+ public void whenSourceIsMultipleCharactersOnMultipleLinesThenPointerPointsToFirstCharacter() {
+ // TODO: show multiple lines
+ var source = new FileFragmentSource(
+ "<filename>",
+ """
+ abcdef
+ ghijkl
+ mnopqr""",
+ 9,
+ 14
+ );
+
+ assertThat(source.describe(), equalTo("""
+ <filename>:2:3
+ ghijkl
+ ^"""));
+ }
+
+ @Test
public void atReturnsSubsectionOfCompleteSource() {
var source = FileFragmentSource.file("<filename>", "abcdefg");