Search in sources :

Example 6 with FoldingModel

use of com.intellij.openapi.editor.FoldingModel in project intellij-community by JetBrains.

the class ExternalProjectPathLookupElement method handleInsert.

@Override
public void handleInsert(InsertionContext context) {
    Editor editor = context.getEditor();
    final FoldingModel foldingModel = editor.getFoldingModel();
    foldingModel.runBatchFoldingOperation(() -> {
        FoldRegion[] regions = foldingModel.getAllFoldRegions();
        for (FoldRegion region : regions) {
            foldingModel.removeFoldRegion(region);
        }
    });
    final Document document = editor.getDocument();
    final int startOffset = context.getStartOffset();
    document.replaceString(startOffset, document.getTextLength(), myProjectPath);
    final Project project = context.getProject();
    PsiDocumentManager.getInstance(project).commitDocument(document);
    foldingModel.runBatchFoldingOperationDoNotCollapseCaret(() -> {
        FoldRegion region = foldingModel.addFoldRegion(startOffset, startOffset + myProjectPath.length(), myProjectName);
        if (region != null) {
            region.setExpanded(false);
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) FoldingModel(com.intellij.openapi.editor.FoldingModel) FoldRegion(com.intellij.openapi.editor.FoldRegion) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Example 7 with FoldingModel

use of com.intellij.openapi.editor.FoldingModel in project intellij-community by JetBrains.

the class FoldingUpdate method updateInjectedFoldRegions.

@Nullable
public static Runnable updateInjectedFoldRegions(@NotNull final Editor editor, @NotNull final PsiFile file, final boolean applyDefaultState) {
    if (file instanceof PsiCompiledElement)
        return null;
    ApplicationManager.getApplication().assertReadAccessAllowed();
    final Project project = file.getProject();
    Document document = editor.getDocument();
    LOG.assertTrue(!PsiDocumentManager.getInstance(project).isUncommited(document));
    final FoldingModel foldingModel = editor.getFoldingModel();
    final long timeStamp = document.getModificationStamp();
    Object lastTimeStamp = editor.getUserData(LAST_UPDATE_INJECTED_STAMP_KEY);
    if (lastTimeStamp instanceof Long && ((Long) lastTimeStamp).longValue() == timeStamp)
        return null;
    List<DocumentWindow> injectedDocuments = InjectedLanguageUtil.getCachedInjectedDocuments(file);
    if (injectedDocuments.isEmpty())
        return null;
    final List<EditorWindow> injectedEditors = new ArrayList<>();
    final List<PsiFile> injectedFiles = new ArrayList<>();
    final List<FoldingMap> maps = new ArrayList<>();
    for (final DocumentWindow injectedDocument : injectedDocuments) {
        if (!injectedDocument.isValid()) {
            continue;
        }
        InjectedLanguageUtil.enumerate(injectedDocument, file, (injectedFile, places) -> {
            if (!injectedFile.isValid())
                return;
            Editor injectedEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(editor, injectedFile);
            if (!(injectedEditor instanceof EditorWindow))
                return;
            injectedEditors.add((EditorWindow) injectedEditor);
            injectedFiles.add(injectedFile);
            final FoldingMap map = new FoldingMap();
            maps.add(map);
            getFoldingsFor(injectedFile, injectedEditor.getDocument(), map, false);
        });
    }
    return () -> {
        final ArrayList<Runnable> updateOperations = new ArrayList<>(injectedEditors.size());
        for (int i = 0; i < injectedEditors.size(); i++) {
            EditorWindow injectedEditor = injectedEditors.get(i);
            PsiFile injectedFile = injectedFiles.get(i);
            if (!injectedEditor.getDocument().isValid())
                continue;
            FoldingMap map = maps.get(i);
            updateOperations.add(new UpdateFoldRegionsOperation(project, injectedEditor, injectedFile, map, applyDefaultState ? EXCEPT_CARET_REGION : NO, !applyDefaultState, true));
        }
        foldingModel.runBatchFoldingOperation(() -> {
            for (Runnable operation : updateOperations) {
                operation.run();
            }
        });
        editor.putUserData(LAST_UPDATE_INJECTED_STAMP_KEY, timeStamp);
    };
}
Also used : Document(com.intellij.openapi.editor.Document) EditorWindow(com.intellij.injected.editor.EditorWindow) DocumentWindow(com.intellij.injected.editor.DocumentWindow) Project(com.intellij.openapi.project.Project) FoldingModel(com.intellij.openapi.editor.FoldingModel) Editor(com.intellij.openapi.editor.Editor) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with FoldingModel

use of com.intellij.openapi.editor.FoldingModel in project intellij-community by JetBrains.

the class PyConsoleStartFolding method addFolding.

private void addFolding(DocumentEvent event) {
    Document document = myConsoleView.getEditor().getDocument();
    if (doNotAddFoldingAgain || document.getTextLength() == 0) {
        return;
    }
    if (myNumberOfCommandExecuted >= myNumberOfCommandToStop) {
        document.removeDocumentListener(this);
        return;
    }
    FoldingModel foldingModel = myConsoleView.getEditor().getFoldingModel();
    foldingModel.runBatchFoldingOperation(() -> {
        int start = myStartLineOffset;
        int finish = document.getTextLength() - 1;
        String placeholderText = DEFAULT_FOLDING_MESSAGE;
        int firstLine = document.getLineNumber(myStartLineOffset);
        for (int line = firstLine; line < document.getLineCount(); line++) {
            String lineText = document.getText(DocumentUtil.getLineTextRange(document, line));
            if (lineText.startsWith("Python")) {
                if (start == myStartLineOffset) {
                    start = document.getLineStartOffset(line);
                }
                placeholderText = lineText;
                break;
            }
            if (lineText.startsWith("PyDev console")) {
                start = document.getLineStartOffset(line);
            }
        }
        String newFragment = event.getNewFragment().toString();
        if (newFragment.startsWith("In[") || newFragment.startsWith(PyConsoleUtil.ORDINARY_PROMPT)) {
            finish = event.getOffset() - 1;
            doNotAddFoldingAgain = true;
        }
        if (myStartFoldRegion != null) {
            foldingModel.removeFoldRegion(myStartFoldRegion);
        }
        FoldRegion foldRegion = foldingModel.addFoldRegion(start, finish, placeholderText);
        if (foldRegion != null) {
            foldRegion.setExpanded(false);
            myStartFoldRegion = foldRegion;
        }
    });
}
Also used : FoldingModel(com.intellij.openapi.editor.FoldingModel) FoldRegion(com.intellij.openapi.editor.FoldRegion) Document(com.intellij.openapi.editor.Document)

Aggregations

FoldingModel (com.intellij.openapi.editor.FoldingModel)8 FoldRegion (com.intellij.openapi.editor.FoldRegion)6 Document (com.intellij.openapi.editor.Document)3 Editor (com.intellij.openapi.editor.Editor)3 Project (com.intellij.openapi.project.Project)2 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 EditorWindow (com.intellij.injected.editor.EditorWindow)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 ArrangementEngine (com.intellij.psi.codeStyle.arrangement.engine.ArrangementEngine)1 ArrangementGroupingRule (com.intellij.psi.codeStyle.arrangement.group.ArrangementGroupingRule)1 ArrangementSectionRule (com.intellij.psi.codeStyle.arrangement.match.ArrangementSectionRule)1 Nullable (org.jetbrains.annotations.Nullable)1