use of com.intellij.openapi.editor.ex.FoldingModelEx in project intellij-community by JetBrains.
the class UpdateFoldRegionsOperation method run.
@Override
public void run() {
EditorFoldingInfo info = EditorFoldingInfo.get(myEditor);
FoldingModelEx foldingModel = (FoldingModelEx) myEditor.getFoldingModel();
Map<TextRange, Boolean> rangeToExpandStatusMap = new THashMap<>();
// 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 = new THashMap<>();
Map<FoldingGroup, Boolean> groupExpand = new THashMap<>();
List<FoldRegion> newRegions = addNewRegions(info, foldingModel, elementsToFold, rangeToExpandStatusMap, shouldExpand, groupExpand);
applyExpandStatus(newRegions, shouldExpand, groupExpand);
foldingModel.clearDocumentRangesModificationStatus();
}
use of com.intellij.openapi.editor.ex.FoldingModelEx in project intellij-community by JetBrains.
the class CodeFoldingManagerImpl method buildInitialFoldings.
@Nullable
@Override
public CodeFoldingState buildInitialFoldings(@NotNull final Document document) {
if (myProject.isDisposed()) {
return null;
}
ApplicationManager.getApplication().assertReadAccessAllowed();
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(myProject);
if (psiDocumentManager.isUncommited(document)) {
// skip building foldings for uncommitted document, CodeFoldingPass invoked by daemon will do it later
return null;
}
//Do not save/restore folding for code fragments
final PsiFile file = psiDocumentManager.getPsiFile(document);
if (file == null || !file.isValid() || !file.getViewProvider().isPhysical() && !ApplicationManager.getApplication().isUnitTestMode()) {
return null;
}
final FoldingUpdate.FoldingMap foldingMap = FoldingUpdate.getFoldingsFor(file, document, true);
return editor -> {
ApplicationManagerEx.getApplicationEx().assertIsDispatchThread();
if (myProject.isDisposed() || editor.isDisposed())
return;
final FoldingModelEx foldingModel = (FoldingModelEx) editor.getFoldingModel();
if (!foldingModel.isFoldingEnabled())
return;
if (isFoldingsInitializedInEditor(editor))
return;
if (DumbService.isDumb(myProject) && !FoldingUpdate.supportsDumbModeFolding(editor))
return;
foldingModel.runBatchFoldingOperationDoNotCollapseCaret(new UpdateFoldRegionsOperation(myProject, editor, file, foldingMap, UpdateFoldRegionsOperation.ApplyDefaultStateMode.YES, false, false));
initFolding(editor);
};
}
use of com.intellij.openapi.editor.ex.FoldingModelEx in project intellij-community by JetBrains.
the class CollapseSelectionHandler method invoke.
@Override
public void invoke(@NotNull Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
editor.getFoldingModel().runBatchFoldingOperation(() -> {
final EditorFoldingInfo info = EditorFoldingInfo.get(editor);
FoldingModelEx foldingModel = (FoldingModelEx) editor.getFoldingModel();
if (editor.getSelectionModel().hasSelection()) {
int start = editor.getSelectionModel().getSelectionStart();
int end = editor.getSelectionModel().getSelectionEnd();
if (start + 1 >= end) {
return;
}
Document doc = editor.getDocument();
if (start < end && doc.getCharsSequence().charAt(end - 1) == '\n')
end--;
FoldRegion region;
if ((region = FoldingUtil.findFoldRegion(editor, start, end)) != null) {
if (info.getPsiElement(region) == null) {
editor.getFoldingModel().removeFoldRegion(region);
info.removeRegion(region);
}
} else if (!foldingModel.intersectsRegion(start, end)) {
region = foldingModel.addFoldRegion(start, end, ourPlaceHolderText);
LOG.assertTrue(region != null, "Fold region is not created. Folding model: " + foldingModel);
region.setExpanded(false);
int offset = Math.min(start + ourPlaceHolderText.length(), doc.getTextLength());
editor.getCaretModel().moveToOffset(offset);
}
} else {
FoldRegion[] regions = FoldingUtil.getFoldRegionsAtOffset(editor, editor.getCaretModel().getOffset());
if (regions.length > 0) {
FoldRegion region = regions[0];
if (info.getPsiElement(region) == null) {
editor.getFoldingModel().removeFoldRegion(region);
info.removeRegion(region);
} else {
region.setExpanded(!region.isExpanded());
}
}
}
});
}
use of com.intellij.openapi.editor.ex.FoldingModelEx in project intellij-community by JetBrains.
the class JavaFoldingGotoTest method testIDEA127145.
public void testIDEA127145() {
PsiFile file = myFixture.addFileToProject("Program.java", "import java.io.InputStream;\n" + "import java.util.HashMap;\n" + "import java.util.Map;\n" + "\n" + "class Program {\n" + " private static InputStream getFile(String name, Map<String, Object> args) {\n" + " return Program.class.getResourceAsStream(name);\n" + " }\n" + "\n" + " public static void main(String[] args) {\n" + " // Ctrl + B or Ctrl + Left Mouse Button work correctly for following string:\n" + " final String name = \"file.sql\";\n" + " // But it jumps only to folder in following case:\n" + " final InputStream inputStream = getFile(\"dir/fil<caret>e.sql\", new HashMap<String, Object>());\n" + " }\n" + "}");
PsiFile fileSql = myFixture.addFileToProject("dir/file.sql", "select 1;");
myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
Editor editor = myFixture.getEditor();
CodeFoldingManager.getInstance(getProject()).buildInitialFoldings(editor);
FoldingModelEx foldingModel = (FoldingModelEx) editor.getFoldingModel();
foldingModel.rebuild();
myFixture.doHighlighting();
PsiElement element = GotoDeclarationAction.findTargetElement(getProject(), editor, editor.getCaretModel().getOffset());
assertTrue("Should navigate to: file.sql instead of " + element, element != null && element.equals(fileSql));
}
use of com.intellij.openapi.editor.ex.FoldingModelEx in project intellij-community by JetBrains.
the class EditorImplTest method testChangingHighlightersAfterClearingFoldingsDuringFoldingBatchUpdate.
public void testChangingHighlightersAfterClearingFoldingsDuringFoldingBatchUpdate() throws Exception {
initText("abc\n\ndef");
addCollapsedFoldRegion(2, 6, "...");
runFoldingOperation(() -> {
((FoldingModelEx) myEditor.getFoldingModel()).clearFoldRegions();
myEditor.getMarkupModel().addRangeHighlighter(7, 8, 0, new TextAttributes(null, null, null, null, Font.BOLD), HighlighterTargetArea.EXACT_RANGE);
});
RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
assertEquals(1, highlighters.length);
assertEquals(7, highlighters[0].getStartOffset());
assertEquals(8, highlighters[0].getEndOffset());
}
Aggregations