Search in sources :

Example 16 with HighlighterIterator

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

the class CustomFileTypeCompletionContributor method inCommentOrLiteral.

private static boolean inCommentOrLiteral(CompletionParameters parameters) {
    HighlighterIterator iterator = ((EditorEx) parameters.getEditor()).getHighlighter().createIterator(parameters.getOffset());
    if (iterator.atEnd())
        return false;
    IElementType elementType = iterator.getTokenType();
    if (elementType == CustomHighlighterTokenType.WHITESPACE) {
        iterator.retreat();
        elementType = iterator.getTokenType();
    }
    return elementType == CustomHighlighterTokenType.LINE_COMMENT || elementType == CustomHighlighterTokenType.MULTI_LINE_COMMENT || elementType == CustomHighlighterTokenType.STRING || elementType == CustomHighlighterTokenType.SINGLE_QUOTED_STRING;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 17 with HighlighterIterator

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

the class HTMLTextPainter method writeStyles.

private void writeStyles(@NonNls final Writer writer) throws IOException {
    EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
    writer.write("<style type=\"text/css\">\n");
    final Color lineNumbers = scheme.getColor(EditorColors.LINE_NUMBERS_COLOR);
    writer.write(String.format(".ln { color: #%s; font-weight: normal; font-style: normal; }\n", ColorUtil.toHex(lineNumbers == null ? Gray.x00 : lineNumbers)));
    HighlighterIterator hIterator = myHighlighter.createIterator(myOffset);
    while (!hIterator.atEnd()) {
        TextAttributes textAttributes = hIterator.getTextAttributes();
        if (!myStyleMap.containsKey(textAttributes)) {
            @NonNls String styleName = "s" + myStyleMap.size();
            myStyleMap.put(textAttributes, styleName);
            writer.write("." + styleName + " { ");
            Color foreColor = textAttributes.getForegroundColor();
            if (foreColor == null)
                foreColor = scheme.getDefaultForeground();
            writer.write("color: " + colorToHtml(foreColor) + "; ");
            if (BitUtil.isSet(textAttributes.getFontType(), Font.BOLD)) {
                writer.write("font-weight: bold; ");
            }
            if (BitUtil.isSet(textAttributes.getFontType(), Font.ITALIC)) {
                writer.write("font-style: italic; ");
            }
            writer.write("}\n");
        }
        hIterator.advance();
    }
    for (LineMarkerInfo separator : myMethodSeparators) {
        Color color = separator.separatorColor;
        if (color != null && !mySeparatorStyles.containsKey(color)) {
            @NonNls String styleName = "ls" + mySeparatorStyles.size();
            mySeparatorStyles.put(color, styleName);
            String htmlColor = colorToHtml(color);
            writer.write("." + styleName + " { height: 1px; border-width: 0; color: " + htmlColor + "; background-color:" + htmlColor + "}\n");
        }
    }
    writer.write("</style>\n");
}
Also used : NonNls(org.jetbrains.annotations.NonNls) LineMarkerInfo(com.intellij.codeInsight.daemon.LineMarkerInfo) JBColor(com.intellij.ui.JBColor) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 18 with HighlighterIterator

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

the class HTMLTextPainter method paint.

@SuppressWarnings({ "HardCodedStringLiteral" })
public void paint(TreeMap refMap, FileType fileType) throws FileNotFoundException {
    HighlighterIterator hIterator = myHighlighter.createIterator(myOffset);
    if (hIterator.atEnd())
        return;
    OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(myHTMLFileName), CharsetToolkit.UTF8_CHARSET);
    lineCount = myFirstLineNumber;
    TextAttributes prevAttributes = null;
    Iterator refKeys = null;
    int refOffset = -1;
    PsiReference ref = null;
    if (refMap != null) {
        refKeys = refMap.keySet().iterator();
        if (refKeys.hasNext()) {
            Integer key = (Integer) refKeys.next();
            ref = (PsiReference) refMap.get(key);
            refOffset = key.intValue();
        }
    }
    int referenceEnd = -1;
    try {
        writeHeader(writer, new File(myFileName).getName());
        if (myFirstLineNumber == 0) {
            writeLineNumber(writer);
        }
        String closeTag = null;
        getMethodSeparator(hIterator.getStart());
        while (!hIterator.atEnd()) {
            TextAttributes textAttributes = hIterator.getTextAttributes();
            int hStart = hIterator.getStart();
            int hEnd = hIterator.getEnd();
            if (hEnd > mySegmentEnd)
                break;
            boolean haveNonWhiteSpace = false;
            for (int offset = hStart; offset < hEnd; offset++) {
                char c = myText.charAt(offset);
                if (c != ' ' && c != '\t') {
                    haveNonWhiteSpace = true;
                    break;
                }
            }
            if (!haveNonWhiteSpace) {
                // don't write separate spans for whitespace-only text fragments
                writeString(writer, myText, hStart, hEnd - hStart, fileType);
                hIterator.advance();
                continue;
            }
            if (refOffset > 0 && hStart <= refOffset && hEnd > refOffset) {
                referenceEnd = writeReferenceTag(writer, ref);
            }
            //        if(myForceFonts || !equals(prevAttributes, textAttributes)) {
            if (!equals(prevAttributes, textAttributes) && referenceEnd < 0) {
                if (closeTag != null) {
                    writer.write(closeTag);
                }
                closeTag = writeFontTag(writer, textAttributes);
                prevAttributes = textAttributes;
            }
            writeString(writer, myText, hStart, hEnd - hStart, fileType);
            //        }
            if (referenceEnd > 0 && hEnd >= referenceEnd) {
                writer.write("</a>");
                referenceEnd = -1;
                if (refKeys.hasNext()) {
                    Integer key = (Integer) refKeys.next();
                    ref = (PsiReference) refMap.get(key);
                    refOffset = key.intValue();
                }
            }
            hIterator.advance();
        }
        if (closeTag != null) {
            writer.write(closeTag);
        }
        writeFooter(writer);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
    }
}
Also used : PsiReference(com.intellij.psi.PsiReference) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiFile(com.intellij.psi.PsiFile) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 19 with HighlighterIterator

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

the class EnterAfterUnmatchedBraceHandler method getUnmatchedLBracesNumberBefore.

/**
   * Calculates number of unmatched left braces before the given offset.
   *
   * @param editor   target editor
   * @param offset   target offset
   * @param fileType target file type
   * @return number of unmatched braces before the given offset;
   * negative value if it's not possible to perform the calculation or if there are no unmatched left braces before
   * the given offset
   */
protected static int getUnmatchedLBracesNumberBefore(Editor editor, int offset, FileType fileType) {
    if (offset == 0) {
        return -1;
    }
    CharSequence chars = editor.getDocument().getCharsSequence();
    if (chars.charAt(offset - 1) != '{') {
        return -1;
    }
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset - 1);
    BraceMatcher braceMatcher = BraceMatchingUtil.getBraceMatcher(fileType, iterator);
    if (!braceMatcher.isLBraceToken(iterator, chars, fileType) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
        return -1;
    }
    Language language = iterator.getTokenType().getLanguage();
    iterator = highlighter.createIterator(0);
    int lBracesBeforeOffset = 0;
    int lBracesAfterOffset = 0;
    int rBracesBeforeOffset = 0;
    int rBracesAfterOffset = 0;
    for (; !iterator.atEnd(); iterator.advance()) {
        IElementType tokenType = iterator.getTokenType();
        if (!tokenType.getLanguage().equals(language) || !braceMatcher.isStructuralBrace(iterator, chars, fileType)) {
            continue;
        }
        boolean beforeOffset = iterator.getStart() < offset;
        if (braceMatcher.isLBraceToken(iterator, chars, fileType)) {
            if (beforeOffset) {
                lBracesBeforeOffset++;
            } else {
                lBracesAfterOffset++;
            }
        } else if (braceMatcher.isRBraceToken(iterator, chars, fileType)) {
            if (beforeOffset) {
                rBracesBeforeOffset++;
            } else {
                rBracesAfterOffset++;
            }
        }
    }
    return lBracesBeforeOffset - rBracesBeforeOffset - (rBracesAfterOffset - lBracesAfterOffset);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) EditorEx(com.intellij.openapi.editor.ex.EditorEx) BraceMatcher(com.intellij.codeInsight.highlighting.BraceMatcher) Language(com.intellij.lang.Language) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 20 with HighlighterIterator

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

the class EnterInLineCommentHandler method isInLineComment.

private static boolean isInLineComment(@NotNull Editor editor, int offset, IElementType lineCommentType) {
    if (offset < 1)
        return false;
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset - 1);
    return iterator.getTokenType() == lineCommentType;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) 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