Search in sources :

Example 11 with TextAttributes

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

the class PropertiesAnnotator method highlightTokens.

private static void highlightTokens(final Property property, final ASTNode node, final AnnotationHolder holder, PropertiesHighlighter highlighter) {
    Lexer lexer = highlighter.getHighlightingLexer();
    final String s = node.getText();
    lexer.start(s);
    while (lexer.getTokenType() != null) {
        IElementType elementType = lexer.getTokenType();
        TextAttributesKey[] keys = highlighter.getTokenHighlights(elementType);
        for (TextAttributesKey key : keys) {
            Pair<String, HighlightSeverity> pair = PropertiesHighlighter.DISPLAY_NAMES.get(key);
            String displayName = pair.getFirst();
            HighlightSeverity severity = pair.getSecond();
            if (severity != null) {
                int start = lexer.getTokenStart() + node.getTextRange().getStartOffset();
                int end = lexer.getTokenEnd() + node.getTextRange().getStartOffset();
                TextRange textRange = new TextRange(start, end);
                final Annotation annotation;
                if (severity == HighlightSeverity.WARNING) {
                    annotation = holder.createWarningAnnotation(textRange, displayName);
                } else if (severity == HighlightSeverity.ERROR) {
                    annotation = holder.createErrorAnnotation(textRange, displayName);
                } else {
                    annotation = holder.createInfoAnnotation(textRange, displayName);
                }
                TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key);
                annotation.setEnforcedTextAttributes(attributes);
                if (key == PropertiesHighlighter.PROPERTIES_INVALID_STRING_ESCAPE) {
                    annotation.registerFix(new IntentionAction() {

                        @NotNull
                        public String getText() {
                            return PropertiesBundle.message("unescape");
                        }

                        @NotNull
                        public String getFamilyName() {
                            return getText();
                        }

                        public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
                            if (!property.isValid() || !property.getManager().isInProject(property))
                                return false;
                            String text = property.getPropertiesFile().getContainingFile().getText();
                            int startOffset = annotation.getStartOffset();
                            return text.length() > startOffset && text.charAt(startOffset) == '\\';
                        }

                        public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
                            int offset = annotation.getStartOffset();
                            if (property.getPropertiesFile().getContainingFile().getText().charAt(offset) == '\\') {
                                editor.getDocument().deleteString(offset, offset + 1);
                            }
                        }

                        public boolean startInWriteAction() {
                            return true;
                        }
                    });
                }
            }
        }
        lexer.advance();
    }
}
Also used : HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) TextRange(com.intellij.openapi.util.TextRange) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) NotNull(org.jetbrains.annotations.NotNull) Annotation(com.intellij.lang.annotation.Annotation) IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) Lexer(com.intellij.lexer.Lexer) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 12 with TextAttributes

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

the class XmlTagInplaceRenamer method addHighlights.

private static void addHighlights(List<TextRange> ranges, Editor editor, ArrayList<RangeHighlighter> highlighters) {
    EditorColorsManager colorsManager = EditorColorsManager.getInstance();
    final TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.WRITE_SEARCH_RESULT_ATTRIBUTES);
    final HighlightManager highlightManager = HighlightManager.getInstance(editor.getProject());
    for (final TextRange range : ranges) {
        highlightManager.addOccurrenceHighlight(editor, range.getStartOffset(), range.getEndOffset(), attributes, 0, highlighters, null);
    }
    for (RangeHighlighter highlighter : highlighters) {
        highlighter.setGreedyToLeft(true);
        highlighter.setGreedyToRight(true);
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) HighlightManager(com.intellij.codeInsight.highlighting.HighlightManager) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.intellij.openapi.util.TextRange) EditorColorsManager(com.intellij.openapi.editor.colors.EditorColorsManager)

Example 13 with TextAttributes

use of com.intellij.openapi.editor.markup.TextAttributes in project ideavim by JetBrains.

the class SearchGroup method highlightSearchLines.

private static void highlightSearchLines(@NotNull Editor editor, @NotNull String pattern, int startLine, int endLine, boolean ignoreCase) {
    final TextAttributes color = editor.getColorsScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    Collection<RangeHighlighter> highlighters = EditorData.getLastHighlights(editor);
    if (highlighters == null) {
        highlighters = new ArrayList<RangeHighlighter>();
        EditorData.setLastHighlights(editor, highlighters);
    }
    for (TextRange range : findAll(editor, pattern, startLine, endLine, ignoreCase)) {
        final RangeHighlighter highlighter = highlightMatch(editor, range.getStartOffset(), range.getEndOffset());
        highlighter.setErrorStripeMarkColor(color.getBackgroundColor());
        highlighter.setErrorStripeTooltip(pattern);
        highlighters.add(highlighter);
    }
}
Also used : RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 14 with TextAttributes

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

the class DebuggerTreeRenderer method getDescriptorText.

private static SimpleColoredText getDescriptorText(DebuggerContextImpl debuggerContext, NodeDescriptorImpl descriptor, EditorColorsScheme colorScheme, boolean multiline, boolean appendValue) {
    SimpleColoredText descriptorText = new SimpleColoredText();
    String text;
    String nodeName;
    if (descriptor == null) {
        text = "";
        nodeName = null;
    } else {
        text = descriptor.getLabel();
        nodeName = descriptor.getName();
    }
    if (text.equals(XDebuggerUIConstants.COLLECTING_DATA_MESSAGE)) {
        descriptorText.append(XDebuggerUIConstants.COLLECTING_DATA_MESSAGE, XDebuggerUIConstants.COLLECTING_DATA_HIGHLIGHT_ATTRIBUTES);
        return descriptorText;
    }
    if (descriptor instanceof ValueDescriptor) {
        final ValueMarkup markup = ((ValueDescriptor) descriptor).getMarkup(debuggerContext.getDebugProcess());
        if (markup != null) {
            descriptorText.append("[" + markup.getText() + "] ", new SimpleTextAttributes(SimpleTextAttributes.STYLE_BOLD, markup.getColor()));
        }
    }
    String[] strings = breakString(text, nodeName);
    if (strings[0] != null) {
        if (descriptor instanceof MessageDescriptor && ((MessageDescriptor) descriptor).getKind() == MessageDescriptor.SPECIAL) {
            descriptorText.append(strings[0], SPECIAL_NODE_ATTRIBUTES);
        } else {
            descriptorText.append(strings[0], DEFAULT_ATTRIBUTES);
        }
    }
    if (strings[1] != null) {
        descriptorText.append(strings[1], XDebuggerUIConstants.VALUE_NAME_ATTRIBUTES);
    }
    if (strings[2] != null) {
        if (descriptor instanceof ValueDescriptorImpl) {
            if (multiline && strings[2].indexOf('\n') >= 0) {
                strings = breakString(strings[2], "=");
                if (strings[2] != null) {
                    strings[2] = strings[0] + strings[1] + "\n" + strings[2];
                }
            }
            ValueDescriptorImpl valueDescriptor = (ValueDescriptorImpl) descriptor;
            String valueLabel = valueDescriptor.getValueLabel();
            strings = breakString(strings[2], valueLabel);
            if (strings[0] != null) {
                descriptorText.append(strings[0], DEFAULT_ATTRIBUTES);
            }
            if (appendValue && strings[1] != null) {
                if (valueLabel != null && StringUtil.startsWithChar(valueLabel, '{') && valueLabel.indexOf('}') > 0 && !StringUtil.endsWithChar(valueLabel, '}')) {
                    int idx = valueLabel.indexOf('}');
                    String objectId = valueLabel.substring(0, idx + 1);
                    valueLabel = valueLabel.substring(idx + 1);
                    descriptorText.append(objectId, OBJECT_ID_HIGHLIGHT_ATTRIBUTES);
                }
                valueLabel = DebuggerUtilsEx.truncateString(valueLabel);
                final SimpleTextAttributes valueLabelAttribs;
                if (valueDescriptor.isDirty()) {
                    valueLabelAttribs = XDebuggerUIConstants.CHANGED_VALUE_ATTRIBUTES;
                } else {
                    TextAttributes attributes = null;
                    if (valueDescriptor.isNull()) {
                        attributes = colorScheme.getAttributes(JavaHighlightingColors.KEYWORD);
                    } else if (valueDescriptor.isString()) {
                        attributes = colorScheme.getAttributes(JavaHighlightingColors.STRING);
                    }
                    valueLabelAttribs = attributes != null ? SimpleTextAttributes.fromTextAttributes(attributes) : DEFAULT_ATTRIBUTES;
                }
                final EvaluateException exception = descriptor.getEvaluateException();
                if (exception != null) {
                    final String errorMessage = exception.getMessage();
                    if (valueLabel.endsWith(errorMessage)) {
                        appendValueTextWithEscapesRendering(descriptorText, valueLabel.substring(0, valueLabel.length() - errorMessage.length()), valueLabelAttribs, colorScheme);
                        descriptorText.append(errorMessage, XDebuggerUIConstants.EXCEPTION_ATTRIBUTES);
                    } else {
                        appendValueTextWithEscapesRendering(descriptorText, valueLabel, valueLabelAttribs, colorScheme);
                        descriptorText.append(errorMessage, XDebuggerUIConstants.EXCEPTION_ATTRIBUTES);
                    }
                } else {
                    if (valueLabel.equals(XDebuggerUIConstants.COLLECTING_DATA_MESSAGE)) {
                        descriptorText.append(XDebuggerUIConstants.COLLECTING_DATA_MESSAGE, XDebuggerUIConstants.COLLECTING_DATA_HIGHLIGHT_ATTRIBUTES);
                    } else {
                        appendValueTextWithEscapesRendering(descriptorText, valueLabel, valueLabelAttribs, colorScheme);
                    }
                }
            }
        } else {
            descriptorText.append(strings[2], DEFAULT_ATTRIBUTES);
        }
    }
    return descriptorText;
}
Also used : EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) ValueDescriptor(com.intellij.debugger.ui.tree.ValueDescriptor) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) ValueMarkup(com.intellij.xdebugger.impl.ui.tree.ValueMarkup)

Example 15 with TextAttributes

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

the class SelectInEditorManagerImpl method doSelect.

private void doSelect(final boolean toUseNormalSelection, @NotNull final Editor editor, final boolean toSelectLine, final TextRange textRange) {
    int startOffset = textRange.getStartOffset();
    int endOffset = textRange.getEndOffset();
    if (toUseNormalSelection) {
        DocumentEx doc = (DocumentEx) editor.getDocument();
        if (toSelectLine) {
            int lineNumber = doc.getLineNumber(startOffset);
            if (lineNumber >= 0 && lineNumber < doc.getLineCount()) {
                editor.getSelectionModel().setSelection(doc.getLineStartOffset(lineNumber), doc.getLineEndOffset(lineNumber) + doc.getLineSeparatorLength(lineNumber));
            }
        } else {
            editor.getSelectionModel().setSelection(startOffset, endOffset);
        }
        return;
    }
    TextAttributes selectionAttributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    releaseAll();
    if (toSelectLine) {
        DocumentEx doc = (DocumentEx) editor.getDocument();
        int lineNumber = doc.getLineNumber(startOffset);
        if (lineNumber >= 0 && lineNumber < doc.getLineCount()) {
            mySegmentHighlighter = editor.getMarkupModel().addRangeHighlighter(doc.getLineStartOffset(lineNumber), doc.getLineEndOffset(lineNumber) + doc.getLineSeparatorLength(lineNumber), HighlighterLayer.LAST + 1, selectionAttributes, HighlighterTargetArea.EXACT_RANGE);
        }
    } else {
        mySegmentHighlighter = editor.getMarkupModel().addRangeHighlighter(startOffset, endOffset, HighlighterLayer.LAST + 1, selectionAttributes, HighlighterTargetArea.EXACT_RANGE);
    }
    myEditor = editor;
    myEditor.getContentComponent().addFocusListener(this);
    myEditor.getCaretModel().addCaretListener(this);
}
Also used : DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Aggregations

TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)215 EditorColorsManager (com.intellij.openapi.editor.colors.EditorColorsManager)40 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)33 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)31 NotNull (org.jetbrains.annotations.NotNull)29 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)28 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)26 TextRange (com.intellij.openapi.util.TextRange)23 SimpleTextAttributes (com.intellij.ui.SimpleTextAttributes)23 Nullable (org.jetbrains.annotations.Nullable)21 Project (com.intellij.openapi.project.Project)20 Editor (com.intellij.openapi.editor.Editor)19 ArrayList (java.util.ArrayList)19 PsiElement (com.intellij.psi.PsiElement)12 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)11 JBColor (com.intellij.ui.JBColor)11 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)10 Document (com.intellij.openapi.editor.Document)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 ContainerUtil (com.intellij.util.containers.ContainerUtil)9