Search in sources :

Example 31 with CaretModel

use of com.intellij.openapi.editor.CaretModel in project intellij-plugins by JetBrains.

the class FlexLiveTemplatesTest method testPublicFunctionWithTypingInMxml.

public void testPublicFunctionWithTypingInMxml() throws Exception {
    doTest("pf", "mxml", segmentIndex -> {
        final CaretModel caretModel = getEditor().getCaretModel();
        switch(segmentIndex) {
            case 0:
                type("functionName");
                caretModel.moveToOffset(caretModel.getOffset() + 1);
                break;
            case 1:
                type("param:Object");
                caretModel.moveToOffset(caretModel.getOffset() + 1);
                break;
            case 2:
                type("String");
                caretModel.moveToOffset(caretModel.getOffset() + 1);
                break;
        }
    });
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel)

Example 32 with CaretModel

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

the class UnicodeUnescapeIntention method processIntention.

@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
        // does not check if Unicode escape is inside char or string literal (garbage in, garbage out)
        final Document document = editor.getDocument();
        final int start = selectionModel.getSelectionStart();
        final int end = selectionModel.getSelectionEnd();
        final String text = document.getText(new TextRange(start, end));
        final int textLength = end - start;
        final StringBuilder replacement = new StringBuilder(textLength);
        int anchor = 0;
        while (true) {
            final int index = indexOfUnicodeEscape(text, anchor + 1);
            if (index < 0) {
                break;
            }
            replacement.append(text.substring(anchor, index));
            int hexStart = index + 1;
            while (text.charAt(hexStart) == 'u') {
                hexStart++;
            }
            anchor = hexStart + 4;
            final int c = Integer.parseInt(text.substring(hexStart, anchor), 16);
            replacement.appendCodePoint(c);
        }
        replacement.append(text.substring(anchor, textLength));
        document.replaceString(start, end, replacement);
    } else {
        final CaretModel caretModel = editor.getCaretModel();
        final Document document = editor.getDocument();
        final int lineNumber = document.getLineNumber(caretModel.getOffset());
        final int lineStartOffset = document.getLineStartOffset(lineNumber);
        final String line = document.getText(new TextRange(lineStartOffset, document.getLineEndOffset(lineNumber)));
        final int column = caretModel.getLogicalPosition().column;
        final int index1 = indexOfUnicodeEscape(line, column);
        final int index2 = indexOfUnicodeEscape(line, column + 1);
        // if the caret is between two unicode escapes, replace the one to the right
        final int escapeStart = index2 == column ? index2 : index1;
        int hexStart = escapeStart + 1;
        while (line.charAt(hexStart) == 'u') {
            hexStart++;
        }
        final char c = (char) Integer.parseInt(line.substring(hexStart, hexStart + 4), 16);
        if (Character.isHighSurrogate(c)) {
            hexStart += 4;
            if (line.charAt(hexStart++) == '\\' && line.charAt(hexStart++) == 'u') {
                while (line.charAt(hexStart) == 'u') hexStart++;
                final char d = (char) Integer.parseInt(line.substring(hexStart, hexStart + 4), 16);
                document.replaceString(lineStartOffset + escapeStart, lineStartOffset + hexStart + 4, String.valueOf(new char[] { c, d }));
                return;
            }
        } else if (Character.isLowSurrogate(c)) {
            if (escapeStart >= 6 && StringUtil.isHexDigit(line.charAt(escapeStart - 1)) && StringUtil.isHexDigit(line.charAt(escapeStart - 2)) && StringUtil.isHexDigit(line.charAt(escapeStart - 3)) && StringUtil.isHexDigit(line.charAt(escapeStart - 4))) {
                int i = escapeStart - 5;
                while (i > 0 && line.charAt(i) == 'u') i--;
                if (line.charAt(i) == '\\' && (i == 0 || line.charAt(i - 1) != '\\')) {
                    final char b = (char) Integer.parseInt(line.substring(escapeStart - 4, escapeStart), 16);
                    document.replaceString(lineStartOffset + i, lineStartOffset + hexStart + 4, String.valueOf(new char[] { b, c }));
                    return;
                }
            }
        }
        document.replaceString(lineStartOffset + escapeStart, lineStartOffset + hexStart + 4, String.valueOf(c));
    }
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) SelectionModel(com.intellij.openapi.editor.SelectionModel) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document)

Example 33 with CaretModel

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

the class FlipExpressionIntention method processIntention.

@Override
protected void processIntention(Editor editor, @NotNull PsiElement element) {
    final CaretModel caretModel = editor.getCaretModel();
    final int offset = caretModel.getOffset();
    super.processIntention(editor, element);
    caretModel.moveToOffset(offset);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel)

Example 34 with CaretModel

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

the class FoldingProcessingOnDocumentModificationTest method testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange.

public void testUnexpectedClassLevelJavadocExpandingOnClassSignatureChange() throws IOException {
    // Inspired by IDEA-61275
    String text = "/**\n" + " * This is a test comment\n" + " */\n" + "public <caret>class Test {\n" + "}";
    init(text, TestFileType.JAVA);
    CaretModel caretModel = myEditor.getCaretModel();
    int caretOffset = caretModel.getOffset();
    assertEquals(caretOffset, caretModel.getOffset());
    updateFoldRegions();
    toggleFoldRegionState(getFoldRegion(0), false);
    type('a');
    updateFoldRegions();
    assertEquals(caretOffset + 1, caretModel.getOffset());
    assertEquals(1, myEditor.getFoldingModel().getAllFoldRegions().length);
    FoldRegion foldRegion = getFoldRegion(0);
    assertFalse(foldRegion.isExpanded());
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) FoldRegion(com.intellij.openapi.editor.FoldRegion)

Example 35 with CaretModel

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

the class SuppressIntentionAction method getElement.

@Nullable
private static PsiElement getElement(@NotNull Editor editor, @NotNull PsiFile file) {
    CaretModel caretModel = editor.getCaretModel();
    int position = caretModel.getOffset();
    return file.findElementAt(position);
}
Also used : CaretModel(com.intellij.openapi.editor.CaretModel) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CaretModel (com.intellij.openapi.editor.CaretModel)57 Document (com.intellij.openapi.editor.Document)23 PsiElement (com.intellij.psi.PsiElement)14 SelectionModel (com.intellij.openapi.editor.SelectionModel)10 TextRange (com.intellij.openapi.util.TextRange)9 Editor (com.intellij.openapi.editor.Editor)8 Project (com.intellij.openapi.project.Project)7 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)6 Nullable (org.jetbrains.annotations.Nullable)6 EditorEx (com.intellij.openapi.editor.ex.EditorEx)5 IElementType (com.intellij.psi.tree.IElementType)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)3 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)3 PsiFile (com.intellij.psi.PsiFile)3 List (java.util.List)3 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)2 TemplateState (com.intellij.codeInsight.template.impl.TemplateState)2 Caret (com.intellij.openapi.editor.Caret)2 ScrollingModel (com.intellij.openapi.editor.ScrollingModel)2