Java Code Examples for com.intellij.codeInsight.folding.CodeFoldingManager#getInstance()

The following examples show how to use com.intellij.codeInsight.folding.CodeFoldingManager#getInstance() . 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: CollapseExpandDocCommentsHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredWriteAction
@Override
public void invokeInWriteAction(@Nonnull Project project, @Nonnull final Editor editor, @Nonnull PsiFile file){
  PsiDocumentManager.getInstance(project).commitAllDocuments();

  CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
  foldingManager.updateFoldRegions(editor);
  final FoldRegion[] allFoldRegions = editor.getFoldingModel().getAllFoldRegions();
  Runnable processor = new Runnable() {
    @Override
    public void run() {
      for (FoldRegion region : allFoldRegions) {
        PsiElement element = EditorFoldingInfo.get(editor).getPsiElement(region);
        if (element instanceof PsiDocCommentBase) {
          region.setExpanded(myExpand);
        }
      }
    }
  };
  editor.getFoldingModel().runBatchFoldingOperation(processor);
}
 
Example 2
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 3
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 4
Source File: RestoreFoldArrangementCallback.java    From consulo with Apache License 2.0 5 votes vote down vote up
public RestoreFoldArrangementCallback(@Nonnull Editor editor) {
  myEditor = editor;

  Project project = editor.getProject();
  if (project == null) {
    myCodeFoldingState = null;
  }
  else {
    final CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(editor.getProject());
    myCodeFoldingState = foldingManager.saveFoldingState(editor);
  }
}
 
Example 5
Source File: RestoreFoldArrangementCallback.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void afterArrangement(@Nonnull final List<ArrangementMoveInfo> moveInfos) {
  // Restore state for the PSI elements not affected by arrangement.
  Project project = myEditor.getProject();
  if (myCodeFoldingState != null && project != null) {
    final CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(project);
    foldingManager.updateFoldRegions(myEditor);
    myEditor.getFoldingModel().runBatchFoldingOperation(new Runnable() {
      @Override
      public void run() {
        foldingManager.restoreFoldingState(myEditor, myCodeFoldingState);
      }
    });
  }
}
 
Example 6
Source File: DesktopPsiAwareTextEditorImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void dispose() {
  CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(myProject);
  if (foldingManager != null) {
    foldingManager.releaseFoldings(getEditor());
  }
  super.dispose();
}