public void testSoftWrapToHardWrapConversion() throws IOException { String text = "this is line 1\n" + "this is line 2\n" + "this is line 3\n" + "this is line 4\n" + "this is line 5"; init(50, text, 7); VisualPosition changePosition = new VisualPosition(1, 0); myEditor.getCaretModel().moveToVisualPosition(changePosition); int logicalLinesBefore = myEditor.offsetToLogicalPosition(text.length()).line; int offsetBefore = myEditor.getCaretModel().getOffset(); LogicalPosition logicalPositionBefore = myEditor.visualToLogicalPosition(changePosition); assertEquals(1, logicalPositionBefore.softWrapLinesOnCurrentLogicalLine); assertTrue(logicalPositionBefore.column > 0); SoftWrap softWrap = getSoftWrapModel().getSoftWrap(offsetBefore); assertNotNull(softWrap); type('a'); LogicalPosition logicalPositionAfter = myEditor.visualToLogicalPosition(changePosition); assertEquals(new LogicalPosition(1, 0, 0, 0, 0, 0, 0), logicalPositionAfter); assertEquals( offsetBefore + softWrap.getText().length() + 1, myEditor.getCaretModel().getOffset()); assertEquals(logicalLinesBefore + 1, myEditor.offsetToLogicalPosition(text.length()).line); }
private VerticalInfo createVerticalInfo(LogicalPosition position) { Document document = myEditor.getDocument(); int logicalLine = position.line; if (logicalLine >= document.getLineCount()) { logicalLine = Math.max(0, document.getLineCount() - 1); } int startOffset = document.getLineStartOffset(logicalLine); int endOffset = document.getLineEndOffset(logicalLine); // There is a possible case that active logical line is represented on multiple lines due to // soft wraps processing. // We want to highlight those visual lines as 'active' then, so, we calculate 'y' position for // the logical line start // and height in accordance with the number of occupied visual lines. VisualPosition visualPosition = myEditor.offsetToVisualPosition(document.getLineStartOffset(logicalLine)); int y = myEditor.visualPositionToXY(visualPosition).y; int lineHeight = myEditor.getLineHeight(); int height = lineHeight; List<? extends SoftWrap> softWraps = myEditor.getSoftWrapModel().getSoftWrapsForRange(startOffset, endOffset); for (SoftWrap softWrap : softWraps) { height += StringUtil.countNewLines(softWrap.getText()) * lineHeight; } return new VerticalInfo(y, height); }
@Override public void beforeDocumentChangeAtCaret() { CaretModel caretModel = myEditor.getCaretModel(); VisualPosition visualCaretPosition = caretModel.getVisualPosition(); if (!isInsideSoftWrap(visualCaretPosition)) { return; } SoftWrap softWrap = myStorage.getSoftWrap(caretModel.getOffset()); if (softWrap == null) { return; } myEditor .getDocument() .replaceString(softWrap.getStart(), softWrap.getEnd(), softWrap.getText()); caretModel.moveToVisualPosition(visualCaretPosition); }