Friday, 27 July 2012

Adding an item to VisualStudio's Error List Window

I just spent a ridiculous amount of time trying to figure this out, so here is a solution for something that should be simple, I struggled to find a solution, but when I finally found the solution, it was not that bad.

Basically, what I'm trying to do is to add errors/items to the Error List in VisualStudio based on Code Analysis (either at solution or file level).

If you want to read the solution, it is here https://gist.github.com/3185313 (also included at the end of this post). This script was executed from an O2 REPL Script environment running inside VisualStudio.

The next text is from an email I wrote asking for help to a MS guru on VS Extensibility.

----start----

I was expecting this to be something like this (in pseudo C# code):

VisualStudio.Editor.OnTextChange(
      ()=>{ 
              var code = VisualStudio.Editor.GetCode();
              var errorListPane =  VisualStudio.ErrorPane;
              var issues = AnalyzeAndGetIssues(code);
              foreach(var issue in issues)
                   errorListPane.Add(issue.Text, issue.Span, issue.Severity)
          });

or even just simply:

VisualStudio.Editor.OnTextChange(
      (code)=>{ 
                  var issues = code.AnalyzeAndGetIssues();
                  VisualStudio.ErrorPane.add(issues)
              });

VisualStudio.Editor.OnCompile(
      (assemblies)=>{ 
                       var issues = assemblies.AnalyzeAndGetIssues();
                       VisualStudio.ErrorPane.add(issues)
                    });

Here is a thread about what I'm trying to do and with the same problems that I have: How to add parsing errors to ErrorList window?

Here is more information about what I have done already and what I can/can't do:

  • I have written an VS Add-in which gives me access to the DTE2 and AddIn objects inside a REPL environment on a running VisualStudio instance (think of it as a mix of Interactive Window + Roslyn REPL) 
  • I have a VSPackage/VSIX that gives me access to the Package object and MEF exports from a similar REPL environment. 
  • From the Addin I can create VisualStudio native dockable panels (with an WinForms or WPF control inside). Take a look at the video here http://diniscruz.blogspot.co.uk/2012/06/real-time-vulnerability-creation.html to see an Code Editor (from SharpDevelop) running natively in VisualStudio with a couple extra panels showing security vulnerabilities information (note that these controls are hosted natively in VisualStudio windows created using DTE's CreateToolWindow2 method) 
  • One of the problems I had was in gaining access to the live instance of the Error Panel, since in one of my tests I had the error: The service "Microsoft.VisualStudio.Shell.Interop.IVsTaskList" must be installed for this feature to work" 
  • I could try to access the actually ListView control via the WPF/WinForms GUI controls, but that would be a massive hack 
  • I was able to create a PoC that did exactly what I wanted using a variation of the Roslyn's CodeIssue example. This did exactly what I wanted since I had a callback from Roslyn that expected to receive a List of CodeIssue objects (which where then added to the VisualStudio Error List). I have looked at Roslyn code, but can't figure out its connection with the actually error list (there are too many layers of callbacks). 
  • One key objective that we have is to able to distribute our solution in one file installer. At the moment a VSIX seems like a perfect solution since it allows the easy distribution and install of VisualStudio plugins (vs the Addin model which requires the user to copy it to correct location)

----end----

Here are a number of  good resources on this topic (note: which helped find the solution):


solution