Search in sources :

Example 1 with HighlighterIterator

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

the class GroovyEnterHandler method handleBetweenSquareBraces.

private static boolean handleBetweenSquareBraces(Editor editor, int caret, DataContext context, Project project, EditorActionHandler originalHandler) {
    String text = editor.getDocument().getText();
    if (text == null || text.isEmpty())
        return false;
    final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    if (caret < 1 || caret > text.length() - 1) {
        return false;
    }
    HighlighterIterator iterator = highlighter.createIterator(caret - 1);
    if (GroovyTokenTypes.mLBRACK == iterator.getTokenType()) {
        if (text.length() > caret) {
            iterator = highlighter.createIterator(caret);
            if (GroovyTokenTypes.mRBRACK == iterator.getTokenType()) {
                originalHandler.execute(editor, context);
                originalHandler.execute(editor, context);
                editor.getCaretModel().moveCaretRelatively(0, -1, false, false, true);
                insertSpacesByGroovyContinuationIndent(editor, project);
                return true;
            }
        }
    }
    return false;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 2 with HighlighterIterator

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

the class GroovyEnterHandler method preprocessEnter.

@Override
public Result preprocessEnter(@NotNull PsiFile file, @NotNull Editor editor, @NotNull Ref<Integer> caretOffset, @NotNull Ref<Integer> caretAdvance, @NotNull DataContext dataContext, EditorActionHandler originalHandler) {
    Document document = editor.getDocument();
    Project project = file.getProject();
    CaretModel caretModel = editor.getCaretModel();
    if (!(file instanceof GroovyFileBase)) {
        return Result.Continue;
    }
    int docLength = document.getTextLength();
    if (docLength == 0) {
        return Result.Continue;
    }
    final int caret = caretModel.getOffset();
    final EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    if (caret >= 1 && caret < docLength && CodeInsightSettings.getInstance().SMART_INDENT_ON_ENTER) {
        HighlighterIterator iterator = highlighter.createIterator(caret);
        iterator.retreat();
        while (!iterator.atEnd() && TokenType.WHITE_SPACE == iterator.getTokenType()) {
            iterator.retreat();
        }
        boolean afterArrow = !iterator.atEnd() && iterator.getTokenType() == GroovyTokenTypes.mCLOSABLE_BLOCK_OP;
        if (afterArrow) {
            originalHandler.execute(editor, dataContext);
            PsiDocumentManager.getInstance(project).commitDocument(document);
            CodeStyleManager.getInstance(project).adjustLineIndent(file, caretModel.getOffset());
        }
        iterator = highlighter.createIterator(caretModel.getOffset());
        while (!iterator.atEnd() && TokenType.WHITE_SPACE == iterator.getTokenType()) {
            iterator.advance();
        }
        if (!iterator.atEnd() && GroovyTokenTypes.mRCURLY == iterator.getTokenType()) {
            PsiDocumentManager.getInstance(project).commitDocument(document);
            final PsiElement element = file.findElementAt(iterator.getStart());
            if (element != null && element.getNode().getElementType() == GroovyTokenTypes.mRCURLY && element.getParent() instanceof GrClosableBlock && docLength > caret && afterArrow) {
                return Result.DefaultForceIndent;
            }
        }
        if (afterArrow) {
            return Result.Stop;
        }
        if (editor.isInsertMode() && !HandlerUtils.isReadOnly(editor) && !editor.getSelectionModel().hasSelection() && handleFlyingGeese(editor, caret, dataContext, originalHandler, file)) {
            return Result.DefaultForceIndent;
        }
    }
    if (handleEnter(editor, dataContext, project, originalHandler))
        return Result.Stop;
    return Result.Continue;
}
Also used : Project(com.intellij.openapi.project.Project) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) CaretModel(com.intellij.openapi.editor.CaretModel) EditorEx(com.intellij.openapi.editor.ex.EditorEx) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 3 with HighlighterIterator

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

the class XmlHighlightingTest method testDTDEditorHighlighting.

public void testDTDEditorHighlighting() throws Exception {
    //                       10        20
    //             012345678901234567890 123456 789
    String text = "<!ENTITY % Charsets \"CDATA\">";
    EditorHighlighter dtdHighlighter = HighlighterFactory.createHighlighter(StdFileTypes.DTD, EditorColorsManager.getInstance().getGlobalScheme(), myProject);
    dtdHighlighter.setText(text);
    HighlighterIterator iterator = dtdHighlighter.createIterator(3);
    assertSame("Xml entity name", iterator.getTokenType(), XmlTokenType.XML_ENTITY_DECL_START);
    iterator = dtdHighlighter.createIterator(13);
    assertSame("Xml name in dtd", iterator.getTokenType(), XmlTokenType.XML_NAME);
    iterator = dtdHighlighter.createIterator(23);
    assertSame("Xml attribute value in dtd", iterator.getTokenType(), XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN);
    //                10        20        30        40
    //      0123456789012345678901 2345678901234567890123456789
    text = "<!ELEMENT base EMPTY>\n<!ATTLIST base id ID #IMPLIED>";
    dtdHighlighter.setText(text);
    iterator = dtdHighlighter.createIterator(3);
    assertSame("Xml element name", iterator.getTokenType(), XmlTokenType.XML_ELEMENT_DECL_START);
    iterator = dtdHighlighter.createIterator(25);
    assertSame("Xml attr list", iterator.getTokenType(), XmlTokenType.XML_ATTLIST_DECL_START);
    iterator = dtdHighlighter.createIterator(14);
    assertSame("Xml attr list", iterator.getTokenType(), TokenType.WHITE_SPACE);
    iterator = dtdHighlighter.createIterator(21);
    assertSame("Xml attr list", iterator.getTokenType(), TokenType.WHITE_SPACE);
    //                10        20        30        40        50        60
    //      0123456789012345678901234567890123456789012345678901234567890123456789
    text = "<![%sgml;[<![IGNORE[<![ INCLUDE [<!ENTITY % aaa SYSTEM 'zzz'>]]>]]>]]>";
    dtdHighlighter.setText(text);
    iterator = dtdHighlighter.createIterator(2);
    assertEquals("Xml conditional section start", XmlTokenType.XML_CONDITIONAL_SECTION_START, iterator.getTokenType());
    iterator = dtdHighlighter.createIterator(67);
    assertEquals("Xml conditional section end", XmlTokenType.XML_CONDITIONAL_SECTION_END, iterator.getTokenType());
    iterator = dtdHighlighter.createIterator(5);
    assertEquals("entity ref in conditional section", XmlTokenType.XML_ENTITY_REF_TOKEN, iterator.getTokenType());
    iterator = dtdHighlighter.createIterator(15);
    assertEquals("ignore in conditional section", XmlTokenType.XML_CONDITIONAL_IGNORE, iterator.getTokenType());
    iterator = dtdHighlighter.createIterator(27);
    assertEquals("include in conditional section", XmlTokenType.XML_CONDITIONAL_INCLUDE, iterator.getTokenType());
    iterator = dtdHighlighter.createIterator(9);
    assertEquals("markup start in conditional section", XmlTokenType.XML_MARKUP_START, iterator.getTokenType());
    iterator = dtdHighlighter.createIterator(33);
    assertEquals("entity decl start in conditional section", XmlTokenType.XML_ENTITY_DECL_START, iterator.getTokenType());
    //                10        20          30         40        50        60         70
    //      012345678901234567890123 456789 0 123456789012345678901234567890123456 7890123456789
    text = "<!ENTITY % ContentType \"CDATA\"\n    -- media type, as per [RFC2045]\n    -- --xxx-->";
    dtdHighlighter.setText(text);
    iterator = dtdHighlighter.createIterator(35);
    assertEquals("Dtd comment start", XmlTokenType.XML_COMMENT_START, iterator.getTokenType());
    iterator = dtdHighlighter.createIterator(40);
    assertEquals("Dtd comment content", XmlTokenType.XML_COMMENT_CHARACTERS, iterator.getTokenType());
    iterator = dtdHighlighter.createIterator(71);
    assertEquals("Dtd comment end", XmlTokenType.XML_COMMENT_END, iterator.getTokenType());
    iterator = dtdHighlighter.createIterator(78);
    assertEquals("Dtd comment content", XmlTokenType.XML_COMMENT_CHARACTERS, iterator.getTokenType());
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 4 with HighlighterIterator

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

the class XmlHighlightingTest method testEditorHighlighting.

public void testEditorHighlighting() throws Exception {
    //                       10
    //             0123456789012
    String text = "<html></html>";
    EditorHighlighter xmlHighlighter = XmlHighlighterFactory.createXMLHighlighter(EditorColorsManager.getInstance().getGlobalScheme());
    xmlHighlighter.setText(text);
    HighlighterIterator iterator = xmlHighlighter.createIterator(1);
    assertSame("Xml tag name", iterator.getTokenType(), XmlTokenType.XML_TAG_NAME);
    iterator = xmlHighlighter.createIterator(8);
    assertSame("Xml tag name at end of tag", iterator.getTokenType(), XmlTokenType.XML_TAG_NAME);
    //               10        20         30
    //      0123456789012345678901234567890
    text = "<a:xxx /> <a:xxx attr = \"1111\"/>";
    xmlHighlighter.setText(text);
    iterator = xmlHighlighter.createIterator(6);
    assertEquals(XmlTokenType.TAG_WHITE_SPACE, iterator.getTokenType());
    iterator = xmlHighlighter.createIterator(21);
    assertEquals(XmlTokenType.TAG_WHITE_SPACE, iterator.getTokenType());
    iterator = xmlHighlighter.createIterator(23);
    assertEquals(XmlTokenType.TAG_WHITE_SPACE, iterator.getTokenType());
    iterator = xmlHighlighter.createIterator(9);
    assertEquals(XmlTokenType.XML_REAL_WHITE_SPACE, iterator.getTokenType());
    //                10        20        30        40          50        60
    //      012345678901234567890123456789012345678901 234567 890123456789012 345678 90
    text = "<!DOCTYPE schema [ <!ENTITY RelativeURL  \"[^:#/\\?]*(:{0,0}|[#/\\?].*)\">";
    xmlHighlighter.setText(text);
    iterator = xmlHighlighter.createIterator(53);
    assertSame("Xml attribute value", iterator.getTokenType(), XmlTokenType.XML_DATA_CHARACTERS);
    assertEquals(iterator.getStart(), 41);
    assertEquals(iterator.getEnd(), 70);
    //              10        20        30        40          50        60
    //    012345678901234567890123456789012345678901 234567 890123456789012 345678 90
    text = "<a name='$'/>";
    xmlHighlighter.setText(text);
    iterator = xmlHighlighter.createIterator(9);
    assertEquals("$ in attr value", XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN, iterator.getTokenType());
    iterator = xmlHighlighter.createIterator(10);
    assertEquals("' after $ in attr value", XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER, iterator.getTokenType());
    //                10         20         30        40           50        60
    //      0123456789012345678 901 23456789012345678901 2345 67 890123456789012 345678 90
    text = "<project><jar file=\"${\"> </jar><jar file=\"${}\"/></project> ";
    xmlHighlighter.setText(text);
    iterator = xmlHighlighter.createIterator(22);
    assertEquals("${ in attr value", XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER, iterator.getTokenType());
    //                10
    //      01234567890123456789
    text = "<!--&nbsp; &aaa;-->";
    xmlHighlighter.setText(text);
    iterator = xmlHighlighter.createIterator(5);
    assertEquals("char entity in comment", XmlTokenType.XML_COMMENT_CHARACTERS, iterator.getTokenType());
    iterator = xmlHighlighter.createIterator(12);
    assertEquals("entity ref in comment", XmlTokenType.XML_COMMENT_CHARACTERS, iterator.getTokenType());
    //                10
    //      01234567890123456789
    text = "<!-- -- -->";
    xmlHighlighter.setText(text);
    iterator = xmlHighlighter.createIterator(5);
    assertEquals("double -- in xml comment", XmlTokenType.XML_BAD_CHARACTER, iterator.getTokenType());
}
Also used : HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 5 with HighlighterIterator

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

the class KotlinTypedHandler method indentBrace.

/**
     * Copied from
     * @see com.intellij.codeInsight.editorActions.TypedHandler#indentBrace(Project, Editor, char)
     */
private static void indentBrace(@NotNull final Project project, @NotNull final Editor editor, char braceChar) {
    final int offset = editor.getCaretModel().getOffset() - 1;
    Document document = editor.getDocument();
    CharSequence chars = document.getCharsSequence();
    if (offset < 0 || chars.charAt(offset) != braceChar)
        return;
    int spaceStart = CharArrayUtil.shiftBackward(chars, offset - 1, " \t");
    if (spaceStart < 0 || chars.charAt(spaceStart) == '\n' || chars.charAt(spaceStart) == '\r') {
        PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
        documentManager.commitDocument(document);
        final PsiFile file = documentManager.getPsiFile(document);
        if (file == null || !file.isWritable())
            return;
        PsiElement element = file.findElementAt(offset);
        if (element == null)
            return;
        EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
        HighlighterIterator iterator = highlighter.createIterator(offset);
        FileType fileType = file.getFileType();
        BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
        boolean isBrace = braceMatcher.isLBraceToken(iterator, chars, fileType) || braceMatcher.isRBraceToken(iterator, chars, fileType);
        if (element.getNode() != null && isBrace) {
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                @Override
                public void run() {
                    int newOffset = CodeStyleManager.getInstance(project).adjustLineIndent(file, offset);
                    editor.getCaretModel().moveToOffset(newOffset + 1);
                    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
                    editor.getSelectionModel().removeSelection();
                }
            });
        }
    }
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Document(com.intellij.openapi.editor.Document) FileType(com.intellij.openapi.fileTypes.FileType) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

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