some editor actions: clear read-only status outside write action

This commit is contained in:
peter
2016-12-02 08:26:35 +01:00
parent 2d78283eee
commit 0e534ab16d
8 changed files with 56 additions and 73 deletions
@@ -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);
}
@@ -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;
}
}
}
@@ -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;
}
}
@@ -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));
@@ -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;
@@ -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();
@@ -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<AnAction> collectOptions(Project project, Editor editor, PsiFile file) {
private static List<AnAction> collectOptions(Project project, Editor editor, PsiFile file) {
List<AnAction> 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<AnAction> options, Editor editor) {
private static void showPopup(final List<AnAction> options, Editor editor) {
final ScopeHighlighter highlighter = new ScopeHighlighter(editor);
DefaultListModel m = new DefaultListModel();
DefaultListModel<String> 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);
}
@@ -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