Search in sources :

Example 1 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter 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 EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter 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 EditorHighlighter

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

the class PythonConsoleView method addTextRangeToHistory.

@NotNull
protected String addTextRangeToHistory(@NotNull TextRange textRange, @NotNull EditorEx inputEditor, boolean preserveMarkup) {
    String text;
    EditorHighlighter highlighter;
    if (inputEditor instanceof EditorWindow) {
        PsiFile file = ((EditorWindow) inputEditor).getInjectedFile();
        highlighter = HighlighterFactory.createHighlighter(file.getVirtualFile(), EditorColorsManager.getInstance().getGlobalScheme(), getProject());
        String fullText = InjectedLanguageUtil.getUnescapedText(file, null, null);
        highlighter.setText(fullText);
        text = textRange.substring(fullText);
    } else {
        text = inputEditor.getDocument().getText(textRange);
        highlighter = inputEditor.getHighlighter();
    }
    SyntaxHighlighter syntax = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter) highlighter).getSyntaxHighlighter() : null;
    doAddPromptToHistory(true);
    if (syntax != null) {
        ConsoleViewUtil.printWithHighlighting(this, text, syntax, () -> doAddPromptToHistory(false));
    } else {
        print(text, ConsoleViewContentType.USER_INPUT);
    }
    print("\n", ConsoleViewContentType.NORMAL_OUTPUT);
    return text;
}
Also used : PsiFile(com.intellij.psi.PsiFile) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) EditorWindow(com.intellij.injected.editor.EditorWindow) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter 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 5 with EditorHighlighter

use of com.intellij.openapi.editor.highlighter.EditorHighlighter 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)

Aggregations

EditorHighlighter (com.intellij.openapi.editor.highlighter.EditorHighlighter)63 EditorEx (com.intellij.openapi.editor.ex.EditorEx)36 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)36 Document (com.intellij.openapi.editor.Document)13 IElementType (com.intellij.psi.tree.IElementType)11 Editor (com.intellij.openapi.editor.Editor)8 FileType (com.intellij.openapi.fileTypes.FileType)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 NotNull (org.jetbrains.annotations.NotNull)8 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)7 PsiFile (com.intellij.psi.PsiFile)7 LexerEditorHighlighter (com.intellij.openapi.editor.ex.util.LexerEditorHighlighter)6 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)5 PsiElement (com.intellij.psi.PsiElement)5 Language (com.intellij.lang.Language)4 CaretModel (com.intellij.openapi.editor.CaretModel)4 Project (com.intellij.openapi.project.Project)4 BraceMatcher (com.intellij.codeInsight.highlighting.BraceMatcher)3 EditorWindow (com.intellij.injected.editor.EditorWindow)3 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)3