Search in sources :

Example 31 with HighlighterIterator

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

the class BackspaceHandler method handleBackspace.

protected boolean handleBackspace(Editor editor, Caret caret, DataContext dataContext, boolean toWordStart) {
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null)
        return false;
    PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
    if (file == null)
        return false;
    if (editor.getSelectionModel().hasSelection())
        return false;
    int offset = DocumentUtil.getPreviousCodePointOffset(editor.getDocument(), editor.getCaretModel().getOffset());
    if (offset < 0)
        return false;
    CharSequence chars = editor.getDocument().getCharsSequence();
    int c = Character.codePointAt(chars, offset);
    final Editor injectedEditor = TypedHandler.injectedEditorIfCharTypedIsSignificant(c, editor, file);
    final Editor originalEditor = editor;
    if (injectedEditor != editor) {
        int injectedOffset = injectedEditor.getCaretModel().getOffset();
        if (isOffsetInsideInjected(injectedEditor, injectedOffset)) {
            file = PsiDocumentManager.getInstance(project).getPsiFile(injectedEditor.getDocument());
            editor = injectedEditor;
            offset = DocumentUtil.getPreviousCodePointOffset(injectedEditor.getDocument(), injectedOffset);
        }
    }
    final BackspaceHandlerDelegate[] delegates = Extensions.getExtensions(BackspaceHandlerDelegate.EP_NAME);
    if (!toWordStart && Character.isBmpCodePoint(c)) {
        for (BackspaceHandlerDelegate delegate : delegates) {
            delegate.beforeCharDeleted((char) c, file, editor);
        }
    }
    FileType fileType = file.getFileType();
    final QuoteHandler quoteHandler = TypedHandler.getQuoteHandler(file, editor);
    HighlighterIterator hiterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    boolean wasClosingQuote = quoteHandler != null && quoteHandler.isClosingQuote(hiterator, offset);
    myOriginalHandler.execute(originalEditor, caret, dataContext);
    if (!toWordStart && Character.isBmpCodePoint(c)) {
        for (BackspaceHandlerDelegate delegate : delegates) {
            if (delegate.charDeleted((char) c, file, editor)) {
                return true;
            }
        }
    }
    if (offset >= editor.getDocument().getTextLength())
        return true;
    chars = editor.getDocument().getCharsSequence();
    if ((c == '(' || c == '[' || c == '{') && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
        char c1 = chars.charAt(offset);
        if (c1 != getRightChar((char) c))
            return true;
        HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
        BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
        if (!braceMatcher.isLBraceToken(iterator, chars, fileType) && !braceMatcher.isRBraceToken(iterator, chars, fileType)) {
            return true;
        }
        int rparenOffset = BraceMatchingUtil.findRightmostRParen(iterator, iterator.getTokenType(), chars, fileType);
        if (rparenOffset >= 0) {
            iterator = ((EditorEx) editor).getHighlighter().createIterator(rparenOffset);
            boolean matched = BraceMatchingUtil.matchBrace(chars, fileType, iterator, false);
            if (matched)
                return true;
        }
        editor.getDocument().deleteString(offset, offset + 1);
    } else if ((c == '"' || c == '\'' || c == '`') && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_QUOTE) {
        char c1 = chars.charAt(offset);
        if (c1 != c)
            return true;
        if (wasClosingQuote)
            return true;
        HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
        if (quoteHandler == null || !quoteHandler.isOpeningQuote(iterator, offset))
            return true;
        editor.getDocument().deleteString(offset, offset + 1);
    }
    return true;
}
Also used : BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Project(com.intellij.openapi.project.Project) FileType(com.intellij.openapi.fileTypes.FileType) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 32 with HighlighterIterator

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

the class BraceMatcherBasedSelectioner method select.

@Override
public List<TextRange> select(final PsiElement e, final CharSequence editorText, final int cursorOffset, final Editor editor) {
    final VirtualFile file = e.getContainingFile().getVirtualFile();
    final FileType fileType = file == null ? null : file.getFileType();
    if (fileType == null)
        return super.select(e, editorText, cursorOffset, editor);
    final int textLength = editorText.length();
    final TextRange totalRange = e.getTextRange();
    final HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(totalRange.getStartOffset());
    final BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
    final ArrayList<TextRange> result = new ArrayList<>();
    final LinkedList<Trinity<Integer, Integer, IElementType>> stack = new LinkedList<>();
    while (!iterator.atEnd() && iterator.getStart() < totalRange.getEndOffset()) {
        final Trinity<Integer, Integer, IElementType> last;
        if (braceMatcher.isLBraceToken(iterator, editorText, fileType)) {
            stack.addLast(Trinity.create(iterator.getStart(), iterator.getEnd(), iterator.getTokenType()));
        } else if (braceMatcher.isRBraceToken(iterator, editorText, fileType) && !stack.isEmpty() && braceMatcher.isPairBraces((last = stack.getLast()).third, iterator.getTokenType())) {
            stack.removeLast();
            result.addAll(expandToWholeLine(editorText, new TextRange(last.first, iterator.getEnd())));
            int bodyStart = last.second;
            int bodyEnd = iterator.getStart();
            while (bodyStart < textLength && Character.isWhitespace(editorText.charAt(bodyStart))) bodyStart++;
            while (bodyEnd > 0 && bodyStart < bodyEnd && Character.isWhitespace(editorText.charAt(bodyEnd - 1))) bodyEnd--;
            result.addAll(expandToWholeLine(editorText, new TextRange(bodyStart, bodyEnd)));
        }
        iterator.advance();
    }
    result.add(e.getTextRange());
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Trinity(com.intellij.openapi.util.Trinity) ArrayList(java.util.ArrayList) TextRange(com.intellij.openapi.util.TextRange) LinkedList(java.util.LinkedList) IElementType(com.intellij.psi.tree.IElementType) FileType(com.intellij.openapi.fileTypes.FileType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 33 with HighlighterIterator

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

the class AutoFormatTypedHandler method isInsertSpaceAtCaret.

private static boolean isInsertSpaceAtCaret(@NotNull Editor editor, char charTyped, @NotNull DataContext dataContext) {
    if (!isSpaceAroundAssignment(editor, dataContext)) {
        return false;
    }
    int caretOffset = editor.getCaretModel().getOffset();
    CharSequence text = editor.getDocument().getImmutableCharSequence();
    HighlighterIterator lexerIterator = createLexerIterator(editor, caretOffset);
    if (lexerIterator == null || lexerIterator.getTokenType() == JavaTokenType.STRING_LITERAL) {
        return false;
    }
    boolean insertBeforeEq = charTyped == '=' && isInsertSpaceBeforeEq(caretOffset, text);
    boolean insertAfterEq = caretOffset > 0 && caretOffset - 1 < text.length() && text.charAt(caretOffset - 1) == '=' && isAssignmentOperator(lexerIterator) && isInsertSpaceAfterEq(charTyped);
    return (insertBeforeEq || insertAfterEq);
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 34 with HighlighterIterator

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

the class JavaBackspaceHandler method handleLTDeletion.

public static void handleLTDeletion(final Editor editor, final int offset, final IElementType lt, final IElementType gt, final TokenSet invalidInsideReference) {
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset);
    while (iterator.getStart() > 0 && !invalidInsideReference.contains(iterator.getTokenType())) {
        iterator.retreat();
    }
    if (invalidInsideReference.contains(iterator.getTokenType()))
        iterator.advance();
    int balance = 0;
    while (!iterator.atEnd() && balance >= 0) {
        final IElementType tokenType = iterator.getTokenType();
        if (tokenType == lt) {
            balance++;
        } else if (tokenType == gt) {
            balance--;
        } else if (invalidInsideReference.contains(tokenType)) {
            break;
        }
        iterator.advance();
    }
    if (balance < 0) {
        editor.getDocument().deleteString(offset, offset + 1);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 35 with HighlighterIterator

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

the class JavaTypedHandler method handleSemicolon.

private static boolean handleSemicolon(Editor editor, FileType fileType) {
    if (fileType != StdFileTypes.JAVA)
        return false;
    int offset = editor.getCaretModel().getOffset();
    if (offset == editor.getDocument().getTextLength())
        return false;
    char charAt = editor.getDocument().getCharsSequence().charAt(offset);
    if (charAt != ';')
        return false;
    HighlighterIterator hi = ((EditorEx) editor).getHighlighter().createIterator(offset);
    if (hi.atEnd() || hi.getTokenType() != JavaTokenType.SEMICOLON)
        return false;
    EditorModificationUtil.moveCaretRelatively(editor, 1);
    return true;
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Aggregations

HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)83 EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)36 EditorEx (com.intellij.openapi.editor.ex.EditorEx)33 IElementType (com.intellij.psi.tree.IElementType)33 Document (com.intellij.openapi.editor.Document)12 FileType (com.intellij.openapi.fileTypes.FileType)12 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)9 TextRange (com.intellij.openapi.util.TextRange)8 Editor (com.intellij.openapi.editor.Editor)7 PsiElement (com.intellij.psi.PsiElement)6 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)5 PsiFile (com.intellij.psi.PsiFile)5 Language (com.intellij.lang.Language)4 CaretModel (com.intellij.openapi.editor.CaretModel)4 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)4 Project (com.intellij.openapi.project.Project)4 NontrivialBraceMatcher (com.intellij.codeInsight.highlighting.NontrivialBraceMatcher)3 Trinity (com.intellij.openapi.util.Trinity)3 ArrayList (java.util.ArrayList)3 LineMarkerInfo (com.intellij.codeInsight.daemon.LineMarkerInfo)2