From 0e534ab16d036585af1fa0a609b5f09b60e37477 Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 2 Dec 2016 08:26:35 +0100 Subject: [PATCH] some editor actions: clear read-only status outside write action --- .../actions/MultiCaretCodeInsightAction.java | 3 + .../editorActions/EmacsStyleIndentAction.java | 32 ++++----- .../codeInsight/editorActions/EndHandler.java | 67 +++++++++---------- .../editorActions/FixDocCommentAction.java | 3 +- .../CommentByBlockCommentHandler.java | 4 -- .../CommentByLineCommentHandler.java | 5 -- .../codeInsight/unwrap/UnwrapHandler.java | 12 ++-- .../EditorWriteActionHandler.java | 3 +- 8 files changed, 56 insertions(+), 73 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/actions/MultiCaretCodeInsightAction.java b/platform/lang-impl/src/com/intellij/codeInsight/actions/MultiCaretCodeInsightAction.java index 855c79a1ffde..2648af1d52cd 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/actions/MultiCaretCodeInsightAction.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/actions/MultiCaretCodeInsightAction.java @@ -15,6 +15,7 @@ */ package com.intellij.codeInsight.actions; +import com.intellij.codeInsight.FileModificationService; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.CommonDataKeys; @@ -55,6 +56,8 @@ public abstract class MultiCaretCodeInsightAction extends AnAction { if (hostEditor == null) { return; } + PsiFile hostFile = PsiDocumentManager.getInstance(project).getPsiFile(hostEditor.getDocument()); + if (hostFile != null && !FileModificationService.getInstance().prepareFileForWrite(hostFile)) return; actionPerformedImpl(project, hostEditor); } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EmacsStyleIndentAction.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EmacsStyleIndentAction.java index 8b277d5d0df5..357cd9a0ae4c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EmacsStyleIndentAction.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EmacsStyleIndentAction.java @@ -22,7 +22,7 @@ import com.intellij.codeInsight.actions.BaseCodeInsightAction; import com.intellij.codeInsight.editorActions.emacs.EmacsProcessingHandler; import com.intellij.codeInsight.editorActions.emacs.LanguageEmacsExtension; import com.intellij.lang.LanguageFormatting; -import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.ScrollType; @@ -33,13 +33,10 @@ import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; -import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; public class EmacsStyleIndentAction extends BaseCodeInsightAction implements DumbAware { - private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.generation.actions.EmacsStyleIndentAction"); - @NotNull @Override protected CodeInsightActionHandler getHandler() { @@ -64,18 +61,18 @@ public class EmacsStyleIndentAction extends BaseCodeInsightAction implements Dum return; } - EmacsProcessingHandler emacsProcessingHandler = LanguageEmacsExtension.INSTANCE.forLanguage(file.getLanguage()); - if (emacsProcessingHandler != null) { - EmacsProcessingHandler.Result result = emacsProcessingHandler.changeIndent(project, editor, file); - if (result == EmacsProcessingHandler.Result.STOP) { - return; + WriteAction.run(() -> { + EmacsProcessingHandler emacsProcessingHandler = LanguageEmacsExtension.INSTANCE.forLanguage(file.getLanguage()); + if (emacsProcessingHandler != null) { + EmacsProcessingHandler.Result result = emacsProcessingHandler.changeIndent(project, editor, file); + if (result == EmacsProcessingHandler.Result.STOP) { + return; + } } - } - final Document document = editor.getDocument(); - int startLine = document.getLineNumber(editor.getSelectionModel().getSelectionStart()); - int endLine = document.getLineNumber(editor.getSelectionModel().getSelectionEnd()); - try{ + final Document document = editor.getDocument(); + int startLine = document.getLineNumber(editor.getSelectionModel().getSelectionStart()); + int endLine = document.getLineNumber(editor.getSelectionModel().getSelectionEnd()); for (int line = startLine; line <= endLine; line++) { final int lineStart = document.getLineStartOffset(line); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); @@ -86,15 +83,12 @@ public class EmacsStyleIndentAction extends BaseCodeInsightAction implements Dum editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); } } - } - catch(IncorrectOperationException e){ - LOG.error(e); - } + }); } @Override public boolean startInWriteAction() { - return true; + return false; } } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EndHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EndHandler.java index dce7674debcf..1a943b9954b4 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EndHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/EndHandler.java @@ -21,6 +21,7 @@ import com.intellij.ide.DataManager; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.editor.*; import com.intellij.openapi.editor.actionSystem.EditorActionHandler; import com.intellij.openapi.fileEditor.FileDocumentManager; @@ -30,8 +31,6 @@ import com.intellij.psi.PsiFile; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.util.text.CharArrayUtil; -import java.util.concurrent.atomic.AtomicBoolean; - public class EndHandler extends EditorActionHandler { private final EditorActionHandler myOriginalHandler; @@ -87,46 +86,32 @@ public class EndHandler extends EditorActionHandler { if (isEmptyLine) { // There is a possible case that indent string is not calculated for particular document (that is true at least for plain text - // documents). Hence, we check that and don't finish processing in case we have such a situation. AtomicBoolean is used - // here just as a boolean value holder due to requirement to declare variable used from inner class as final. - final AtomicBoolean stopProcessing = new AtomicBoolean(true); + // documents). Hence, we check that and don't finish processing in case we have such a situation. + boolean stopProcessing = true; PsiDocumentManager.getInstance(project).commitAllDocuments(); - ApplicationManager.getApplication().runWriteAction(new Runnable() { - @Override - public void run() { - CodeStyleManager styleManager = CodeStyleManager.getInstance(project); - final String lineIndent = styleManager.getLineIndent(file, caretOffset); - if (lineIndent != null) { - int col = calcColumnNumber(lineIndent, editor.getSettings().getTabSize(project)); - int line = caretModel.getVisualPosition().line; - caretModel.moveToVisualPosition(new VisualPosition(line, col)); + CodeStyleManager styleManager = CodeStyleManager.getInstance(project); + final String lineIndent = styleManager.getLineIndent(file, caretOffset); + if (lineIndent != null) { + int col = calcColumnNumber(lineIndent, editor.getSettings().getTabSize(project)); + int line = caretModel.getVisualPosition().line; + caretModel.moveToVisualPosition(new VisualPosition(line, col)); - if (caretModel.getLogicalPosition().column != col){ - if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) { - return; - } - editor.getSelectionModel().removeSelection(); - document.replaceString(offset1 + 1, offset2, lineIndent); - } + if (caretModel.getLogicalPosition().column != col){ + if (!ApplicationManager.getApplication().isWriteAccessAllowed() && + !FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) { + return; } - else { - stopProcessing.set(false); - } - - editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); editor.getSelectionModel().removeSelection(); + WriteAction.run(() -> document.replaceString(offset1 + 1, offset2, lineIndent)); } + } + else { + stopProcessing = false; + } - private int calcColumnNumber(final String lineIndent, final int tabSize) { - int result = 0; - for (char c : lineIndent.toCharArray()) { - if (c == ' ') result++; - if (c == '\t') result += tabSize; - } - return result; - } - }); - if (stopProcessing.get()) { + editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); + editor.getSelectionModel().removeSelection(); + if (stopProcessing) { return; } } @@ -137,4 +122,14 @@ public class EndHandler extends EditorActionHandler { myOriginalHandler.execute(editor, caret, dataContext); } } + + private static int calcColumnNumber(final String lineIndent, final int tabSize) { + int result = 0; + for (char c : lineIndent.toCharArray()) { + if (c == ' ') result++; + if (c == '\t') result += tabSize; + } + return result; + } + } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/FixDocCommentAction.java b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/FixDocCommentAction.java index ecafc1f01450..da232961e9f2 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/editorActions/FixDocCommentAction.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/editorActions/FixDocCommentAction.java @@ -77,7 +77,7 @@ public class FixDocCommentAction extends EditorAction { private static void process(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Project project, int offset) { PsiElement elementAtOffset = file.findElementAt(offset); - if (elementAtOffset == null) { + if (elementAtOffset == null || !FileModificationService.getInstance().preparePsiElementForWrite(elementAtOffset)) { return; } generateOrFixComment(elementAtOffset, project, editor); @@ -152,7 +152,6 @@ public class FixDocCommentAction extends EditorAction { @NotNull CodeDocumentationAwareCommenter commenter, @NotNull Project project) { - if (!FileModificationService.getInstance().preparePsiElementForWrite(anchor)) return; Document document = editor.getDocument(); int commentStartOffset = anchor.getTextRange().getStartOffset(); int lineStartOffset = document.getLineStartOffset(document.getLineNumber(commentStartOffset)); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByBlockCommentHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByBlockCommentHandler.java index 0edde0b040eb..27455d00ee6c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByBlockCommentHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByBlockCommentHandler.java @@ -28,7 +28,6 @@ import com.intellij.lang.LanguageCommenters; import com.intellij.lexer.Lexer; import com.intellij.openapi.editor.*; import com.intellij.openapi.editor.ex.util.EditorUtil; -import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.impl.AbstractFileType; import com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType; @@ -71,9 +70,6 @@ public class CommentByBlockCommentHandler extends MultiCaretCodeInsightActionHan myDocument = editor.getDocument(); - if (!FileDocumentManager.getInstance().requestWriting(myDocument, project)) { - return; - } FeatureUsageTracker.getInstance().triggerFeatureUsed("codeassists.comment.block"); final Commenter commenter = findCommenter(myFile, myEditor, caret); if (commenter == null) return; diff --git a/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByLineCommentHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByLineCommentHandler.java index e317bd36dfa6..3df1a5f930b9 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByLineCommentHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/generation/CommentByLineCommentHandler.java @@ -31,7 +31,6 @@ import com.intellij.openapi.actionSystem.IdeActions; import com.intellij.openapi.actionSystem.ex.ActionManagerEx; import com.intellij.openapi.editor.*; import com.intellij.openapi.editor.ex.util.EditorUtil; -import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.fileTypes.impl.AbstractFileType; import com.intellij.openapi.fileTypes.impl.CustomSyntaxTableFileType; @@ -82,10 +81,6 @@ public class CommentByLineCommentHandler extends MultiCaretCodeInsightActionHand } Document document = editor.getDocument(); - if (!FileDocumentManager.getInstance().requestWriting(document, project)) { - return; - } - boolean hasSelection = caret.hasSelection(); int startOffset = caret.getSelectionStart(); int endOffset = caret.getSelectionEnd(); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/unwrap/UnwrapHandler.java b/platform/lang-impl/src/com/intellij/codeInsight/unwrap/UnwrapHandler.java index 560c7d638728..ccfe1e5b1251 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/unwrap/UnwrapHandler.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/unwrap/UnwrapHandler.java @@ -54,7 +54,7 @@ public class UnwrapHandler implements CodeInsightActionHandler { @Override public boolean startInWriteAction() { - return true; + return false; } @Override @@ -64,7 +64,7 @@ public class UnwrapHandler implements CodeInsightActionHandler { selectOption(options, editor, file); } - private List collectOptions(Project project, Editor editor, PsiFile file) { + private static List collectOptions(Project project, Editor editor, PsiFile file) { List result = new ArrayList<>(); UnwrapDescriptor d = getUnwrapDescription(file); @@ -80,7 +80,7 @@ public class UnwrapHandler implements CodeInsightActionHandler { return LanguageUnwrappers.INSTANCE.forLanguage(file.getLanguage()); } - private AnAction createUnwrapAction(Unwrapper u, PsiElement el, Editor ed, Project p) { + private static AnAction createUnwrapAction(Unwrapper u, PsiElement el, Editor ed, Project p) { return new MyUnwrapAction(p, ed, u, el); } @@ -97,10 +97,10 @@ public class UnwrapHandler implements CodeInsightActionHandler { showPopup(options, editor); } - private void showPopup(final List options, Editor editor) { + private static void showPopup(final List options, Editor editor) { final ScopeHighlighter highlighter = new ScopeHighlighter(editor); - DefaultListModel m = new DefaultListModel(); + DefaultListModel m = new DefaultListModel<>(); for (AnAction a : options) { m.addElement(((MyUnwrapAction)a).getName()); } @@ -192,7 +192,7 @@ public class UnwrapHandler implements CodeInsightActionHandler { private void saveCaretPosition(PsiFile file) { int offset = myEditor.getCaretModel().getOffset(); PsiElement el = file.findElementAt(offset); - + if (el == null) return; int innerOffset = offset - el.getTextOffset(); el.putCopyableUserData(CARET_POS_KEY, innerOffset); } diff --git a/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorWriteActionHandler.java b/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorWriteActionHandler.java index 61d9af0fc8e8..fa6de37bde37 100644 --- a/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorWriteActionHandler.java +++ b/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorWriteActionHandler.java @@ -37,7 +37,8 @@ public abstract class EditorWriteActionHandler extends EditorActionHandler { @Override public void doExecute(final Editor editor, @Nullable final Caret caret, final DataContext dataContext) { - if (editor.isViewer() || !EditorModificationUtil.requestWriting(editor)) return; + if (editor.isViewer()) return; + if (!ApplicationManager.getApplication().isWriteAccessAllowed() && !EditorModificationUtil.requestWriting(editor)) return; DocumentRunnable runnable = new DocumentRunnable(editor.getDocument(), editor.getProject()) { @Override