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

The following examples show how to use com.intellij.openapi.editor.FoldRegion#isExpanded() . 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
protected TIntFunction getLineConvertor(final int index) {
  return new TIntFunction() {
    @Override
    public int execute(int value) {
      updateLineNumbers(false);
      for (FoldedBlock folding : getFoldedBlocks()) { // TODO: avoid full scan - it could slowdown painting
        int line = folding.getLine(index);
        if (line == -1) continue;
        if (line > value) break;
        FoldRegion region = folding.getRegion(index);
        if (line == value && region != null && !region.isExpanded()) return -1;
      }
      return value;
    }
  };
}
 
Example 2
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 3
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 4
Source File: ExpandRegionAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void expandRegionAtOffset(@Nonnull Project project, @Nonnull final Editor editor, final int offset) {
  CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
  foldingManager.updateFoldRegions(editor);

  final int line = editor.getDocument().getLineNumber(offset);
  Runnable processor = () -> {
    FoldRegion region = FoldingUtil.findFoldRegionStartingAtLine(editor, line);
    if (region != null && !region.isExpanded()) {
      region.setExpanded(true);
    }
    else {
      FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, offset);
      for (int i = regions.length - 1; i >= 0; i--) {
        region = regions[i];
        if (!region.isExpanded()) {
          region.setExpanded(true);
          break;
        }
      }
    }
  };
  editor.getFoldingModel().runBatchFoldingOperation(processor);
}
 
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: 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 7
Source File: CollapseRegionAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
public CollapseRegionAction() {
  super(new BaseFoldingHandler() {
    @Override
    public void doExecute(@Nonnull final Editor editor, @Nullable Caret caret, DataContext dataContext) {
      CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(editor.getProject());
      foldingManager.updateFoldRegions(editor);

      final int line = editor.getCaretModel().getLogicalPosition().line;

      Runnable processor = () -> {
        FoldRegion region = FoldingUtil.findFoldRegionStartingAtLine(editor, line);
        if (region != null && region.isExpanded()) {
          region.setExpanded(false);
        }
        else {
          int offset = editor.getCaretModel().getOffset();
          FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, offset);
          for (FoldRegion region1 : regions) {
            if (region1.isExpanded()) {
              region1.setExpanded(false);
              break;
            }
          }
        }
      };
      editor.getFoldingModel().runBatchFoldingOperation(processor);
    }
  });
}
 
Example 8
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 9
Source File: XBreakpointUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Toggle line breakpoint with editor support:
 * - unfolds folded block on the line
 * - if folded, checks if line breakpoints could be toggled inside folded text
 */
@Nonnull
public static AsyncResult<XLineBreakpoint> toggleLineBreakpoint(@Nonnull Project project,
                                                                @Nonnull XSourcePosition position,
                                                                @Nullable Editor editor,
                                                                boolean temporary,
                                                                boolean moveCaret) {
  int lineStart = position.getLine();
  VirtualFile file = position.getFile();
  // for folded text check each line and find out type with the biggest priority
  int linesEnd = lineStart;
  if (editor != null) {
    FoldRegion region = FoldingUtil.findFoldRegionStartingAtLine(editor, lineStart);
    if (region != null && !region.isExpanded()) {
      linesEnd = region.getDocument().getLineNumber(region.getEndOffset());
    }
  }

  final XBreakpointManager breakpointManager = XDebuggerManager.getInstance(project).getBreakpointManager();
  XLineBreakpointType<?>[] lineTypes = XDebuggerUtil.getInstance().getLineBreakpointTypes();
  XLineBreakpointType<?> typeWinner = null;
  int lineWinner = -1;
  for (int line = lineStart; line <= linesEnd; line++) {
    for (XLineBreakpointType<?> type : lineTypes) {
      final XLineBreakpoint<? extends XBreakpointProperties> breakpoint = breakpointManager.findBreakpointAtLine(type, file, line);
      if (breakpoint != null && temporary && !breakpoint.isTemporary()) {
        breakpoint.setTemporary(true);
      }
      else if(breakpoint != null) {
        typeWinner = type;
        lineWinner = line;
        break;
      }
    }

    XLineBreakpointType<?> breakpointType = XLineBreakpointResolverTypeExtension.INSTANCE.resolveBreakpointType(project, file, line);
    if(breakpointType != null) {
      typeWinner = breakpointType;
      lineWinner = line;
    }

    // already found max priority type - stop
    if (typeWinner != null) {
      break;
    }
  }

  if (typeWinner != null) {
    XSourcePosition winPosition = (lineStart == lineWinner) ? position : XSourcePositionImpl.create(file, lineWinner);
    if (winPosition != null) {
      AsyncResult<XLineBreakpoint> res =
              XDebuggerUtilImpl.toggleAndReturnLineBreakpoint(project, typeWinner, winPosition, temporary, editor);

      if (editor != null && lineStart != lineWinner) {
        int offset = editor.getDocument().getLineStartOffset(lineWinner);
        ExpandRegionAction.expandRegionAtOffset(project, editor, offset);
        if (moveCaret) {
          editor.getCaretModel().moveToOffset(offset);
        }
      }
      return res;
    }
  }

  return AsyncResult.rejected();
}