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

The following examples show how to use com.intellij.openapi.editor.FoldRegion#isValid() . 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: FoldingModelSupport.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private BooleanGetter getHighlighterCondition(@Nonnull final FoldedBlock[] block, final int index) {
  return new BooleanGetter() {
    @Override
    public boolean get() {
      if (!myEditors[index].getFoldingModel().isFoldingEnabled()) return false;

      for (FoldedBlock folding : block) {
        FoldRegion region = folding.getRegion(index);
        boolean visible = region != null && region.isValid() && !region.isExpanded();
        if (folding == FoldedBlock.this) return visible;
        if (visible) return false; // do not paint separator, if 'parent' folding is collapsed
      }
      return false;
    }
  };
}
 
Example 2
Source File: FoldingUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static FoldRegion findFoldRegionStartingAtLine(@Nonnull Editor editor, int line) {
  if (line < 0 || line >= editor.getDocument().getLineCount()) {
    return null;
  }
  FoldRegion result = null;
  FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
  for (FoldRegion region : regions) {
    if (!region.isValid()) {
      continue;
    }
    if (region.getDocument().getLineNumber(region.getStartOffset()) == line) {
      if (result != null) return null;
      result = region;
    }
  }
  return result;
}
 
Example 3
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 4
Source File: DocumentFoldingInfo.java    From consulo with Apache License 2.0 6 votes vote down vote up
void loadFromEditor(@Nonnull Editor editor) {
  ApplicationManager.getApplication().assertIsDispatchThread();
  LOG.assertTrue(!editor.isDisposed());
  clear();

  FoldRegion[] foldRegions = editor.getFoldingModel().getAllFoldRegions();
  for (FoldRegion region : foldRegions) {
    if (!region.isValid()) continue;
    boolean expanded = region.isExpanded();
    String signature = region.getUserData(UpdateFoldRegionsOperation.SIGNATURE);
    if (signature == UpdateFoldRegionsOperation.NO_SIGNATURE) continue;
    Boolean storedCollapseByDefault = region.getUserData(UpdateFoldRegionsOperation.COLLAPSED_BY_DEFAULT);
    boolean collapseByDefault = storedCollapseByDefault != null && storedCollapseByDefault && !FoldingUtil.caretInsideRange(editor, TextRange.create(region));
    if (collapseByDefault == expanded || signature == null) {
      if (signature != null) {
        myInfos.add(new Info(signature, expanded));
      }
      else {
        RangeMarker marker = editor.getDocument().createRangeMarker(region.getStartOffset(), region.getEndOffset());
        myRangeMarkers.add(marker);
        marker.putUserData(FOLDING_INFO_KEY, new FoldingInfo(region.getPlaceholderText(), expanded));
      }
    }
  }
}
 
Example 5
Source File: FoldingModelSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void process(@Nonnull Handler handler) {
  for (FoldedBlock[] block : myFoldings) {
    for (FoldedBlock folding : block) {
      FoldRegion region1 = folding.getRegion(myLeft);
      FoldRegion region2 = folding.getRegion(myRight);
      if (region1 == null || !region1.isValid() || region1.isExpanded()) continue;
      if (region2 == null || !region2.isValid() || region2.isExpanded()) continue;
      int line1 = myEditors[myLeft].getDocument().getLineNumber(region1.getStartOffset());
      int line2 = myEditors[myRight].getDocument().getLineNumber(region2.getStartOffset());
      if (!handler.process(line1, line2)) return;
      break;
    }
  }
}
 
Example 6
Source File: FoldingModelSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void updateLineNumber(int index) {
  FoldRegion region = myRegions[index];
  if (region == null || !region.isValid()) {
    myLines[index] = -1;
  }
  else {
    myLines[index] = myEditors[index].getDocument().getLineNumber(region.getStartOffset());
  }
}
 
Example 7
Source File: FoldingTransformation.java    From consulo with Apache License 2.0 5 votes vote down vote up
public FoldingTransformation(Editor editor) {
  myEditor = editor;
  FoldRegion[] foldRegions = myEditor.getFoldingModel().getAllFoldRegions();
  Arrays.sort(foldRegions, RangeMarker.BY_START_OFFSET);
  TIntArrayList foldBeginings = new TIntArrayList();
  for (FoldRegion foldRegion : foldRegions) {
    if (!foldRegion.isValid() || foldRegion.isExpanded()) continue;
    foldBeginings.add(getStartLine(foldRegion));
    myCollapsed.add(foldRegion);
  }
  myFoldBeginings = foldBeginings.toNativeArray();
}
 
Example 8
Source File: FoldingUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static FoldRegion findFoldRegion(@Nonnull Editor editor, int startOffset, int endOffset) {
  FoldRegion region = editor.getFoldingModel().getFoldRegion(startOffset, endOffset);
  return region != null && region.isValid() ? region : null;
}
 
Example 9
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();
}
 
Example 10
Source File: UpdateFoldRegionsOperation.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean regionCanBeRemovedWhenCollapsed(FoldRegion region) {
  return Boolean.TRUE.equals(region.getUserData(CAN_BE_REMOVED_WHEN_COLLAPSED)) ||
         ((FoldingModelEx)myEditor.getFoldingModel()).hasDocumentRegionChangedFor(region) ||
         !region.isValid() ||
         isRegionInCaretLine(region);
}