Skip to content

Commit ad80ce6

Browse files
fix LineIndexer::getLine for the first index of a line
1 parent cb3f767 commit ad80ce6

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

enigma/src/main/java/org/quiltmc/enigma/util/LineIndexer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public int getLine(int index) {
5959
} else {
6060
int lineStart;
6161
while ((lineStart = this.collectLine()) > 0) {
62-
if (lineStart >= index) {
62+
if (lineStart == index) {
63+
return this.indexesByLine.size() - 1;
64+
} else if (lineStart > index) {
6365
// -2 to get the preceding line
6466
return this.indexesByLine.size() - 2;
6567
}

enigma/src/test/java/org/quiltmc/enigma/util/LineIndexerTest.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.jupiter.api.Test;
66

77
import java.util.List;
8+
import java.util.function.Supplier;
89

910
import static org.junit.jupiter.api.Assertions.assertEquals;
1011

@@ -104,12 +105,7 @@ void testGetLine() {
104105
for (int index = lineStartIndex; index < lineEndIndex; index++) {
105106
final int line = indexer.getLine(index);
106107

107-
final int finalIndex = index;
108-
final int finalExpectedLine = expectedLine;
109-
assertEquals(expectedLine, line, () ->
110-
"expected index %s to have line %s, but had line %s!"
111-
.formatted(finalIndex, finalExpectedLine, line)
112-
);
108+
assertEquals(expectedLine, line, unexpectedLineMessageFactoryOf(index, expectedLine, line));
113109
}
114110

115111
if (lastLine) {
@@ -119,4 +115,19 @@ void testGetLine() {
119115

120116
assertEquals(-1, indexer.getLine(SUBJECT.length()));
121117
}
118+
119+
@Test
120+
void testGetLineAtStartOfLine() {
121+
final LineIndexer indexer = createIndexer();
122+
123+
final int index = START_INDEX_EXPECTATIONS.get(1);
124+
final int line = indexer.getLine(index);
125+
final int expectedLine = 1;
126+
assertEquals(expectedLine, line, unexpectedLineMessageFactoryOf(index, expectedLine, line));
127+
}
128+
129+
private static Supplier<String> unexpectedLineMessageFactoryOf(int index, int expectedLine, int line) {
130+
return () -> "expected index %s to have line %s, but had line %s!"
131+
.formatted(index, expectedLine, line);
132+
}
122133
}

0 commit comments

Comments
 (0)