Java 类com.intellij.openapi.editor.FoldRegion 实例源码

项目:logger-folding    文件:FoldLoggerMethodCallsAction.java   
/**
 * Runs a batch folding operation that folds the given text range
 *
 * @param editor          the editor to get the folding model
 * @param textRange       the text range to fold
 * @param placeholderText the fold region placeholder text
 */
private void fold(@NotNull final Editor editor, @NotNull final TextRange textRange, @NotNull final String placeholderText) {

  editor.getFoldingModel().runBatchFoldingOperation(() -> {

    FoldRegion foldRegion = editor.getFoldingModel()
        .getFoldRegion(textRange.getStartOffset(), textRange.getEndOffset());
    if (foldRegion == null) {
      foldRegion = editor.getFoldingModel()
          .addFoldRegion(textRange.getStartOffset(), textRange.getEndOffset(),
              placeholderText);
    }
    if (foldRegion != null) {
      foldRegion.setExpanded(false);
    }
  });
}
项目:intellij-ce-playground    文件:JavaFileEditorManagerTest.java   
public void testFoldingIsNotBlinkingOnNavigationToSingleLineMethod() {
  VirtualFile file = getFile("/src/Bar.java");
  PsiJavaFile psiFile = (PsiJavaFile)getPsiManager().findFile(file);
  assertNotNull(psiFile);
  PsiMethod method = psiFile.getClasses()[0].getMethods()[0];
  method.navigate(true);

  FileEditor[] editors = myManager.getEditors(file);
  assertEquals(1, editors.length);
  Editor editor = ((TextEditor)editors[0]).getEditor();
  FoldRegion[] regions = editor.getFoldingModel().getAllFoldRegions();
  assertEquals(2, regions.length);
  assertTrue(regions[0].isExpanded());
  assertTrue(regions[1].isExpanded());

  CodeInsightTestFixtureImpl.instantiateAndRun(psiFile, editor, new int[]{Pass.UPDATE_ALL, Pass.LOCAL_INSPECTIONS}, false);

  regions = editor.getFoldingModel().getAllFoldRegions();
  assertEquals(2, regions.length);
  assertTrue(regions[0].isExpanded());
  assertTrue(regions[1].isExpanded());
}
项目:intellij-ce-playground    文件:FoldingProcessingOnDocumentModificationTest.java   
public void testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange() throws IOException {
  // Inspired by IDEA-61275

  String text =
    "/**\n" +
    " * This is a test comment\n" +
    " */\n" +
    "public <caret>class Test {\n" +
    "}";
  init(text, TestFileType.JAVA);

  CaretModel caretModel = myEditor.getCaretModel();
  int caretOffset = caretModel.getOffset();

  assertEquals(caretOffset, caretModel.getOffset());

  updateFoldRegions();
  toggleFoldRegionState(getFoldRegion(0), false);
  type('a');
  updateFoldRegions();

  assertEquals(caretOffset + 1, caretModel.getOffset());
  assertEquals(1, myEditor.getFoldingModel().getAllFoldRegions().length);
  FoldRegion foldRegion = getFoldRegion(0);
  assertFalse(foldRegion.isExpanded());
}
项目:intellij-ce-playground    文件:FoldingModelSupport.java   
@Nullable
private FoldedBlock createRange(int[] starts, int[] ends, boolean expanded) {
  boolean hasFolding = false;
  FoldRegion[] regions = new FoldRegion[myCount];
  boolean hasExpanded = false; // do not desync on runBatchFoldingOperationDoNotCollapseCaret

  for (int i = 0; i < myCount; i++) {
    if (ends[i] - starts[i] < 2) continue;
    regions[i] = addFolding(myEditors[i], starts[i], ends[i], expanded);
    hasFolding |= regions[i] != null;
    hasExpanded |= regions[i] != null && regions[i].isExpanded();
  }
  if (hasExpanded && !expanded) {
    for (FoldRegion region : regions) {
      if (region != null) region.setExpanded(true);
    }
  }
  return hasFolding ? new FoldedBlock(regions) : null;
}
项目:intellij-ce-playground    文件:FoldingModelSupport.java   
@NotNull
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;
    }
  };
}
项目:intellij-ce-playground    文件:FoldingModelSupport.java   
public void expandAll(final boolean expanded) {
  if (myDuringSynchronize) return;
  myDuringSynchronize = true;
  try {
    for (int i = 0; i < myCount; i++) {
      final int index = i;
      final FoldingModelEx model = myEditors[index].getFoldingModel();
      model.runBatchFoldingOperation(new Runnable() {
        @Override
        public void run() {
          for (FoldedBlock folding : getFoldedBlocks()) {
            FoldRegion region = folding.getRegion(index);
            if (region != null) region.setExpanded(expanded);
          }
        }
      });
    }
  }
  finally {
    myDuringSynchronize = false;
  }
}
项目:intellij-ce-playground    文件:FoldingModelSupport.java   
@NotNull
private BooleanGetter getHighlighterCondition(@NotNull 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;
    }
  };
}
项目:intellij-ce-playground    文件:AbstractMappingStrategy.java   
protected void advancePositionOnFolding(@NotNull EditorPosition position, @NotNull FoldRegion foldRegion) {
  Document document = myEditor.getDocument();
  int endOffsetLogicalLine = document.getLineNumber(foldRegion.getEndOffset());
  if (position.logicalLine != endOffsetLogicalLine) {
    // Multi-line fold region.
    position.softWrapColumnDiff = 0;
    position.softWrapLinesBefore += position.softWrapLinesCurrent;
    position.softWrapLinesCurrent = 0;
  }

  int collapsedSymbolsWidthInColumns = -1;
  FoldingData foldingData = getFoldRegionData(foldRegion);
  if (foldingData != null) {
    collapsedSymbolsWidthInColumns = foldingData.getCollapsedSymbolsWidthInColumns();
  }

  position.advance(foldRegion, collapsedSymbolsWidthInColumns);
}
项目:intellij-ce-playground    文件:CacheEntry.java   
@Nullable
public FoldingData getFoldingData(@NotNull final FoldRegion region) {
  FoldingData candidate = myFoldingData.get(region.getStartOffset());
  if (candidate != null) {
    return candidate;
  }

  // Folding implementation is known to postpone actual fold region offsets update on document change, i.e. it performs
  // fold data caching with its further replace by up-to-date info. Hence, there is a possible case that soft wraps processing
  // advances fold region offset but folding model still provides old cached values. Hence, we're trying to match exact given
  // fold region against the cached data here.
  final Ref<FoldingData> result = new Ref<FoldingData>();
  myFoldingData.forEachValue(new TObjectProcedure<FoldingData>() {
    @Override
    public boolean execute(FoldingData data) {
      if (data.getFoldRegion().equals(region)) {
        result.set(data);
        return false;
      }
      return true;
    }
  });
  return result.get();
}
项目:intellij-ce-playground    文件:FoldingDataProvider.java   
@Override
public void advance(int sortingKey) {
  // We inline binary search here because profiling indicates that as a performance boost.
  int start = myIndex;
  int end = myFoldRegions.length - 1;

  // We inline binary search here because profiling indicates that it becomes bottleneck to use Collections.binarySearch().
  while (start <= end) {
    int i = (end + start) >>> 1;
    FoldRegion foldRegion = myFoldRegions[i];
    if (foldRegion.getStartOffset() < sortingKey) {
      start = i + 1;
      continue;
    }
    if (foldRegion.getStartOffset() > sortingKey) {
      end = i - 1;
      continue;
    }

    myIndex = i;
    scrollToInterested();
    return;
  }
  myIndex = start;
  scrollToInterested();
}
项目:intellij-ce-playground    文件:LogicalToOffsetCalculationStrategy.java   
@Override
public Integer processFoldRegion(@NotNull EditorPosition position, @NotNull FoldRegion foldRegion) {
  int startLine = position.logicalLine;
  int startColumn = position.logicalColumn;
  int startX = position.x;

  advancePositionOnFolding(position, foldRegion);

  if (position.logicalLine < targetPosition.line
      || position.logicalLine == targetPosition.line && position.logicalColumn <= targetPosition.column) {
    return null;
  }

  Document document = myEditor.getDocument();
  int lineEndOffset = document.getLineEndOffset(targetPosition.line);
  int result = SoftWrapModelImpl.getEditorTextRepresentationHelper(myEditor)
    .calcSoftWrapUnawareOffset(targetPosition.line == startLine ? foldRegion.getStartOffset()
                                                                : document.getLineStartOffset(targetPosition.line),
                               lineEndOffset,
                               targetPosition.line == startLine ? startColumn : 0,
                               targetPosition.column,
                               targetPosition.line == startLine ? startX : 0);
  return result < 0 ? lineEndOffset : result;
}
项目:intellij-ce-playground    文件:FoldingAnchorsOverlayStrategy.java   
private static void tryAdding(Map<Integer, DisplayedFoldingAnchor> resultsMap,
                       FoldRegion region,
                       int visualLine,
                       int visualHeight,
                       DisplayedFoldingAnchor.Type type,
                       FoldRegion activeRegion) {
  DisplayedFoldingAnchor prev = resultsMap.get(visualLine);
  if (prev != null) {
    if (prev.foldRegion == activeRegion) {
      return;
    }
    if (region != activeRegion && prev.foldRegionVisualLines < visualHeight) {
      return;
    }
  }
  resultsMap.put(visualLine, new DisplayedFoldingAnchor(region, visualLine, visualHeight, type));
}
项目:intellij-ce-playground    文件:SelectUnselectOccurrenceActionsTest.java   
public void testOccurrenceInCollapsedRegion() throws Exception {
  init("normal <selection><caret>line</selection>\n" +
       "collapsed line");
  final FoldingModel foldingModel = myFixture.getEditor().getFoldingModel();
  final Document document = myFixture.getEditor().getDocument();
  foldingModel.runBatchFoldingOperation(new Runnable() {
    @Override
    public void run() {
      FoldRegion foldRegion = foldingModel.addFoldRegion(document.getLineStartOffset(1), document.getLineEndOffset(1), "...");
      assertNotNull(foldRegion);
      foldRegion.setExpanded(false);
    }
  });
  executeAction();
  checkResult("normal <selection><caret>line</selection>\n" +
              "collapsed <selection><caret>line</selection>");
  FoldRegion[] foldRegions = foldingModel.getAllFoldRegions();
  assertEquals(1, foldRegions.length);
  assertTrue(foldRegions[0].isExpanded());
}
项目:intellij-ce-playground    文件:NextPrevWordTest.java   
public void testPrevNextWordWithFolding() {
  myFixture.configureByText("a.txt", "<caret>brown fox");
  EditorTestUtil.addFoldRegion(myFixture.getEditor(), 4, 7, "...", true);
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_NEXT_WORD);
  myFixture.checkResult("brow<caret>n fox");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_NEXT_WORD);
  myFixture.checkResult("brown f<caret>ox");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_NEXT_WORD);
  myFixture.checkResult("brown fox<caret>");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_PREVIOUS_WORD);
  myFixture.checkResult("brown f<caret>ox");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_PREVIOUS_WORD);
  myFixture.checkResult("brow<caret>n fox");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_PREVIOUS_WORD);
  myFixture.checkResult("<caret>brown fox");
  FoldRegion[] foldRegions = myFixture.getEditor().getFoldingModel().getAllFoldRegions();
  assertEquals(1, foldRegions.length);
  assertFalse(foldRegions[0].isExpanded());
}
项目:intellij-ce-playground    文件:NextPrevWordTest.java   
public void testPrevNextWordWithSelectionAndFolding() {
  myFixture.configureByText("a.txt", "<caret>brown fox");
  EditorTestUtil.addFoldRegion(myFixture.getEditor(), 4, 7, "...", true);
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_NEXT_WORD_WITH_SELECTION);
  myFixture.checkResult("<selection>brow<caret></selection>n fox");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_NEXT_WORD_WITH_SELECTION);
  myFixture.checkResult("<selection>brown f<caret></selection>ox");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_NEXT_WORD_WITH_SELECTION);
  myFixture.checkResult("<selection>brown fox<caret></selection>");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_PREVIOUS_WORD_WITH_SELECTION);
  myFixture.checkResult("<selection>brown f<caret></selection>ox");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_PREVIOUS_WORD_WITH_SELECTION);
  myFixture.checkResult("<selection>brow<caret></selection>n fox");
  myFixture.performEditorAction(IdeActions.ACTION_EDITOR_PREVIOUS_WORD_WITH_SELECTION);
  myFixture.checkResult("<caret>brown fox");
  FoldRegion[] foldRegions = myFixture.getEditor().getFoldingModel().getAllFoldRegions();
  assertEquals(1, foldRegions.length);
  assertFalse(foldRegions[0].isExpanded());
}
项目:intellij-ce-playground    文件:FoldingAnchorsOverlayStrategyTest.java   
private void verifyAnchors(FoldRegion activeFoldRegion, Object... expectedAnchorParameters) {
  Collection<DisplayedFoldingAnchor> actualAnchors = new FoldingAnchorsOverlayStrategy((EditorImpl)myFixture.getEditor())
    .getAnchorsToDisplay(0, myFixture.getEditor().getDocument().getTextLength(), activeFoldRegion);
  List<DisplayedFoldingAnchor> sortedActualAnchors = new ArrayList<DisplayedFoldingAnchor>(actualAnchors);
  Collections.sort(sortedActualAnchors, new Comparator<DisplayedFoldingAnchor>() {
    @Override
    public int compare(DisplayedFoldingAnchor o1, DisplayedFoldingAnchor o2) {
      return o1.visualLine - o2.visualLine;
    }
  });

  assertEquals("Wrong number of anchors", expectedAnchorParameters.length / 2, sortedActualAnchors.size());
  int i = 0;
  for (DisplayedFoldingAnchor anchor :  sortedActualAnchors) {
    int expectedVisualLine = (Integer) expectedAnchorParameters[i++];
    assertEquals("Folding anchor at wrong line found", expectedVisualLine, anchor.visualLine);
    Type expectedType = (Type)expectedAnchorParameters[i++];
    assertEquals("Folding anchor of wrong type found at line " + expectedVisualLine, expectedType, anchor.type);
  }
}
项目:intellij-ce-playground    文件:AbstractFoldingPolicyTest.java   
protected void doTest(@NotNull String text, @NotNull String ext) {
  myFixture.configureByText("test." + ext, text);

  CodeFoldingManager.getInstance(getProject()).buildInitialFoldings(myFixture.getEditor());
  EditorFoldingInfo info = EditorFoldingInfo.get(myFixture.getEditor());
  FoldRegion[] foldRegions = myFixture.getEditor().getFoldingModel().getAllFoldRegions();
  assertTrue(foldRegions.length > 0);
  for (FoldRegion region : foldRegions) {
    PsiElement element = info.getPsiElement(region);
    if (element == null) {
      continue;
    }
    String signature = FoldingPolicy.getSignature(element);
    assertNotNull(signature);
    assertEquals(element, FoldingPolicy.restoreBySignature(element.getContainingFile(), signature));
  }
}
项目:intellij-ce-playground    文件:FoldingUtilTest.java   
public void testFoldTreeIterator() {
  configureFromFileText(getTestName(false) + ".txt",
                        "abcdefghijklmnopqrstuvwxyz");
  EditorTestUtil.addFoldRegion(myEditor, 0, 10, ".", true);
  EditorTestUtil.addFoldRegion(myEditor, 0, 5, ".", false);
  EditorTestUtil.addFoldRegion(myEditor, 1, 2, ".", true);
  EditorTestUtil.addFoldRegion(myEditor, 7, 10, ".", false);
  EditorTestUtil.addFoldRegion(myEditor, 10, 11, ".", true);

  StringBuilder b = new StringBuilder();
  Iterator<FoldRegion> it = FoldingUtil.createFoldTreeIterator(myEditor);
  while (it.hasNext()) {
    FoldRegion region = it.next();
    if (b.length() > 0) {
      b.append('|');
    }
    b.append(region.getStartOffset()).append(',').append(region.getEndOffset());
  }

  assertEquals("0,10|0,5|1,2|7,10|10,11", b.toString());
}
项目:intellij-ce-playground    文件:CopyPasteFoldingProcessor.java   
@NotNull
@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()])));
}
项目:intellij-ce-playground    文件:UpdateFoldRegionsOperation.java   
@Override
public void run() {
  EditorFoldingInfo info = EditorFoldingInfo.get(myEditor);
  FoldingModelEx foldingModel = (FoldingModelEx)myEditor.getFoldingModel();
  Map<TextRange,Boolean> rangeToExpandStatusMap = newTroveMap();

  // FoldingUpdate caches instances of our object, so they must be immutable.
  FoldingUpdate.FoldingMap elementsToFold = new FoldingUpdate.FoldingMap(myElementsToFoldMap); 

  removeInvalidRegions(info, foldingModel, elementsToFold, rangeToExpandStatusMap);

  Map<FoldRegion, Boolean> shouldExpand = newTroveMap();
  Map<FoldingGroup, Boolean> groupExpand = newTroveMap();
  List<FoldRegion> newRegions = addNewRegions(info, foldingModel, elementsToFold, rangeToExpandStatusMap, shouldExpand, groupExpand);

  applyExpandStatus(newRegions, shouldExpand, groupExpand);
}
项目:intellij-ce-playground    文件:FoldingUtil.java   
@Nullable
public static FoldRegion findFoldRegionStartingAtLine(@NotNull 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;
}
项目:intellij-ce-playground    文件:BaseFoldingHandler.java   
/**
 * 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(@NotNull 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<FoldRegion>();
    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);
}
项目:intellij-ce-playground    文件:CollapseSelectionHandler.java   
public static boolean isEnabled(@NotNull Editor editor) {
  if (editor.getSelectionModel().hasSelection()) {
    int start = editor.getSelectionModel().getSelectionStart();
    int end = editor.getSelectionModel().getSelectionEnd();
    if (start + 1 >= end) {
      return false;
    }
    if (start < end && editor.getDocument().getCharsSequence().charAt(end - 1) == '\n') end--;
    FoldRegion region = FoldingUtil.findFoldRegion(editor, start, end);
    if (region == null) {
      return !((FoldingModelEx) editor.getFoldingModel()).intersectsRegion(start, end); 
    } else {
      return EditorFoldingInfo.get(editor).getPsiElement(region) == null;
    }
  } else {
    return FoldingUtil.getFoldRegionsAtOffset(editor, editor.getCaretModel().getOffset()).length > 0;
  }
}
项目:IntellijMethodHierarchyHighlighter    文件:MethodFolder.java   
private Optional<FoldRegion> getFoldRegionOfMethod(Editor editor, PsiMethod psiMethod) {
    TextRange textRange = psiMethod.getTextRange();
    for (FoldRegion foldRegion : editor.getFoldingModel().getAllFoldRegions()) {
        if (textRange.getEndOffset() == foldRegion.getEndOffset()) {
            return Optional.of(foldRegion);
        }
    }
    return Optional.empty();
}
项目:intellij-ce-playground    文件:JavaMoveLineTest.java   
public void testMoveThroughFolding() throws Exception {
  configureByFile(BASE_PATH + "/" + getTestName(false) + ".java");
  CodeFoldingManager.getInstance(ourProject).buildInitialFoldings(myEditor);
  FoldRegion lambdaStart = myEditor.getFoldingModel().getFoldRegion(140, 227);
  assertNotNull(lambdaStart);
  assertFalse(lambdaStart.isExpanded());
  FoldRegion lambdaEnd = myEditor.getFoldingModel().getFoldRegion(248, 272);
  assertNotNull(lambdaEnd);
  assertFalse(lambdaEnd.isExpanded());

  executeAction(IdeActions.ACTION_MOVE_LINE_UP_ACTION);

  checkResultByFile(BASE_PATH + "/" + getTestName(false) + "-after.java");
}
项目:intellij-ce-playground    文件:FoldingModelSupport.java   
@Nullable
private static FoldRegion addFolding(@NotNull EditorEx editor, int start, int end, boolean expanded) {
  DocumentEx document = editor.getDocument();
  final int startOffset = document.getLineStartOffset(start);
  final int endOffset = document.getLineEndOffset(end - 1);

  FoldRegion value = editor.getFoldingModel().addFoldRegion(startOffset, endOffset, PLACEHOLDER);
  if (value != null) value.setExpanded(expanded);
  return value;
}
项目:intellij-ce-playground    文件:FoldingModelSupport.java   
private void destroyFoldings(final int index) {
  final FoldingModelEx model = myEditors[index].getFoldingModel();
  model.runBatchFoldingOperation(new Runnable() {
    @Override
    public void run() {
      for (FoldedBlock folding : getFoldedBlocks()) {
        FoldRegion region = folding.getRegion(index);
        if (region != null) model.removeFoldRegion(region);
      }
    }
  });
}
项目:intellij-ce-playground    文件:FoldingModelSupport.java   
@Override
public void onFoldProcessingEnd() {
  if (myModifiedRegions.isEmpty()) return;
  myDuringSynchronize = true;
  try {
    for (int i = 0; i < myCount; i++) {
      if (i == myIndex) continue;
      final int pairedIndex = i;
      myEditors[pairedIndex].getFoldingModel().runBatchFoldingOperation(new Runnable() {
        @Override
        public void run() {
          for (FoldedBlock folding : getFoldedBlocks()) {
            FoldRegion region = folding.getRegion(myIndex);
            if (region == null || !region.isValid()) continue;
            if (myModifiedRegions.contains(region)) {
              FoldRegion pairedRegion = folding.getRegion(pairedIndex);
              if (pairedRegion == null || !pairedRegion.isValid()) continue;
              pairedRegion.setExpanded(region.isExpanded());
            }
          }
        }
      });
    }

    myModifiedRegions.clear();
  }
  finally {
    myDuringSynchronize = false;
  }
}
项目:intellij-ce-playground    文件:FoldingModelSupport.java   
@Override
public void process(@NotNull 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;
    }
  }
}
项目:intellij-ce-playground    文件:FoldingTransformation.java   
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();
}
项目:intellij-ce-playground    文件:FoldingTransformation.java   
public int transform(int line) {
  FoldRegion foldRegion = findFoldRegion(line);
  int yOffset = 0;
  if (foldRegion != null) {
    int startLine = getStartLine(foldRegion);
    yOffset = (int)((double)(line - startLine) / getLineLength(foldRegion) * myEditor.getLineHeight());
    line = startLine;
  }
  yOffset += myEditor.logicalPositionToXY(new LogicalPosition(line, 0)).y;

  final JComponent header = myEditor.getHeaderComponent();
  int headerOffset = header == null ? 0 : header.getHeight();

  return yOffset - myEditor.getScrollingModel().getVerticalScrollOffset() + headerOffset;
}
项目:intellij-ce-playground    文件:FoldingTransformation.java   
private FoldRegion findFoldRegion(int line) {
  int index = Arrays.binarySearch(myFoldBeginings, line);
  FoldRegion region;
  if (index >= 0) region = myCollapsed.get(index);
  else {
    index = -index - 1;
    if (index == 0) return null;
    region = myCollapsed.get(index - 1);
  }
  if (getEndLine(region) < line) return null;
  return region;
}
项目:intellij-ce-playground    文件:EditorFoldingInfo.java   
@Nullable
public TextRange getPsiElementRange(@NotNull FoldRegion region) {
  PsiElement element = getPsiElement(region);
  if (element == null) return null;
  PsiFile containingFile = element.getContainingFile();
  InjectedLanguageManager injectedManager = InjectedLanguageManager.getInstance(containingFile.getProject());
  boolean isInjected = injectedManager.isInjectedFragment(containingFile);
  TextRange range = element.getTextRange();
  if (isInjected) {
    range = injectedManager.injectedToHost(element, range);
  }
  return range;
}
项目:intellij-ce-playground    文件:EditorView.java   
LineLayout getFoldRegionLayout(FoldRegion foldRegion) {
  LineLayout layout = foldRegion.getUserData(FOLD_REGION_TEXT_LAYOUT);
  if (layout == null) {
    TextAttributes placeholderAttributes = myEditor.getFoldingModel().getPlaceholderAttributes();
    layout = new LineLayout(this, foldRegion.getPlaceholderText(), 
                            placeholderAttributes == null ? Font.PLAIN : placeholderAttributes.getFontType());
    foldRegion.putUserData(FOLD_REGION_TEXT_LAYOUT, layout);
  }
  return layout;
}
项目:intellij-ce-playground    文件:EditorSizeManager.java   
@Override
public void onFoldRegionStateChange(@NotNull FoldRegion region) {
  if (region.isValid()) {
    foldingChangeStartOffset = Math.min(foldingChangeStartOffset, region.getStartOffset());
    foldingChangeEndOffset = Math.max(foldingChangeEndOffset, region.getEndOffset());
  }
}
项目:intellij-ce-playground    文件:VisualLineFragmentsIterator.java   
private int[] getVisualColumnForXInsideFoldRegion(FoldRegion foldRegion, float x) {
  LineLayout layout = myView.getFoldRegionLayout(foldRegion);
  for (LineLayout.VisualFragment fragment : layout.getFragmentsInVisualOrder(0)) {
    if (x <= fragment.getEndX()) {
      return fragment.xToVisualColumn(x);
    }
  }
  return new int[] {getFoldRegionWidthInColumns(foldRegion), 1};
}
项目:intellij-ce-playground    文件:VisualLineFragmentsIterator.java   
private float getXForVisualColumnInsideFoldRegion(FoldRegion foldRegion, int column) { 
  LineLayout layout = myView.getFoldRegionLayout(foldRegion);
  for (LineLayout.VisualFragment fragment : layout.getFragmentsInVisualOrder(0)) {
    if (column <= fragment.getEndVisualColumn()) {
      return fragment.visualColumnToX(column);
    }
  }
  return getFoldRegionWidthInPixels(foldRegion);
}
项目:intellij-ce-playground    文件:FoldingModelWindow.java   
@Nullable
@Override
public FoldRegion getFoldRegion(int startOffset, int endOffset) {
  TextRange range = new TextRange(startOffset, endOffset);
  TextRange hostRange = myDocumentWindow.injectedToHost(range);
  FoldRegion hostRegion = myDelegate.getFoldRegion(hostRange.getStartOffset(), hostRange.getEndOffset());
  if (hostRegion == null) {
    return null;
  }
  FoldingRegionWindow window = hostRegion.getUserData(FOLD_REGION_WINDOW);
  return window != null && window.getEditor() == myEditorWindow ? window : null;
}
项目:intellij-ce-playground    文件:FoldingModelWindow.java   
@Override
public FoldRegion addFoldRegion(int startOffset, int endOffset, @NotNull String placeholderText) {
  FoldRegion region = createFoldRegion(startOffset, endOffset, placeholderText, null, false);
  if (region == null) return null;
  if (!addFoldRegion(region)) {
    region.dispose();
    return null;
  }

  return region;
}
项目:intellij-ce-playground    文件:FoldRegionImpl.java   
private static void doSetExpanded(boolean expanded, FoldingModelImpl foldingModel, FoldRegion region) {
  if (expanded) {
    foldingModel.expandFoldRegion(region);
  }
  else{
    foldingModel.collapseFoldRegion(region);
  }
}