Search code examples
c#visual-studiovisual-studio-extensions

How to populate the 'Code' field of the VS 2022 Error List when writing a VS Extension?


I'm writing an extension for Visual Studio 2022 using the Community.VisualStudio.Toolkit package and part of my extension writes errors to the Error List window. I've figured out how to populate all the other fields I want (Severity, Description, Project, and Line) but I can't quite figure out how to write to the Code field.

I created an ErrorListProvider object from the Microsoft.VisualStudio.Shell package that I then add error messages to like so:

errorListProvider.Tasks.Add(new ErrorTask
{
    ErrorCategory = TaskErrorCategory.Warning,
    Text = error.Message,
    Document = error.FileName,
    Line = error.Line - 1, // error list uses 0 based indexing 
    Column = error.Column,
    HierarchyItem = hierarchy, // populates the 'Project' field in the error list
});

The only other fields that I can modify on the ErrorTask item are HelpKeyword and Category, which don't seem to affect the Code field.


Solution

  • I accomplished this by making a class that implements ITableEntry and has a member variable errorCode

    public bool TryGetValue(string keyName, out object content)
    {
        switch (keyName)
        {
            case StandardTableKeyNames.ErrorCode:
                content = errorCode;
                return true;
            default:
                content = "";
                return false;
        }
    }
    
    public bool CanSetValue(string keyName)
    {
        switch (keyName)
        {
            case StandardTableKeyNames.ErrorCode:
                return true;
            default:
                return false;
        }
    }
    
    list.Add(new MyErrorListEntry()
    {
        errorCode = error.Code
    });