/** * Advance the position in the post-translation file. e.g. (1, 3) -> * advanceByTextSize(blah\nblah\nblah) -> (3, 4) NB: slower than specifying directly using * advanceInLine or advanceToNewLine */ public void advanceByTextSize(String text) { TextPosition eofPos = LengthScanner.getLength(text); if (eofPos.getLine() == 1) { advanceInLine(eofPos.getColumn() - 1); } else { advanceToNewLine(eofPos.getLine() - 1, eofPos.getColumn()); } }
/** Record a change to the cumulative offset, beginning at the current position. */ public void recordOffsetChange(String text, int startPos, boolean insert) { TextPosition eofPos = LengthScanner.getLength(text); int lineOffsetChange = -1; int colOffsetChange = -1; if (eofPos.getLine() == 1) { lineOffsetChange = 0; colOffsetChange = eofPos.getColumn() - 1; } else { lineOffsetChange = eofPos.getLine() - 1; colOffsetChange = eofPos.getColumn() - startPos; } if (insert) { recordOffsetChange(-1 * lineOffsetChange, -1 * colOffsetChange); } else { recordOffsetChange(lineOffsetChange, colOffsetChange); } }
/** * Advance the position in the post-translation file. e.g. (1, 3) -> advanceToNewLine(2, 4) -> (3, * 4) */ public void advanceToNewLine(int numLines, int newCol) { if (numLines < 1) { throw new IllegalArgumentException("Attempted to move to an earlier line: " + numLines); } else if (newCol < 1) { throw new IllegalArgumentException("Attempted to move to an invalid column: " + newCol); } currPos = new TextPosition(currPos.getLine() + numLines, newCol); }
/** * Convert the list of changes to the cumulative offset into a list of cumulative offsets and * build a map from them. */ public PositionMap buildPositionMap() { // TODO-AC: verify correctness of map? int accumulatedLineOffset = 0; int line = -1; int lineAccumulatedColOffset = 0; SortedMap<TextPosition, Offset> offsetMap = new TreeMap<TextPosition, Offset>(); // NB: must iterate in order of increasing position for (Map.Entry<TextPosition, OffsetChange> entry : offsetChangeMap.entrySet()) { TextPosition pos = entry.getKey(); if (line != pos.getLine()) { line = pos.getLine(); lineAccumulatedColOffset = 0; } OffsetChange offsetChange = entry.getValue(); accumulatedLineOffset += offsetChange.lineOffsetChange; lineAccumulatedColOffset += offsetChange.colOffsetChange; offsetMap.put(pos, new Offset(accumulatedLineOffset, lineAccumulatedColOffset)); offsetMap.put( new TextPosition(pos.getLine() + 1, 1), new Offset(accumulatedLineOffset, 0)); // expect this to be overridden } return new OffsetPositionMap(offsetMap); }
/** * Advance the position in the post-translation file. e.g. (1, 3) -> advanceInLine(2) -> (1, 5) */ public void advanceInLine(int numCols) { if (numCols < 0) { throw new IllegalArgumentException("Attempted to move backwards in line: " + numCols); } currPos = new TextPosition(currPos.getLine(), currPos.getColumn() + numCols); }
TextPosition reverseOffset(TextPosition original) { return new TextPosition(original.getLine() - lineOffset, original.getColumn() - colOffset); }
TextPosition forwardOffset(TextPosition original) { return new TextPosition(original.getLine() + lineOffset, original.getColumn() + colOffset); }