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) });
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?
- 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):
- http://www.mztools.com/articles/2008/MZ2008022.aspx
- http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/81c2959c-a21a-4baa-88b2-757ce0769532/
- http://social.msdn.microsoft.com/forums/en-US/vsx/thread/a1d37fdf-09e0-41f9-a045-52a8109b8943
- http://social.msdn.microsoft.com/Forums/en/vsx/thread/806682c2-6c2d-42ac-9efd-696eb66080c2
- http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/b1c37c03-f92b-46b1-83f5-7d54a1bbf5e6/
solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vsixPackage = O2_FluentSharp_VSIXPackage.vsixPackage; // this is a reference to an Package object | |
var ivsSolution = (IVsSolution)Package.GetGlobalService(typeof(IVsSolution)); | |
var dte = (EnvDTE80.DTE2)Package.GetGlobalService(typeof(EnvDTE.DTE)); | |
var errorListProvider = new ErrorListProvider(vsixPackage); | |
var errorText = "this is a test item"; | |
var errorCategory = TaskErrorCategory.Error; | |
//Get first project details | |
var proj = dte.Solution.Projects.Item(1); | |
var projectUniqueName = proj.FileName; | |
var firstFileInProject = proj.ProjectItems.Item(1).FileNames[0]; | |
//Get first project IVsHierarchy item (needed to link the task with a project) | |
IVsHierarchy hierarchyItem; | |
ivsSolution.GetProjectOfUniqueName(projectUniqueName, out hierarchyItem); | |
var newError = new ErrorTask() | |
{ | |
ErrorCategory = errorCategory, | |
Category = TaskCategory.BuildCompile, | |
Text = errorText, | |
Document = firstFileInProject, | |
Line = 2, | |
Column = 6, | |
HierarchyItem = hierarchyItem | |
}; | |
newError.Navigate += (sender,e)=> | |
{ | |
//there are two Bugs in the errorListProvider.Navigate method: | |
// Line number needs adjusting | |
// Column is not shown | |
newError.Line++; | |
errorListProvider.Navigate(newError, EnvDTE.Constants.vsViewKindCode.guid()); | |
newError.Line--; | |
}; | |
errorListProvider.Tasks.Clear(); // clear previously created | |
errorListProvider.Tasks.Add(newError); // add item | |
errorListProvider.Show(); // make sure it is visible | |
return errorListProvider; | |
//using Microsoft.VisualStudio.Shell | |
//using Microsoft.VisualStudio.Shell.Interop | |
//using O2.FluentSharp.VSIX; | |
//O2Ref:O2_FluentSharp_VSIX.dll | |
//O2Ref:EnvDTE.dll | |
//O2Ref:EnvDTE80.dll | |
//O2Ref:Microsoft.VisualStudio.Shell.10.0.dll | |
//O2Ref:Microsoft.VisualStudio.Shell.Interop.dll | |
//O2Ref:Microsoft.VisualStudio.OLE.Interop.dll | |
//O2Ref:Microsoft.VisualStudio.Shell.Interop.8.0.dll | |
//O2Ref:Microsoft.VisualStudio.Shell.Interop.9.0.dll | |
//O2Ref:Microsoft.VisualStudio.Shell.Interop.10.0.dll | |
//generateDebugSymbols |