Search in sources :

Example 1 with HighlightInfoHolder

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

the class PsiConcurrencyStressTest method readStep.

private void readStep(final Random random) {
    PsiClass aClass = getPsiClass();
    switch(random.nextInt(4)) {
        case 0:
            mark("v");
            aClass.getContainingFile().accept(new PsiRecursiveElementVisitor() {
            });
            break;
        case 1:
            mark("m");
            for (int offset = 0; offset < myFile.getTextLength(); offset++) {
                myFile.findElementAt(offset);
            }
            break;
        case 2:
            mark("h");
            aClass.accept(new PsiRecursiveElementVisitor() {

                @Override
                public void visitElement(final PsiElement element) {
                    super.visitElement(element);
                    final HighlightInfoHolder infoHolder = new HighlightInfoHolder(myFile);
                    for (HighlightVisitor visitor : Extensions.getExtensions(HighlightVisitor.EP_HIGHLIGHT_VISITOR, getProject())) {
                        // to avoid race for com.intellij.codeInsight.daemon.impl.DefaultHighlightVisitor.myAnnotationHolder
                        HighlightVisitor v = visitor.clone();
                        v.analyze(myFile, true, infoHolder, () -> v.visit(element));
                    }
                }
            });
            break;
        case 3:
            mark("u");
            for (PsiMethod method : aClass.getMethods()) {
                method.getName();
            }
            break;
    }
}
Also used : HighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder) HighlightVisitor(com.intellij.codeInsight.daemon.impl.HighlightVisitor)

Example 2 with HighlightInfoHolder

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

the class HighlightVisitorInternalInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    final PsiFile file = holder.getFile();
    if (InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) {
        return PsiElementVisitor.EMPTY_VISITOR;
    }
    final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(file);
    if (virtualFile == null || virtualFile.getFileType() != StdFileTypes.JAVA || CompilerConfiguration.getInstance(holder.getProject()).isExcludedFromCompilation(virtualFile)) {
        return PsiElementVisitor.EMPTY_VISITOR;
    }
    return new HighlightVisitorImpl(JavaPsiFacade.getInstance(holder.getProject()).getResolveHelper()) {

        {
            prepareToRunAsInspection(new HighlightInfoHolder(file) {

                @Override
                public boolean add(@Nullable HighlightInfo info) {
                    if (super.add(info)) {
                        if (info != null && info.getSeverity() == HighlightSeverity.ERROR) {
                            final int startOffset = info.getStartOffset();
                            final PsiElement element = file.findElementAt(startOffset);
                            if (element != null) {
                                holder.registerProblem(element, info.getDescription());
                            }
                        }
                        return true;
                    }
                    return false;
                }

                @Override
                public boolean hasErrorResults() {
                    //accept multiple errors per file
                    return false;
                }
            });
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HighlightVisitorImpl(com.intellij.codeInsight.daemon.impl.analysis.HighlightVisitorImpl) HighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with HighlightInfoHolder

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

the class GeneralHighlightingPass method collectHighlights.

private boolean collectHighlights(@NotNull final List<PsiElement> elements1, @NotNull final List<ProperTextRange> ranges1, @NotNull final List<PsiElement> elements2, @NotNull final List<ProperTextRange> ranges2, @NotNull final ProgressIndicator progress, @NotNull final HighlightVisitor[] visitors, @NotNull final List<HighlightInfo> insideResult, @NotNull final List<HighlightInfo> outsideResult, final boolean forceHighlightParents) {
    final Set<PsiElement> skipParentsSet = new THashSet<>();
    // TODO - add color scheme to holder
    final HighlightInfoHolder holder = createInfoHolder(getFile());
    // one percent precision is enough
    final int chunkSize = Math.max(1, (elements1.size() + elements2.size()) / 100);
    boolean success = analyzeByVisitors(visitors, holder, 0, () -> {
        Stack<TextRange> nestedRange = new Stack<>();
        Stack<List<HighlightInfo>> nestedInfos = new Stack<>();
        runVisitors(elements1, ranges1, chunkSize, progress, skipParentsSet, holder, insideResult, outsideResult, forceHighlightParents, visitors, nestedRange, nestedInfos);
        final TextRange priorityIntersection = myPriorityRange.intersection(myRestrictRange);
        if ((!elements1.isEmpty() || !insideResult.isEmpty()) && priorityIntersection != null) {
            // do not apply when there were no elements to highlight
            myHighlightInfoProcessor.highlightsInsideVisiblePartAreProduced(myHighlightingSession, insideResult, myPriorityRange, myRestrictRange, getId());
        }
        runVisitors(elements2, ranges2, chunkSize, progress, skipParentsSet, holder, insideResult, outsideResult, forceHighlightParents, visitors, nestedRange, nestedInfos);
    });
    List<HighlightInfo> postInfos = new ArrayList<>(holder.size());
    // there can be extra highlights generated in PostHighlightVisitor
    for (int j = 0; j < holder.size(); j++) {
        final HighlightInfo info = holder.get(j);
        assert info != null;
        postInfos.add(info);
    }
    myHighlightInfoProcessor.highlightsInsideVisiblePartAreProduced(myHighlightingSession, postInfos, getFile().getTextRange(), getFile().getTextRange(), POST_UPDATE_ALL);
    return success;
}
Also used : THashSet(gnu.trove.THashSet) Stack(com.intellij.util.containers.Stack) HighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder) CustomHighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.CustomHighlightInfoHolder) SmartList(com.intellij.util.SmartList)

Example 4 with HighlightInfoHolder

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

the class WolfTheProblemSolverImpl method orderVincentToCleanTheCar.

// returns true if car has been cleaned
private boolean orderVincentToCleanTheCar(@NotNull final VirtualFile file, @NotNull final ProgressIndicator progressIndicator) throws ProcessCanceledException {
    if (!isToBeHighlighted(file)) {
        clearProblems(file);
        // file is going to be red waved no more
        return true;
    }
    if (hasSyntaxErrors(file)) {
        // optimization: it's no use anyway to try clean the file with syntax errors, only changing the file itself can help
        return false;
    }
    if (myProject.isDisposed())
        return false;
    if (willBeHighlightedAnyway(file))
        return false;
    final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(file);
    if (psiFile == null)
        return false;
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    if (document == null)
        return false;
    final AtomicReference<HighlightInfo> error = new AtomicReference<>();
    final AtomicBoolean hasErrorElement = new AtomicBoolean();
    try {
        GeneralHighlightingPass pass = new GeneralHighlightingPass(myProject, psiFile, document, 0, document.getTextLength(), false, new ProperTextRange(0, document.getTextLength()), null, HighlightInfoProcessor.getEmpty()) {

            @Override
            protected HighlightInfoHolder createInfoHolder(@NotNull final PsiFile file) {
                return new HighlightInfoHolder(file) {

                    @Override
                    public boolean add(@Nullable HighlightInfo info) {
                        if (info != null && info.getSeverity() == HighlightSeverity.ERROR) {
                            error.set(info);
                            hasErrorElement.set(myHasErrorElement);
                            throw new ProcessCanceledException();
                        }
                        return super.add(info);
                    }
                };
            }
        };
        pass.collectInformation(progressIndicator);
    } catch (ProcessCanceledException e) {
        if (error.get() != null) {
            ProblemImpl problem = new ProblemImpl(file, error.get(), hasErrorElement.get());
            reportProblems(file, Collections.singleton(problem));
        }
        return false;
    }
    clearProblems(file);
    return true;
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder) Nullable(org.jetbrains.annotations.Nullable) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 5 with HighlightInfoHolder

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

the class InjectedGeneralHighlightingPass method addInjectedPsiHighlights.

private boolean addInjectedPsiHighlights(@NotNull PsiFile injectedPsi, TextAttributes injectedAttributes, @NotNull Collection<HighlightInfo> outInfos, @NotNull ProgressIndicator progress, @NotNull InjectedLanguageManager injectedLanguageManager) {
    DocumentWindow documentWindow = (DocumentWindow) PsiDocumentManager.getInstance(myProject).getCachedDocument(injectedPsi);
    if (documentWindow == null)
        return true;
    Place places = InjectedLanguageUtil.getShreds(injectedPsi);
    boolean addTooltips = places.size() < 100;
    for (PsiLanguageInjectionHost.Shred place : places) {
        PsiLanguageInjectionHost host = place.getHost();
        if (host == null)
            continue;
        TextRange textRange = place.getRangeInsideHost().shiftRight(host.getTextRange().getStartOffset());
        if (textRange.isEmpty())
            continue;
        HighlightInfo.Builder builder = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_BACKGROUND).range(textRange);
        if (injectedAttributes != null && InjectedLanguageUtil.isHighlightInjectionBackground(host)) {
            builder.textAttributes(injectedAttributes);
        }
        if (addTooltips) {
            String desc = injectedPsi.getLanguage().getDisplayName() + ": " + injectedPsi.getText();
            builder.unescapedToolTip(desc);
        }
        HighlightInfo info = builder.createUnconditionally();
        info.setFromInjection(true);
        outInfos.add(info);
    }
    HighlightInfoHolder holder = createInfoHolder(injectedPsi);
    runHighlightVisitorsForInjected(injectedPsi, holder, progress);
    for (int i = 0; i < holder.size(); i++) {
        HighlightInfo info = holder.get(i);
        final int startOffset = documentWindow.injectedToHost(info.startOffset);
        final TextRange fixedTextRange = getFixedTextRange(documentWindow, startOffset);
        addPatchedInfos(info, injectedPsi, documentWindow, injectedLanguageManager, fixedTextRange, outInfos);
    }
    int injectedStart = holder.size();
    highlightInjectedSyntax(injectedPsi, holder);
    for (int i = injectedStart; i < holder.size(); i++) {
        HighlightInfo info = holder.get(i);
        final int startOffset = info.startOffset;
        final TextRange fixedTextRange = getFixedTextRange(documentWindow, startOffset);
        if (fixedTextRange == null) {
            info.setFromInjection(true);
            outInfos.add(info);
        } else {
            HighlightInfo patched = new HighlightInfo(info.forcedTextAttributes, info.forcedTextAttributesKey, info.type, fixedTextRange.getStartOffset(), fixedTextRange.getEndOffset(), info.getDescription(), info.getToolTip(), info.type.getSeverity(null), info.isAfterEndOfLine(), null, false, 0, info.getProblemGroup(), info.getGutterIconRenderer());
            patched.setFromInjection(true);
            outInfos.add(patched);
        }
    }
    if (!isDumbMode()) {
        List<HighlightInfo> todos = new ArrayList<>();
        highlightTodos(injectedPsi, injectedPsi.getText(), 0, injectedPsi.getTextLength(), progress, myPriorityRange, todos, todos);
        for (HighlightInfo info : todos) {
            addPatchedInfos(info, injectedPsi, documentWindow, injectedLanguageManager, null, outInfos);
        }
    }
    advanceProgress(1);
    return true;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) HighlightInfoHolder(com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder) Place(com.intellij.psi.impl.source.tree.injected.Place)

Aggregations

HighlightInfoHolder (com.intellij.codeInsight.daemon.impl.analysis.HighlightInfoHolder)5 NotNull (org.jetbrains.annotations.NotNull)2 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 HighlightVisitor (com.intellij.codeInsight.daemon.impl.HighlightVisitor)1 CustomHighlightInfoHolder (com.intellij.codeInsight.daemon.impl.analysis.CustomHighlightInfoHolder)1 HighlightVisitorImpl (com.intellij.codeInsight.daemon.impl.analysis.HighlightVisitorImpl)1 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 Document (com.intellij.openapi.editor.Document)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 Place (com.intellij.psi.impl.source.tree.injected.Place)1 SmartList (com.intellij.util.SmartList)1 Stack (com.intellij.util.containers.Stack)1 THashSet (gnu.trove.THashSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nullable (org.jetbrains.annotations.Nullable)1