Search in sources :

Example 11 with UndoManager

use of com.intellij.openapi.command.undo.UndoManager in project intellij-community by JetBrains.

the class UndoRedoAction method actionPerformed.

public void actionPerformed(AnActionEvent e) {
    DataContext dataContext = e.getDataContext();
    FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(dataContext);
    UndoManager undoManager = getUndoManager(editor, dataContext);
    perform(editor, undoManager);
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) FileEditor(com.intellij.openapi.fileEditor.FileEditor) UndoManager(com.intellij.openapi.command.undo.UndoManager)

Example 12 with UndoManager

use of com.intellij.openapi.command.undo.UndoManager in project intellij-community by JetBrains.

the class XmlTagInsertHandler method insertIncompleteTag.

public static void insertIncompleteTag(char completionChar, final Editor editor, XmlTag tag) {
    XmlElementDescriptor descriptor = tag.getDescriptor();
    final Project project = editor.getProject();
    TemplateManager templateManager = TemplateManager.getInstance(project);
    Template template = templateManager.createTemplate("", "");
    template.setToIndent(true);
    // temp code
    PsiFile containingFile = tag.getContainingFile();
    boolean htmlCode = HtmlUtil.hasHtml(containingFile) || HtmlUtil.supportsXmlTypedHandlers(containingFile);
    template.setToReformat(!htmlCode);
    StringBuilder indirectRequiredAttrs = addRequiredAttributes(descriptor, tag, template, containingFile);
    final boolean chooseAttributeName = addTail(completionChar, descriptor, htmlCode, tag, template, indirectRequiredAttrs);
    templateManager.startTemplate(editor, template, new TemplateEditingAdapter() {

        private RangeMarker myAttrValueMarker;

        @Override
        public void waitingForInput(Template template) {
            int offset = editor.getCaretModel().getOffset();
            myAttrValueMarker = editor.getDocument().createRangeMarker(offset + 1, offset + 4);
        }

        @Override
        public void templateFinished(final Template template, boolean brokenOff) {
            final int offset = editor.getCaretModel().getOffset();
            if (chooseAttributeName && offset > 0) {
                char c = editor.getDocument().getCharsSequence().charAt(offset - 1);
                if (c == '/' || (c == ' ' && brokenOff)) {
                    new WriteCommandAction.Simple(project) {

                        @Override
                        protected void run() throws Throwable {
                            editor.getDocument().replaceString(offset, offset + 3, ">");
                        }
                    }.execute();
                }
            }
        }

        @Override
        public void templateCancelled(final Template template) {
            if (myAttrValueMarker == null) {
                return;
            }
            final UndoManager manager = UndoManager.getInstance(project);
            if (manager.isUndoInProgress() || manager.isRedoInProgress()) {
                return;
            }
            if (chooseAttributeName && myAttrValueMarker.isValid()) {
                final int startOffset = myAttrValueMarker.getStartOffset();
                final int endOffset = myAttrValueMarker.getEndOffset();
                new WriteCommandAction.Simple(project) {

                    @Override
                    protected void run() throws Throwable {
                        editor.getDocument().replaceString(startOffset, endOffset, ">");
                    }
                }.execute();
            }
        }
    });
}
Also used : Project(com.intellij.openapi.project.Project) UndoManager(com.intellij.openapi.command.undo.UndoManager) TemplateManager(com.intellij.codeInsight.template.TemplateManager) TemplateEditingAdapter(com.intellij.codeInsight.template.TemplateEditingAdapter) PsiFile(com.intellij.psi.PsiFile) RangeMarker(com.intellij.openapi.editor.RangeMarker) Template(com.intellij.codeInsight.template.Template)

Aggregations

UndoManager (com.intellij.openapi.command.undo.UndoManager)12 Project (com.intellij.openapi.project.Project)4 FileEditor (com.intellij.openapi.fileEditor.FileEditor)3 DataContext (com.intellij.openapi.actionSystem.DataContext)2 TextEditor (com.intellij.openapi.fileEditor.TextEditor)2 Template (com.intellij.codeInsight.template.Template)1 TemplateEditingAdapter (com.intellij.codeInsight.template.TemplateEditingAdapter)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 Presentation (com.intellij.openapi.actionSystem.Presentation)1 Application (com.intellij.openapi.application.Application)1 AbnormalCommandTerminationException (com.intellij.openapi.command.AbnormalCommandTerminationException)1 DocumentReference (com.intellij.openapi.command.undo.DocumentReference)1 GlobalUndoableAction (com.intellij.openapi.command.undo.GlobalUndoableAction)1 UndoableAction (com.intellij.openapi.command.undo.UndoableAction)1 Attachment (com.intellij.openapi.diagnostic.Attachment)1 Document (com.intellij.openapi.editor.Document)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 PsiFile (com.intellij.psi.PsiFile)1 Charset (java.nio.charset.Charset)1