Example usage for com.intellij.openapi.editor EditorModificationUtil typeInStringAtCaretHonorMultipleCarets

List of usage examples for com.intellij.openapi.editor EditorModificationUtil typeInStringAtCaretHonorMultipleCarets

Introduction

In this page you can find the example usage for com.intellij.openapi.editor EditorModificationUtil typeInStringAtCaretHonorMultipleCarets.

Prototype

public static void typeInStringAtCaretHonorMultipleCarets(final Editor editor, @NotNull final String str,
        final boolean toProcessOverwriteMode, final int caretShift)
        throws ReadOnlyFragmentModificationException 

Source Link

Document

Inserts given string at each caret's position.

Usage

From source file:com.intellij.coldFusion.UI.editorActions.typedHandlers.CfmlTypedHandler.java

License:Apache License

public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile file,
        final FileType fileType) {
    PsiFile cfmlFile = file.getViewProvider().getPsi(CfmlLanguage.INSTANCE);

    if (isNotCfmlFile(cfmlFile, editor)) {
        return Result.CONTINUE;
    }/*from   w  w  w .java2 s  . c  o m*/
    int offset = editor.getCaretModel().getOffset();

    if (c == '{') {
        CfmlBraceMatcher braceMatcher = new CfmlBraceMatcher();
        HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
        if (!braceMatcher.isLBraceToken(iterator, editor.getDocument().getCharsSequence(), fileType)) {
            EditorModificationUtil.typeInStringAtCaretHonorMultipleCarets(editor, "}", true, 0);
            // return Result.STOP;
        }
        return Result.CONTINUE;
    }
    if (c == '#') {
        if (CfmlEditorUtil.countSharpsBalance(editor) == 0) {
            char charAtOffset = DocumentUtils.getCharAt(editor.getDocument(), offset);
            if (charAtOffset == '#') {
                EditorModificationUtil.moveAllCaretsRelatively(editor, 1);
                return Result.STOP;
            }
            EditorModificationUtil.typeInStringAtCaretHonorMultipleCarets(editor, "#", true, 0);
        }
    } else if (c == '>') {
        if (((EditorEx) editor).getHighlighter().createIterator(editor.getCaretModel().getOffset())
                .getTokenType() == CfmlTokenTypes.COMMENT
                || ((EditorEx) editor).getHighlighter().createIterator(editor.getCaretModel().getOffset())
                        .getTokenType().getLanguage() != CfmlLanguage.INSTANCE) {
            return Result.CONTINUE;
        }
        insertCloseTagIfNeeded(editor, cfmlFile, project);
        return Result.STOP;
    }
    return Result.CONTINUE;
}

From source file:com.intellij.coldFusion.UI.editorActions.typedHandlers.CfmlTypedHandler.java

License:Apache License

public static boolean insertCloseTagIfNeeded(Editor editor, PsiFile file, Project project) {
    final Document document = editor.getDocument();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);

    int offset = editor.getCaretModel().getOffset();
    documentManager.commitDocument(document);
    char charAtOffset = DocumentUtils.getCharAt(document, offset);

    if (charAtOffset != '>') {
        EditorModificationUtil.typeInStringAtCaretHonorMultipleCarets(editor, ">", true, 0);
    }/*from www . j av a  2s .  c  om*/
    EditorModificationUtil.moveAllCaretsRelatively(editor, 1);
    ++offset;
    if (DocumentUtils.getCharAt(document, offset - 2) == '/') {
        return false;
    }
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 2);

    while (!iterator.atEnd() && !iterator.getTokenType().equals(CfmlTokenTypes.CF_TAG_NAME)) {
        if (CfmlUtil.isControlToken(iterator.getTokenType())) {
            return false;
        }
        iterator.retreat();
    }
    if (!iterator.atEnd()) {
        iterator.retreat();
        if (!iterator.atEnd() && iterator.getTokenType().equals(CfmlTokenTypes.LSLASH_ANGLEBRACKET)) {
            return false;
        }
        iterator.advance();
    }
    if (iterator.atEnd()) {
        return false;
    }
    String tagName = document.getCharsSequence().subSequence(iterator.getStart(), iterator.getEnd()).toString();
    if (CfmlUtil.isSingleCfmlTag(tagName, project) || CfmlUtil.isUserDefined(tagName)) {
        return false;
    }
    PsiElement tagElement = file.findElementAt(iterator.getStart());
    while (tagElement != null && !(tagElement instanceof CfmlTag)) {
        tagElement = tagElement.getParent();
    }
    if (tagElement == null) {
        return false;
    }
    boolean doInsertion = false;
    if (tagElement.getLastChild() instanceof PsiErrorElement) {
        doInsertion = true;
    } else {
        iterator = ((EditorEx) editor).getHighlighter().createIterator(0);
        while (!iterator.atEnd() && iterator.getStart() < offset) {
            if (iterator.getTokenType() == CfmlTokenTypes.CF_TAG_NAME) {
                String currentTagName = document.getCharsSequence()
                        .subSequence(iterator.getStart(), iterator.getEnd()).toString();
                if (tagName.equals(currentTagName)) {
                    PsiElement currentTagElement = file.findElementAt(iterator.getStart());
                    currentTagElement = PsiTreeUtil.getParentOfType(currentTagElement, CfmlTag.class);
                    if (currentTagElement.getLastChild() instanceof PsiErrorElement) {
                        doInsertion = true;
                        break;
                    }
                }
            }
            iterator.advance();
        }
    }
    if (doInsertion && CfmlUtil.isEndTagRequired(((CfmlTag) tagElement).getTagName(), project)) {
        EditorModificationUtil.typeInStringAtCaretHonorMultipleCarets(editor,
                "</" + ((CfmlTag) tagElement).getTagName() + ">", true, 0);
        return true;
    }
    return false;
}