Search in sources :

Example 1 with CaretAction

use of com.intellij.openapi.editor.CaretAction in project intellij-community by JetBrains.

the class ZenCodingTemplate method doWrap.

public static void doWrap(@NotNull final String abbreviation, @NotNull final CustomTemplateCallback callback) {
    final ZenCodingGenerator defaultGenerator = findApplicableDefaultGenerator(callback.getContext(), true);
    assert defaultGenerator != null;
    ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(callback.getProject(), () -> callback.getEditor().getCaretModel().runForEachCaret(new CaretAction() {

        @Override
        public void perform(Caret caret) {
            String selectedText = callback.getEditor().getSelectionModel().getSelectedText();
            if (selectedText != null) {
                String selection = selectedText.trim();
                ZenCodingNode node = parse(abbreviation, callback, defaultGenerator, selection);
                assert node != null;
                PsiElement context = callback.getContext();
                ZenCodingGenerator generator = findApplicableGenerator(node, context, true);
                List<ZenCodingFilter> filters = getFilters(node, context);
                EditorModificationUtil.deleteSelectedText(callback.getEditor());
                PsiDocumentManager.getInstance(callback.getProject()).commitAllDocuments();
                try {
                    expand(node, generator, filters, selection, callback, true, Registry.intValue("emmet.segments.limit"));
                } catch (EmmetException e) {
                    CommonRefactoringUtil.showErrorHint(callback.getProject(), callback.getEditor(), e.getMessage(), "Emmet error", "");
                }
            }
        }
    }), CodeInsightBundle.message("insert.code.template.command"), null));
}
Also used : XmlZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator) ZenCodingGenerator(com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator) Caret(com.intellij.openapi.editor.Caret) PsiElement(com.intellij.psi.PsiElement) CaretAction(com.intellij.openapi.editor.CaretAction)

Example 2 with CaretAction

use of com.intellij.openapi.editor.CaretAction in project intellij-community by JetBrains.

the class InvokeTemplateAction method perform.

public void perform() {
    final Document document = myEditor.getDocument();
    final VirtualFile file = FileDocumentManager.getInstance().getFile(document);
    if (file != null) {
        ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(file);
    }
    CommandProcessor.getInstance().executeCommand(myProject, () -> myEditor.getCaretModel().runForEachCaret(new CaretAction() {

        public void perform(Caret caret) {
            // at a meaningful position rather than at indent 0)
            if (myEditor.getSelectionModel().hasSelection() && myTemplate.isToReformat()) {
                int offset = myEditor.getSelectionModel().getSelectionStart();
                int selectionEnd = myEditor.getSelectionModel().getSelectionEnd();
                int lineEnd = document.getLineEndOffset(document.getLineNumber(offset));
                while (offset < lineEnd && offset < selectionEnd && (document.getCharsSequence().charAt(offset) == ' ' || document.getCharsSequence().charAt(offset) == '\t')) {
                    offset++;
                }
                // avoid extra line break after $SELECTION$ in case when selection ends with a complete line
                if (selectionEnd == document.getLineStartOffset(document.getLineNumber(selectionEnd))) {
                    selectionEnd--;
                }
                if (offset < lineEnd && offset < selectionEnd) {
                    // found non-WS character in first line of selection
                    myEditor.getSelectionModel().setSelection(offset, selectionEnd);
                }
            }
            String selectionString = myEditor.getSelectionModel().getSelectedText();
            TemplateManager.getInstance(myProject).startTemplate(myEditor, selectionString, myTemplate);
        }
    }), "Wrap with template", "Wrap with template " + myTemplate.getKey());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) Caret(com.intellij.openapi.editor.Caret) CaretAction(com.intellij.openapi.editor.CaretAction)

Example 3 with CaretAction

use of com.intellij.openapi.editor.CaretAction in project intellij-community by JetBrains.

the class EditorActionHandler method isEnabled.

/**
   * @deprecated Implementations should override
   * {@link #isEnabledForCaret(Editor, Caret, DataContext)}
   * instead,
   * client code should invoke
   * {@link #isEnabled(Editor, Caret, DataContext)}
   * instead.
   */
public boolean isEnabled(Editor editor, final DataContext dataContext) {
    if (inCheck) {
        return true;
    }
    inCheck = true;
    try {
        if (editor == null) {
            return false;
        }
        Editor hostEditor = dataContext == null ? null : CommonDataKeys.HOST_EDITOR.getData(dataContext);
        if (hostEditor == null) {
            hostEditor = editor;
        }
        final boolean[] result = new boolean[1];
        final CaretTask check = new CaretTask() {

            @Override
            public void perform(@NotNull Caret caret, @Nullable DataContext dataContext) {
                result[0] = true;
            }
        };
        if (myRunForEachCaret) {
            hostEditor.getCaretModel().runForEachCaret(new CaretAction() {

                @Override
                public void perform(Caret caret) {
                    doIfEnabled(caret, dataContext, check);
                }
            });
        } else {
            doIfEnabled(hostEditor.getCaretModel().getCurrentCaret(), dataContext, check);
        }
        return result[0];
    } finally {
        inCheck = false;
    }
}
Also used : DataContext(com.intellij.openapi.actionSystem.DataContext) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull) Caret(com.intellij.openapi.editor.Caret) Nullable(org.jetbrains.annotations.Nullable) CaretAction(com.intellij.openapi.editor.CaretAction)

Aggregations

Caret (com.intellij.openapi.editor.Caret)3 CaretAction (com.intellij.openapi.editor.CaretAction)3 XmlZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator)1 ZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1