Extending theTask List

 

The Visual Studio Task List lets users add custom programming tasks, view task comments that link to lines in the code, and create and view custom categories for task messages.

Tasks are handled through a service named SVsTaskList, which implements IVsTaskList and IVsTaskList2. To use basic Task List functionality, you must create a task provider by implementing IVsTaskProvider.

Register your task provider with the SVsTaskList service by calling RegisterTaskProvider, which returns a cookie value that must be used to uniquely identify the task provider in all subsequent transactions.

Every task provider implementation is responsible for maintaining an internal list of tasks. The task provider can call RefreshTasks on Task List to update the displayed list of tasks. When this occurs:

  1. The service calls back into the task provider by using the EnumTaskItems method.

  2. The task provider implementation of EnumTaskItems returns an IVsEnumTaskItems object.

  3. The IVsEnumTaskItems object iterates over a collection of IVsTaskItem objects.

  4. The SVsTaskList service then updates the Task List display.

Additional functionality is available. The IVsTaskProvider3 interface enables every task provider to supply its own custom set of columns.

The following code example shows an implementation of a task provider.

class TaskProvider : ITaskProvider, IVsSolutionEvents
{
    public TaskProvider(IServiceProvider provider)
    {
        _imageList.ImageSize = new Size(9, 16);
        _imageList.Images.AddStrip(Resources.priority);
        _imageList.TransparentColor = Color.FromArgb(0, 255, 0);
        _serviceProvider = provider;
        IVsTaskList taskList = _serviceProvider.GetService(typeof(SVsTaskList)) as IVsTaskList;
        int hr = taskList.RegisterTaskProvider(this, out _cookie);
        Debug.Assert(hr == VSConstants.S_OK, "RegisterTaskProvider did not return S_OK.");
        Debug.Assert(_cookie != 0, "RegisterTaskProvider did not return a nonzero cookie.");

        SetCommandHandlers();

        ListenForProjectUnload();
    }

    #region ITaskProvider Members

    public void AddResult(IScanResult result, string projectFile)
    {
        string fullPath = result.FilePath;
        if (!Path.IsPathRooted(fullPath))
        {
            fullPath = Utilities.AbsolutePathFromRelative(fullPath, Path.GetDirectoryName(projectFile));
        }

        if (result.Scanned)
        {
            foreach (IScanHit hit in result.Results)
            {
                if (hit.Warning != null && hit.Warning.Length > 0)
                {
                    // See if we've warned about this term before; 
                    // if so, don't warn again.
                    if (null == _termsWithDuplicateWarning.Find(
                        delegate(string item)
                        {
                           return String.Compare(item, hit.Term.Text, 
                             StringComparison.OrdinalIgnoreCase) == 0;
                        }))
                    {
                     _tasks.Add(new Task(hit.Term.Text, 
                      hit.Term.Severity, hit.Term.Class, hit.Warning, 
                      "", "", -1, -1, "", "", this, _serviceProvider));
                        _termsWithDuplicateWarning.Add(hit.Term.Text);
                    }
                }

                _tasks.Add(new Task(hit.Term.Text, hit.Term.Severity,
 hit.Term.Class, hit.Term.Comment, hit.Term.RecommendedTerm, fullPath,
 hit.Line, hit.Column, projectFile, hit.LineText, this,
 _serviceProvider));
            }
        }
        else
        {
            _tasks.Add(new Task("", 1, "", String.Format(CultureInfo.CurrentUICulture, 
Resources.FileNotScannedError, fullPath), "", fullPath, -1, -1, 
projectFile, "", this, _serviceProvider));
        }
        Refresh();
    }

    public void Clear()
    {
        _tasks.Clear();
        Refresh();
    }

    public void SetAsActiveProvider()
    {
        IVsTaskList2 taskList = _serviceProvider.GetService(typeof(SVsTaskList)) as IVsTaskList2;
        Guid ourGuid = _providerGuid;
        int hr = taskList.SetActiveProvider(ref ourGuid);
        Debug.Assert(hr == VSConstants.S_OK, 
          "SetActiveProvider did not return S_OK.");
    }

    public void ShowTaskList()
    {
        IVsUIShell shell = _serviceProvider.GetService(typeof(SVsUIShell)) as IVsUIShell;
        object dummy = null;
        Guid cmdSetGuid = VSConstants.GUID_VSStandardCommandSet97;
        int hr = shell.PostExecCommand(ref cmdSetGuid, (int)VSConstants.VSStd97CmdID.TaskListWindow, 0, ref dummy);
        Debug.Assert(hr == VSConstants.S_OK, "SetActiveProvider did not return S_OK.");
    }

    /// <summary>
    /// Returns an image index between 0 and 2 inclusive corresponding 
    /// to the specified severity.
    /// </summary>
    public static int GetImageIndexForSeverity(int severity)
    {
        return Math.Max(1, Math.Min(3, severity)) - 1;
    }

    public bool IsShowingIgnoredInstances
    {
        get { return _showingIgnoredInstances; }
    }

    #endregion

    #region IVsTaskProvider Members

    public int EnumTaskItems(out IVsEnumTaskItems ppenum)
    {
        ppenum = new TaskEnumerator(_tasks, IsShowingIgnoredInstances);
        return VSConstants.S_OK;
    }

    [DllImport("comctl32.dll")]
    static extern IntPtr ImageList_Duplicate(IntPtr original);

    public int ImageList(out IntPtr phImageList)
    {
        phImageList = ImageList_Duplicate(_imageList.Handle);
        return VSConstants.S_OK;
    }

    public int OnTaskListFinalRelease(IVsTaskList pTaskList)
    {
        if ((_cookie != 0) && (null != pTaskList))
        {
            int hr = pTaskList.UnregisterTaskProvider(_cookie);
            Debug.Assert(hr == VSConstants.S_OK, "UnregisterTaskProvider did not return S_OK.");
        }

        return VSConstants.S_OK;
    }

    public int ReRegistrationKey(out string pbstrKey)
    {
        pbstrKey = "";
        return VSConstants.E_NOTIMPL;
    }

    public int SubcategoryList(uint cbstr, string[] rgbstr, 
       out uint pcActual)
    {
        pcActual = 0;
        return VSConstants.E_NOTIMPL;
    }
}
Show: