Search in sources :

Example 16 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class InspectionValidatorWrapper method runXmlFileSchemaValidation.

private Map<ProblemDescriptor, HighlightDisplayLevel> runXmlFileSchemaValidation(@NotNull XmlFile xmlFile) {
    final AnnotationHolderImpl holder = new AnnotationHolderImpl(new AnnotationSession(xmlFile));
    final List<ExternalAnnotator> annotators = ExternalLanguageAnnotators.allForFile(StdLanguages.XML, xmlFile);
    for (ExternalAnnotator<?, ?> annotator : annotators) {
        processAnnotator(xmlFile, holder, annotator);
    }
    if (!holder.hasAnnotations())
        return Collections.emptyMap();
    Map<ProblemDescriptor, HighlightDisplayLevel> problemsMap = new LinkedHashMap<>();
    for (final Annotation annotation : holder) {
        final HighlightInfo info = HighlightInfo.fromAnnotation(annotation);
        if (info.getSeverity() == HighlightSeverity.INFORMATION)
            continue;
        final PsiElement startElement = xmlFile.findElementAt(info.startOffset);
        final PsiElement endElement = info.startOffset == info.endOffset ? startElement : xmlFile.findElementAt(info.endOffset - 1);
        if (startElement == null || endElement == null)
            continue;
        final ProblemDescriptor descriptor = myInspectionManager.createProblemDescriptor(startElement, endElement, info.getDescription(), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false);
        final HighlightDisplayLevel level = info.getSeverity() == HighlightSeverity.ERROR ? HighlightDisplayLevel.ERROR : HighlightDisplayLevel.WARNING;
        problemsMap.put(descriptor, level);
    }
    return problemsMap;
}
Also used : AnnotationSession(com.intellij.lang.annotation.AnnotationSession) ExternalAnnotator(com.intellij.lang.annotation.ExternalAnnotator) HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) AnnotationHolderImpl(com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Annotation(com.intellij.lang.annotation.Annotation) PsiElement(com.intellij.psi.PsiElement) LinkedHashMap(com.intellij.util.containers.hash.LinkedHashMap)

Example 17 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project kotlin by JetBrains.

the class TagsTestDataUtil method insertInfoTags.

public static String insertInfoTags(List<HighlightInfo> highlights, String text) {
    List<HighlightTagPoint> highlightPoints = Lists.newArrayList();
    for (HighlightInfo highlight : highlights) {
        highlightPoints.add(new HighlightTagPoint(highlight.startOffset, true, highlight));
        highlightPoints.add(new HighlightTagPoint(highlight.endOffset, false, highlight));
    }
    return insertTagsInText(highlightPoints, text);
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 18 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project kotlin by JetBrains.

the class AbstractQuickFixMultiFileTest method doAction.

@SuppressWarnings({ "HardCodedStringLiteral" })
public void doAction(String text, boolean actionShouldBeAvailable, String testFilePath) throws Exception {
    Pattern pattern = text.startsWith("/") ? Pattern.compile(text.substring(1, text.length() - 1)) : Pattern.compile(StringUtil.escapeToRegexp(text));
    List<IntentionAction> availableActions = getAvailableActions();
    IntentionAction action = findActionByPattern(pattern, availableActions);
    if (action == null) {
        if (actionShouldBeAvailable) {
            List<String> texts = getActionsTexts(availableActions);
            Collection<HighlightInfo> infos = doHighlighting();
            fail("Action with text '" + text + "' is not available in test " + testFilePath + "\n" + "Available actions (" + texts.size() + "): \n" + StringUtil.join(texts, "\n") + "\nActions:\n" + StringUtil.join(availableActions, "\n") + "\nInfos:\n" + StringUtil.join(infos, "\n"));
        } else {
            DirectiveBasedActionUtils.INSTANCE.checkAvailableActionsAreExpected(getFile(), availableActions);
        }
    } else {
        if (!actionShouldBeAvailable) {
            fail("Action '" + text + "' is available (but must not) in test " + testFilePath);
        }
        ShowIntentionActionsHandler.chooseActionAndInvoke(getFile(), getEditor(), action, action.getText());
        UIUtil.dispatchAllInvocationEvents();
        //noinspection ConstantConditions
        if (!shouldBeAvailableAfterExecution()) {
            IntentionAction afterAction = findActionByPattern(pattern, getAvailableActions());
            if (afterAction != null) {
                fail("Action '" + text + "' is still available after its invocation in test " + testFilePath);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 19 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class JumpToColorsAndFontsAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    // todo handle ColorKey's as well
    Project project = e.getData(CommonDataKeys.PROJECT);
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    if (project == null || editor == null)
        return;
    Map<TextAttributesKey, Pair<ColorSettingsPage, AttributesDescriptor>> keyMap = ContainerUtil.newHashMap();
    Processor<RangeHighlighterEx> processor = r -> {
        Object tt = r.getErrorStripeTooltip();
        TextAttributesKey key = tt instanceof HighlightInfo ? ObjectUtils.chooseNotNull(((HighlightInfo) tt).forcedTextAttributesKey, ((HighlightInfo) tt).type.getAttributesKey()) : null;
        Pair<ColorSettingsPage, AttributesDescriptor> p = key == null ? null : ColorSettingsPages.getInstance().getAttributeDescriptor(key);
        if (p != null)
            keyMap.put(key, p);
        return true;
    };
    JBIterable<Editor> editors = editor instanceof EditorWindow ? JBIterable.of(editor, ((EditorWindow) editor).getDelegate()) : JBIterable.of(editor);
    for (Editor ed : editors) {
        TextRange selection = EditorUtil.getSelectionInAnyMode(ed);
        MarkupModel forDocument = DocumentMarkupModel.forDocument(ed.getDocument(), project, false);
        if (forDocument != null) {
            ((MarkupModelEx) forDocument).processRangeHighlightersOverlappingWith(selection.getStartOffset(), selection.getEndOffset(), processor);
        }
        ((MarkupModelEx) ed.getMarkupModel()).processRangeHighlightersOverlappingWith(selection.getStartOffset(), selection.getEndOffset(), processor);
        EditorHighlighter highlighter = ed instanceof EditorEx ? ((EditorEx) ed).getHighlighter() : null;
        SyntaxHighlighter syntaxHighlighter = highlighter instanceof LexerEditorHighlighter ? ((LexerEditorHighlighter) highlighter).getSyntaxHighlighter() : null;
        if (syntaxHighlighter != null) {
            HighlighterIterator iterator = highlighter.createIterator(selection.getStartOffset());
            while (!iterator.atEnd()) {
                for (TextAttributesKey key : syntaxHighlighter.getTokenHighlights(iterator.getTokenType())) {
                    Pair<ColorSettingsPage, AttributesDescriptor> p = key == null ? null : ColorSettingsPages.getInstance().getAttributeDescriptor(key);
                    if (p != null)
                        keyMap.put(key, p);
                }
                if (iterator.getEnd() >= selection.getEndOffset())
                    break;
                iterator.advance();
            }
        }
    }
    if (keyMap.isEmpty()) {
        HintManager.getInstance().showErrorHint(editor, "No text attributes found");
    } else if (keyMap.size() == 1) {
        Pair<ColorSettingsPage, AttributesDescriptor> p = keyMap.values().iterator().next();
        if (!openSettingsAndSelectKey(project, p.first, p.second)) {
            HintManager.getInstance().showErrorHint(editor, "No appropriate settings page found");
        }
    } else {
        ArrayList<Pair<ColorSettingsPage, AttributesDescriptor>> attrs = ContainerUtil.newArrayList(keyMap.values());
        Collections.sort(attrs, (o1, o2) -> StringUtil.naturalCompare(o1.first.getDisplayName() + o1.second.getDisplayName(), o2.first.getDisplayName() + o2.second.getDisplayName()));
        EditorColorsScheme colorsScheme = editor.getColorsScheme();
        JBList<Pair<ColorSettingsPage, AttributesDescriptor>> list = new JBList<>(attrs);
        list.setCellRenderer(new ColoredListCellRenderer<Pair<ColorSettingsPage, AttributesDescriptor>>() {

            @Override
            protected void customizeCellRenderer(@NotNull JList<? extends Pair<ColorSettingsPage, AttributesDescriptor>> list, Pair<ColorSettingsPage, AttributesDescriptor> value, int index, boolean selected, boolean hasFocus) {
                TextAttributes ta = colorsScheme.getAttributes(value.second.getKey());
                Color fg = ObjectUtils.chooseNotNull(ta.getForegroundColor(), colorsScheme.getDefaultForeground());
                Color bg = ObjectUtils.chooseNotNull(ta.getBackgroundColor(), colorsScheme.getDefaultBackground());
                SimpleTextAttributes sa = fromTextAttributes(ta);
                SimpleTextAttributes saOpaque = sa.derive(STYLE_OPAQUE | sa.getStyle(), fg, bg, null);
                SimpleTextAttributes saSelected = REGULAR_ATTRIBUTES.derive(sa.getStyle(), null, null, null);
                SimpleTextAttributes saCur = REGULAR_ATTRIBUTES;
                List<String> split = StringUtil.split(value.first.getDisplayName() + "//" + value.second.getDisplayName(), "//");
                for (int i = 0, len = split.size(); i < len; i++) {
                    boolean last = i == len - 1;
                    saCur = !last ? REGULAR_ATTRIBUTES : selected ? saSelected : saOpaque;
                    if (last)
                        append(" ", saCur);
                    append(split.get(i), saCur);
                    if (last)
                        append(" ", saCur);
                    else
                        append(" > ", GRAYED_ATTRIBUTES);
                }
                Color stripeColor = ta.getErrorStripeColor();
                boolean addStripe = stripeColor != null && stripeColor != saCur.getBgColor();
                boolean addBoxed = ta.getEffectType() == EffectType.BOXED && ta.getEffectColor() != null;
                if (addBoxed) {
                    append("▢" + (addStripe ? "" : " "), saCur.derive(-1, ta.getEffectColor(), null, null));
                }
                if (addStripe) {
                    append(" ", saCur.derive(STYLE_OPAQUE, null, stripeColor, null));
                }
            }
        });
        JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle(StringUtil.notNullize(e.getPresentation().getText())).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
            Pair<ColorSettingsPage, AttributesDescriptor> p = list.getSelectedValue();
            if (p != null && !openSettingsAndSelectKey(project, p.first, p.second)) {
                HintManager.getInstance().showErrorHint(editor, "No appropriate settings page found");
            }
        }).createPopup().showInBestPositionFor(editor);
    }
}
Also used : Settings(com.intellij.openapi.options.ex.Settings) JBIterable(com.intellij.util.containers.JBIterable) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) EditorUtil(com.intellij.openapi.editor.ex.util.EditorUtil) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) ContainerUtil(com.intellij.util.containers.ContainerUtil) ColorSettingsPage(com.intellij.openapi.options.colors.ColorSettingsPage) ArrayList(java.util.ArrayList) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) ShowSettingsUtilImpl(com.intellij.ide.actions.ShowSettingsUtilImpl) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) Map(java.util.Map) EffectType(com.intellij.openapi.editor.markup.EffectType) Project(com.intellij.openapi.project.Project) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CommonDataKeys(com.intellij.openapi.actionSystem.CommonDataKeys) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) JBList(com.intellij.ui.components.JBList) ColorSettingsPages(com.intellij.openapi.options.colors.ColorSettingsPages) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) StringUtil(com.intellij.openapi.util.text.StringUtil) ActionCallback(com.intellij.openapi.util.ActionCallback) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) AttributesDescriptor(com.intellij.openapi.options.colors.AttributesDescriptor) SettingsDialog(com.intellij.openapi.options.newEditor.SettingsDialog) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) EditorWindow(com.intellij.injected.editor.EditorWindow) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) java.awt(java.awt) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction) List(java.util.List) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) JBPopupFactory(com.intellij.openapi.ui.popup.JBPopupFactory) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) Processor(com.intellij.util.Processor) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Pair(com.intellij.openapi.util.Pair) ObjectUtils(com.intellij.util.ObjectUtils) HintManager(com.intellij.codeInsight.hint.HintManager) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) SearchableConfigurable(com.intellij.openapi.options.SearchableConfigurable) javax.swing(javax.swing) EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) ArrayList(java.util.ArrayList) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) SyntaxHighlighter(com.intellij.openapi.fileTypes.SyntaxHighlighter) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) NotNull(org.jetbrains.annotations.NotNull) RangeHighlighterEx(com.intellij.openapi.editor.ex.RangeHighlighterEx) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) EditorColorsScheme(com.intellij.openapi.editor.colors.EditorColorsScheme) ColorSettingsPage(com.intellij.openapi.options.colors.ColorSettingsPage) Pair(com.intellij.openapi.util.Pair) LexerEditorHighlighter(com.intellij.openapi.editor.ex.util.LexerEditorHighlighter) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter) AttributesDescriptor(com.intellij.openapi.options.colors.AttributesDescriptor) TextRange(com.intellij.openapi.util.TextRange) ColoredListCellRenderer(com.intellij.ui.ColoredListCellRenderer) EditorWindow(com.intellij.injected.editor.EditorWindow) Project(com.intellij.openapi.project.Project) MarkupModelEx(com.intellij.openapi.editor.ex.MarkupModelEx) JBList(com.intellij.ui.components.JBList) Editor(com.intellij.openapi.editor.Editor) DocumentMarkupModel(com.intellij.openapi.editor.impl.DocumentMarkupModel) MarkupModel(com.intellij.openapi.editor.markup.MarkupModel) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator)

Example 20 with HighlightInfo

use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.

the class OrderEntryTest method doTest.

private void doTest(String fileName) throws Exception {
    String testFullPath = BASE_PATH + fileName;
    VirtualFile root = ModuleRootManager.getInstance(myModule).getContentRoots()[0].getParent();
    VirtualFile virtualFile = root.findFileByRelativePath(fileName);
    configureByExistingFile(virtualFile);
    ActionHint actionHint = ActionHint.parse(getFile(), getFile().getText());
    Collection<HighlightInfo> infosBefore = highlightErrors();
    final IntentionAction action = findActionAndCheck(actionHint, infosBefore);
    if (action != null) {
        String text = action.getText();
        WriteCommandAction.runWriteCommandAction(null, () -> action.invoke(getProject(), getEditor(), getFile()));
        Collection<HighlightInfo> infosAfter = highlightErrors();
        final IntentionAction afterAction = findActionWithText(text);
        if (afterAction != null) {
            fail("Action '" + text + "' is still available after its invocation in test " + testFullPath);
        }
        assertEquals(infosBefore.size() - 1, infosAfter.size());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Aggregations

HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)221 Nullable (org.jetbrains.annotations.Nullable)51 TextRange (com.intellij.openapi.util.TextRange)33 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)30 VirtualFile (com.intellij.openapi.vfs.VirtualFile)28 NotNull (org.jetbrains.annotations.NotNull)17 HighlightInfoType (com.intellij.codeInsight.daemon.impl.HighlightInfoType)16 Document (com.intellij.openapi.editor.Document)12 ArrayList (java.util.ArrayList)11 PsiElement (com.intellij.psi.PsiElement)10 File (java.io.File)8 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)7 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)6 Pair (com.intellij.openapi.util.Pair)6 PsiFile (com.intellij.psi.PsiFile)6 Editor (com.intellij.openapi.editor.Editor)5 NonNls (org.jetbrains.annotations.NonNls)5 StringUtil (com.intellij.openapi.util.text.StringUtil)4 IElementType (com.intellij.psi.tree.IElementType)4 ContainerUtil (com.intellij.util.containers.ContainerUtil)4