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);
}
});
}
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);
};
}
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;
}
});
}
Aggregations