public static void insertString(Document document, String insertLine, int offset, Project project) { ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) { @Override public void run() { CommandProcessor.getInstance().executeCommand(project, new Runnable() { @Override public void run() { try { document.insertString(offset, insertLine); } catch (Exception ignored) { } } }, insertLine, document); } }); }
public static void replaceString(Project project, Document document, String newString, int offset,int length) { ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) { @Override public void run() { CommandProcessor.getInstance().executeCommand(project, new Runnable() { @Override public void run() { try { document.replaceString(offset, offset + length, newString); } catch (Exception ignored) { } } }, "", document); } }); }
@Override public void execute(final Editor editor, final DataContext dataContext) { SelectionModel selectionModel = editor.getSelectionModel(); if (!selectionModel.hasSelection()) { return; } final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); if (start >= end) { return; } KillRingUtil.copyToKillRing(editor, start, end, false); if (myRemove) { ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(editor.getDocument(),editor.getProject()) { @Override public void run() { editor.getDocument().deleteString(start, end); } }); } }
private void ensureVFSBaseDir() { myBaseDir.mkdirs(); ApplicationManager.getApplication().runWriteAction(new DocumentRunnable.IgnoreDocumentRunnable(){ @Override public void run() { VirtualFile dir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(myBaseDir); myVFSBaseDir = dir; if (dir != null) { dir.getChildren(); ((NewVirtualFile)dir).markDirtyRecursively(); dir.refresh(false, true); } } }); }
@Nullable static VirtualFile save(@NotNull IFile file, Parent element, Object requestor) throws StateStorageException { try { VirtualFile vFile = LocalFileSystem.getInstance().findFileByIoFile(file); Pair<String, String> pair = loadFile(vFile); String text = JDOMUtil.writeParent(element, pair.second); if (file.exists()) { if (text.equals(pair.first)) { return null; } } else { file.createParentDirs(); } // mark this action as modifying the file which daemon analyzer should ignore AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(DocumentRunnable.IgnoreDocumentRunnable.class); try { VirtualFile virtualFile = getOrCreateVirtualFile(requestor, file); byte[] bytes = text.getBytes(CharsetToolkit.UTF8); virtualFile.setBinaryContent(bytes, -1, -1, requestor); return virtualFile; } finally { token.finish(); } } catch (IOException e) { throw new StateStorageException(e); } }
private void strip(final Document document) { if (!document.isWritable()) return; FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance(); VirtualFile file = fileDocumentManager.getFile(document); if (file == null || !file.isValid()) return; final EditorSettingsExternalizable settings = EditorSettingsExternalizable.getInstance(); if (settings == null) return; String stripTrailingSpaces = settings.getStripTrailingSpaces(); final boolean doStrip = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_NONE); final boolean ensureEOL = settings.isEnsureNewLineAtEOF(); if (doStrip) { final boolean inChangedLinesOnly = !stripTrailingSpaces.equals(EditorSettingsExternalizable.STRIP_TRAILING_SPACES_WHOLE); boolean success = stripIfNotCurrentLine(document, inChangedLinesOnly); if (!success) { myDocumentsToStripLater.add(document); } } final int lines = document.getLineCount(); if (ensureEOL && lines > 0) { final int start = document.getLineStartOffset(lines - 1); final int end = document.getLineEndOffset(lines - 1); if (start != end) { final CharSequence content = document.getCharsSequence(); ApplicationManager.getApplication().runWriteAction(new DocumentRunnable(document, null) { @Override public void run() { CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() { public void run() { if (CharArrayUtil.containsOnlyWhiteSpaces(content.subSequence(start, end)) && doStrip) { document.deleteString(start, end); } else { document.insertString(end, "\n"); } } }); } }); } } }