Java Code Examples for com.intellij.openapi.editor.FoldRegion#getEndOffset()

The following examples show how to use com.intellij.openapi.editor.FoldRegion#getEndOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CopyPasteFoldingProcessor.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public List<FoldingTransferableData> collectTransferableData(final PsiFile file, final Editor editor, final int[] startOffsets, final int[] endOffsets) {
  // might be slow
  //CodeFoldingManager.getInstance(file.getManager().getProject()).updateFoldRegions(editor);

  final ArrayList<FoldingData> list = new ArrayList<FoldingData>();
  final FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
  for (final FoldRegion region : regions) {
    if (!region.isValid()) continue;
    for (int j = 0; j < startOffsets.length; j++) {
      if (startOffsets[j] <= region.getStartOffset() && region.getEndOffset() <= endOffsets[j]) {
        list.add(
                new FoldingData(
                        region.getStartOffset() - startOffsets[j],
                        region.getEndOffset() - startOffsets[j],
                        region.isExpanded()
                )
        );
      }
    }
  }

  return Collections.singletonList(new FoldingTransferableData(list.toArray(new FoldingData[list.size()])));
}
 
Example 2
Source File: BaseFoldingHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Returns fold regions inside selection, or all regions in editor, if selection doesn't exist or doesn't contain fold regions.
 */
protected List<FoldRegion> getFoldRegionsForSelection(@Nonnull Editor editor, @Nullable Caret caret) {
  FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions();
  if (caret == null) {
    caret = editor.getCaretModel().getPrimaryCaret();
  }
  if (caret.hasSelection()) {
    List<FoldRegion> result = new ArrayList<>();
    for (FoldRegion region : allRegions) {
      if (region.getStartOffset() >= caret.getSelectionStart() && region.getEndOffset() <= caret.getSelectionEnd()) {
        result.add(region);
      }
    }
    if (!result.isEmpty()) {
      return result;
    }
  }
  return Arrays.asList(allRegions);
}
 
Example 3
Source File: FoldingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static FoldRegion[] getFoldRegionsAtOffset(Editor editor, int offset) {
  List<FoldRegion> list = new ArrayList<>();
  FoldRegion[] allRegions = editor.getFoldingModel().getAllFoldRegions();
  for (FoldRegion region : allRegions) {
    if (region.getStartOffset() <= offset && offset <= region.getEndOffset()) {
      list.add(region);
    }
  }

  FoldRegion[] regions = list.toArray(FoldRegion.EMPTY_ARRAY);
  Arrays.sort(regions, Collections.reverseOrder(RangeMarker.BY_START_OFFSET));
  return regions;
}
 
Example 4
Source File: VisualLinesIterator.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean isCollapsed(int offset) {
  while (foldRegion < myFoldRegions.length) {
    FoldRegion region = myFoldRegions[foldRegion];
    if (offset <= region.getStartOffset()) return false;
    if (offset <= region.getEndOffset()) return true;
    foldRegion++;
  }
  return false;
}
 
Example 5
Source File: FoldingUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean isTextRangeFolded(@Nonnull Editor editor, @Nonnull TextRange range) {
  FoldRegion foldRegion = editor.getFoldingModel().getCollapsedRegionAtOffset(range.getStartOffset());
  return foldRegion != null && range.getEndOffset() <= foldRegion.getEndOffset();
}
 
Example 6
Source File: VisualLineFragmentsIterator.java    From consulo with Apache License 2.0 4 votes vote down vote up
private float getXForOffsetInsideFoldRegion(FoldRegion foldRegion, int offset) {
  return offset < foldRegion.getEndOffset() ? 0 : getFoldRegionWidthInPixels(foldRegion);
}
 
Example 7
Source File: FoldRegionsTree.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public int computeHashCode(FoldRegion o) {
  return o.getStartOffset() * 31 + o.getEndOffset();
}
 
Example 8
Source File: FoldRegionsTree.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean equals(FoldRegion o1, FoldRegion o2) {
  return o1.getStartOffset() == o2.getStartOffset() && o1.getEndOffset() == o2.getEndOffset();
}
 
Example 9
Source File: FoldRegionsTree.java    From consulo with Apache License 2.0 4 votes vote down vote up
static boolean containsStrict(FoldRegion region, int offset) {
  return region.getStartOffset() < offset && offset < region.getEndOffset();
}
 
Example 10
Source File: FoldingAnchorsOverlayStrategy.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
Collection<DisplayedFoldingAnchor> getAnchorsToDisplay(int firstVisibleOffset, int lastVisibleOffset, @Nonnull List<FoldRegion> activeFoldRegions) {
  Map<Integer, DisplayedFoldingAnchor> result = new HashMap<>();
  FoldRegion[] visibleFoldRegions = myEditor.getFoldingModel().fetchVisible();
  if (visibleFoldRegions != null) {
    for (FoldRegion region : visibleFoldRegions) {
      if (!region.isValid()) continue;
      final int startOffset = region.getStartOffset();
      if (startOffset > lastVisibleOffset) continue;
      final int endOffset = region.getEndOffset();
      if (endOffset < firstVisibleOffset) continue;

      boolean singleLine = false;
      int startLogicalLine = myEditor.getDocument().getLineNumber(startOffset);
      int endLogicalLine = myEditor.getDocument().getLineNumber(endOffset);
      if (startLogicalLine == endLogicalLine) {
        singleLine = true;
        if (!region.isGutterMarkEnabledForSingleLine() &&
            (!myEditor.getSettings().isAllowSingleLogicalLineFolding() || (endOffset - startOffset) <= 1 ||
             myEditor.getSoftWrapModel().getSoftWrapsForRange(startOffset + 1, endOffset - 1).isEmpty())) {
          // unless requested, we don't display markers for single-line fold regions
          continue;
        }
      }

      int foldStart = myEditor.offsetToVisualLine(startOffset);
      if (!region.isExpanded()) {
        tryAdding(result, region, foldStart, 0, singleLine ? DisplayedFoldingAnchor.Type.COLLAPSED_SINGLE_LINE : DisplayedFoldingAnchor.Type.COLLAPSED, activeFoldRegions);
      }
      else {
        int foldEnd = myEditor.offsetToVisualLine(endOffset);
        if (foldStart == foldEnd) {
          tryAdding(result, region, foldStart, 0, DisplayedFoldingAnchor.Type.EXPANDED_SINGLE_LINE, activeFoldRegions);
        }
        else {
          tryAdding(result, region, foldStart, foldEnd - foldStart, DisplayedFoldingAnchor.Type.EXPANDED_TOP, activeFoldRegions);
          tryAdding(result, region, foldEnd, foldEnd - foldStart, DisplayedFoldingAnchor.Type.EXPANDED_BOTTOM, activeFoldRegions);
        }
      }
    }
  }
  return result.values();
}