Stop using long to mask line/col

This commit is contained in:
Clément Fournier committed 2022-04-30 14:11:16 +02:00
1 parent 772ccb3386
commit 7ed2b6610a
3 files changed
+11 -40

No files matched your search

@@ -74,27 +74,23 @@ final class RootTextDocument extends BaseCloseable implements TextDocument {
// We use longs to return both numbers at the same time
// This limits us to 2 billion lines or columns, which is FINE
long bpos = positioner.lineColFromOffset(region.getStartOffset(), true);
long epos = region.isEmpty() ? bpos
: positioner.lineColFromOffset(region.getEndOffset(), false);
TextPos2d bpos = positioner.lineColFromOffset(region.getStartOffset(), true);
TextPos2d epos = region.isEmpty() ? bpos
: positioner.lineColFromOffset(region.getEndOffset(), false);
return new FileLocation(
fileName,
SourceCodePositioner.unmaskLine(bpos),
SourceCodePositioner.unmaskCol(bpos),
SourceCodePositioner.unmaskLine(epos),
SourceCodePositioner.unmaskCol(epos),
bpos.getLine(),
bpos.getColumn(),
epos.getLine(),
epos.getColumn(),
region
);
}
@Override
public TextPos2d lineColumnAtOffset(int offset, boolean inclusive) {
long longPos = content.getPositioner().lineColFromOffset(offset, inclusive);
return TextPos2d.pos2d(
SourceCodePositioner.unmaskLine(longPos),
SourceCodePositioner.unmaskCol(longPos)
);
return content.getPositioner().lineColFromOffset(offset, inclusive);
}
@Override
@@ -37,7 +37,7 @@ final class SourceCodePositioner {
return lineOffsets;
}
long lineColFromOffset(int offset, boolean inclusive) {
TextPos2d lineColFromOffset(int offset, boolean inclusive) {
AssertionUtil.requireInInclusiveRange("offset", offset, 0, sourceCodeLength);
int line = searchLineOffset(offset);
@@ -51,23 +51,10 @@ final class SourceCodePositioner {
// handle. This is because an offset may be interpreted as the index
// of a character, or the caret position between two characters. This
// is relevant when building text regions, to respect inclusivity, etc.
return maskLineCol(lineIdx, getLastColumnOfLine(lineIdx));
return TextPos2d.pos2d(lineIdx, getLastColumnOfLine(lineIdx));
}
return maskLineCol(line, 1 + offset - lineOffsets[lineIdx]);
}
// test only
static long maskLineCol(int line, int col) {
return (long) line << 32 | (long) col;
}
static int unmaskLine(long lineCol) {
return (int) (lineCol >> 32);
}
static int unmaskCol(long lineCol) {
return (int) lineCol;
return TextPos2d.pos2d(line, 1 + offset - lineOffsets[lineIdx]);
}
/**
@@ -153,16 +153,4 @@ public class SourceCodePositionerTest {
assertArrayEquals(new int[] { 0, 41, 50, 51 }, positioner.getLineOffsets());
}
@Test
public void longOffsetMasking() {
assertMasking(1, 4);
assertMasking(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
private void assertMasking(int line, int col) {
long l = SourceCodePositioner.maskLineCol(line, col);
assertEquals(line, SourceCodePositioner.unmaskLine(l));
assertEquals(col, SourceCodePositioner.unmaskCol(l));
}
}