How to use CompileContext.addMessage() to show a error in the message toolbox?

Hi
I'm a newbie in developing Intellij plugins. Now I face a problem that I want to use the Message toolbox to show the Errors and Warnings in the Message toolbox (I'm now simply using ConsoleView)
So when I try to Use CompileContext compileContext=null; and complieContext.addMessage(), the program get into infinate loop.
How to use it correctly?
Any help will be appreciated. Thanks.

2 comments
Comment actions Permalink

My plugin does this today, but I will say that I'm likely going to replace the use of the Problems View with a custom tool window that serves the same purpose.  There are two reasons I'm planning to do that:

  1. The Problems View is currently quite tied to (minimally) Java and will prevent your plugin (or at least that aspect of it) from being used in lighter-weight IDEs such as WebStorm, PhpStorm, etc.
  2. I've found some limitations with the Problems View that have frustrated me.  If I'm adding a compiler message for a file (vs. a directory), I can remove it later with no problems.  However, there are times when I need to ascribe the problem to a directory because the error my plugin gets back from its backing system resolves at that level.  I can add a message at the directory level, but I can't seem to remove it.  I've debugged this a bit and evidently it sees that message as a container message which doesn't get explicitly removed.  It's very possible this is because I'm doing something wrong, but the first item above is what's primarily driving me to do this myself.  My users want to be able to install my plugin in other JetBrains IDEs, in particular WebStorm.


So having said that, here's what I do to add a message for a file, clear messages for a file, and clear the Problems View altogether:


 
import com.intellij.compiler.CompilerMessageImpl;
import com.intellij.compiler.ProblemsView;
import com.intellij.compiler.impl.OneProjectItemCompileScope;
import com.intellij.openapi.compiler.CompilerMessage;
import com.intellij.openapi.compiler.CompilerMessageCategory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

public final class ProblemsViewUtil
{
    private static final Key<Object> PROBLEMS_VIEW_SESSION_ID_KEY = Key.create("ProblemsViewSessionKey");
    private static final Key<Object> PROBLEMS_VIEW_FILES_KEY = Key.create("ProblemsViewFiles");

    private ProblemsViewUtil()
    {
        // Utility class
    
}

    @NotNull
    private static
UUID getProblemsViewSessionId(@NotNull Project project)
    {
        UUID problemsViewSessionId = (UUID) project.getUserData(PROBLEMS_VIEW_SESSION_ID_KEY);
        if (problemsViewSessionId == null)
        {
            problemsViewSessionId = UUID.randomUUID();
            project.putUserData(PROBLEMS_VIEW_SESSION_ID_KEY, problemsViewSessionId);
        }
        return problemsViewSessionId;
    }

    @NotNull
    private static
Set<String> getProblemsViewFiles(@NotNull Project project)
    {
        //noinspection unchecked
        
Set<String> problemsViewFiles = (Set<String>) project.getUserData(PROBLEMS_VIEW_FILES_KEY);
        if (problemsViewFiles == null)
        {
            problemsViewFiles = new HashSet<String>();
            project.putUserData(PROBLEMS_VIEW_FILES_KEY, problemsViewFiles);
        }
        return problemsViewFiles;
    }

    public void addToProblemsView(
        @NotNull Project project,
        @NotNull CompilerMessageCategory messageCategory,
        @NotNull String message,
        @NotNull VirtualFile virtualFile,
        int lineNumber,
        int columnNumber)
    {
        CompilerMessage compilerMessage = new CompilerMessageImpl(
            project,
            messageCategory,
            message,
            virtualFile,
            lineNumber,
            columnNumber,
            /*navigatable=*/null
        
);

        // You need to have stored off the existing session ID
        
UUID problemsViewSessionId = getProblemsViewSessionId(project);

        ProblemsView problemsView = ProblemsView.SERVICE.getInstance(project);
        problemsView.addMessage(compilerMessage, problemsViewSessionId);
        Set<String> problemsViewFiles = getProblemsViewFiles(project);
        problemsViewFiles.add(virtualFile.getUrl());
    }

    public static void removeFromProblemsView(@NotNull Project project, @NotNull VirtualFile virtualFile)
    {
        Set<String> problemsViewFiles = getProblemsViewFiles(project);
        if (problemsViewFiles.contains(virtualFile.getUrl()))
        {
            ProblemsView problemsView = ProblemsView.SERVICE.getInstance(project);
            problemsView.clearOldMessages(new OneProjectItemCompileScope(project, virtualFile), UUID.randomUUID());

            problemsViewFiles.remove(virtualFile.getUrl());
        }
    }

    public static void clearProblemsView(@NotNull Project project)
    {
        Set<String> problemsViewFiles = getProblemsViewFiles(project);
        if (!ContainerUtil.isEmpty(problemsViewFiles))
        {
            ProblemsView problemsView = ProblemsView.SERVICE.getInstance(project);
            problemsView.clearProgress();
            problemsView.clearOldMessages(null, UUID.randomUUID());

            problemsViewFiles.clear();
        }
    }
}


Hope it helps!

0
Comment actions Permalink

Thanks a lot!
The code solve my problem perfectly:)

0

Please sign in to leave a comment.