Search in sources :

Example 1 with SoftWrap

use of com.intellij.openapi.editor.SoftWrap in project intellij-community by JetBrains.

the class SoftWrapTest method doTestSoftWraps.

private void doTestSoftWraps(int wrapWidth, String text) throws IOException {
    List<MyFoldRegion> foldRegions = new ArrayList<>();
    List<Integer> wrapPositions = new ArrayList<>();
    int foldInsertPosition = 0;
    int pos = 0;
    int docPos = 0;
    Matcher matcher = Pattern.compile(TAGS_PATTERN).matcher(text);
    StringBuilder cleanedText = new StringBuilder();
    while (matcher.find()) {
        cleanedText.append(text.substring(pos, matcher.start()));
        docPos += matcher.start() - pos;
        pos = matcher.end();
        if (matcher.group(1) != null) {
            // <fold>
            foldRegions.add(foldInsertPosition++, new MyFoldRegion(docPos, matcher.group(3), matcher.group(2) != null));
        } else if (matcher.group(4) != null) {
            // </fold>
            assertTrue("Misplaced closing fold marker tag: " + text, foldInsertPosition > 0);
            foldRegions.get(--foldInsertPosition).endPos = docPos;
        } else {
            // <wrap>
            wrapPositions.add(docPos);
        }
    }
    assertTrue("Missing closing fold marker tag: " + text, foldInsertPosition == 0);
    cleanedText.append(text.substring(pos));
    init(cleanedText.toString(), TestFileType.TEXT);
    for (MyFoldRegion region : foldRegions) {
        FoldRegion r = addFoldRegion(region.startPos, region.endPos, region.placeholder);
        if (region.collapse) {
            toggleFoldRegionState(r, false);
        }
    }
    EditorTestUtil.configureSoftWraps(myEditor, wrapWidth);
    List<Integer> actualWrapPositions = new ArrayList<>();
    for (SoftWrap wrap : myEditor.getSoftWrapModel().getSoftWrapsForRange(0, myEditor.getDocument().getTextLength())) {
        actualWrapPositions.add(wrap.getStart());
    }
    assertEquals("Wrong wrap positions", wrapPositions, actualWrapPositions);
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) FoldRegion(com.intellij.openapi.editor.FoldRegion) SoftWrap(com.intellij.openapi.editor.SoftWrap)

Example 2 with SoftWrap

use of com.intellij.openapi.editor.SoftWrap in project intellij-community by JetBrains.

the class SoftWrapHelper method isCaretAfterSoftWrap.

/**
   * Every soft wrap implies that multiple visual positions correspond to the same document offset. We can classify
   * such positions by the following criteria:
   * <pre>
   * <ul>
   *   <li>positions from visual line with soft wrap start;</li>
   *   <li>positions from visual line with soft wrap end;</li>
   * </ul>
   * </pre>
   * <p/>
   * This method allows to answer if caret offset of the given editor points to soft wrap and visual caret position
   * belongs to the visual line where soft wrap end is located.
   *
   * @return          <code>true</code> if caret offset of the given editor points to visual position that belongs to
   *                  visual line where soft wrap end is located
   */
public static boolean isCaretAfterSoftWrap(CaretImpl caret) {
    if (!caret.isUpToDate()) {
        return false;
    }
    EditorImpl editor = caret.getEditor();
    SoftWrapModel softWrapModel = editor.getSoftWrapModel();
    int offset = caret.getOffset();
    SoftWrap softWrap = softWrapModel.getSoftWrap(offset);
    if (softWrap == null) {
        return false;
    }
    VisualPosition afterWrapPosition = editor.offsetToVisualPosition(offset, false, false);
    VisualPosition caretPosition = caret.getVisualPosition();
    return caretPosition.line == afterWrapPosition.line && caretPosition.column <= afterWrapPosition.column;
}
Also used : EditorImpl(com.intellij.openapi.editor.impl.EditorImpl) SoftWrapModel(com.intellij.openapi.editor.SoftWrapModel) SoftWrap(com.intellij.openapi.editor.SoftWrap) VisualPosition(com.intellij.openapi.editor.VisualPosition)

Example 3 with SoftWrap

use of com.intellij.openapi.editor.SoftWrap in project intellij-community by JetBrains.

the class SoftWrapsStorage method getSoftWrapIndex.

/**
   * Tries to find index of the target soft wrap stored at {@link #myWraps} collection. <code>'Target'</code> soft wrap is the one
   * that starts at the given offset.
   *
   * @param offset    target offset
   * @return          index that conforms to {@link Collections#binarySearch(List, Object)} contract, i.e. non-negative returned
   *                  index points to soft wrap that starts at the given offset; <code>'-(negative value) - 1'</code> points
   *                  to position at {@link #myWraps} collection where soft wrap for the given index should be inserted
   */
public int getSoftWrapIndex(int offset) {
    int start = 0;
    int end = myWraps.size() - 1;
    // is a bottleneck. The most probable reason is a big number of interface calls.
    while (start <= end) {
        int i = (start + end) >>> 1;
        SoftWrap softWrap = myWraps.get(i);
        int softWrapOffset = softWrap.getStart();
        if (softWrapOffset > offset) {
            end = i - 1;
        } else if (softWrapOffset < offset) {
            start = i + 1;
        } else {
            return i;
        }
    }
    return -(start + 1);
}
Also used : SoftWrap(com.intellij.openapi.editor.SoftWrap)

Example 4 with SoftWrap

use of com.intellij.openapi.editor.SoftWrap in project intellij-community by JetBrains.

the class IncrementalCacheUpdateEvent method getVisualLineInfo.

private static VisualLineInfo getVisualLineInfo(@NotNull EditorImpl editor, int offset, boolean beforeSoftWrap) {
    Document document = editor.getDocument();
    int textLength = document.getTextLength();
    if (offset <= 0 || textLength == 0)
        return new VisualLineInfo(0, false);
    offset = Math.min(offset, textLength);
    int startOffset = EditorUtil.getNotFoldedLineStartOffset(editor, offset);
    SoftWrapModelImpl softWrapModel = editor.getSoftWrapModel();
    int wrapIndex = softWrapModel.getSoftWrapIndex(offset);
    int prevSoftWrapIndex = wrapIndex < 0 ? (-wrapIndex - 2) : wrapIndex - (beforeSoftWrap ? 1 : 0);
    SoftWrap prevSoftWrap = prevSoftWrapIndex < 0 ? null : softWrapModel.getRegisteredSoftWraps().get(prevSoftWrapIndex);
    int visualLineStartOffset = prevSoftWrap == null ? startOffset : Math.max(startOffset, prevSoftWrap.getStart());
    return new VisualLineInfo(visualLineStartOffset, prevSoftWrap != null && prevSoftWrap.getStart() == visualLineStartOffset);
}
Also used : SoftWrapModelImpl(com.intellij.openapi.editor.impl.SoftWrapModelImpl) SoftWrap(com.intellij.openapi.editor.SoftWrap) Document(com.intellij.openapi.editor.Document)

Example 5 with SoftWrap

use of com.intellij.openapi.editor.SoftWrap in project intellij-community by JetBrains.

the class SoftWrapsStorage method getNumberOfSoftWrapsInRange.

/**
   * Allows to answer how many soft wraps which {@link TextChange#getStart() start offsets} belong to given
   * <code>[start; end]</code> interval are registered withing the current storage.
   * 
   * @param startOffset   target start offset (inclusive)
   * @param endOffset     target end offset (inclusive)
   * @return              number of soft wraps which {@link TextChange#getStart() start offsets} belong to the target range
   */
public int getNumberOfSoftWrapsInRange(int startOffset, int endOffset) {
    int startIndex = getSoftWrapIndex(startOffset);
    if (startIndex < 0) {
        startIndex = -startIndex - 1;
    }
    if (startIndex >= myWraps.size()) {
        return 0;
    }
    int result = 0;
    int endIndex = startIndex;
    for (; endIndex < myWraps.size(); endIndex++) {
        SoftWrap softWrap = myWraps.get(endIndex);
        if (softWrap.getStart() > endOffset) {
            break;
        }
        result++;
    }
    return result;
}
Also used : SoftWrap(com.intellij.openapi.editor.SoftWrap)

Aggregations

SoftWrap (com.intellij.openapi.editor.SoftWrap)6 FoldRegion (com.intellij.openapi.editor.FoldRegion)2 EditorImpl (com.intellij.openapi.editor.impl.EditorImpl)2 SoftWrapModelImpl (com.intellij.openapi.editor.impl.SoftWrapModelImpl)2 Document (com.intellij.openapi.editor.Document)1 SoftWrapModel (com.intellij.openapi.editor.SoftWrapModel)1 VisualPosition (com.intellij.openapi.editor.VisualPosition)1 FoldingModelEx (com.intellij.openapi.editor.ex.FoldingModelEx)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1