calling Roslyn from VSIX command

install roslyn visual studio 2017
install roslyn visual studio 2019
roslyn analyzer
roslyn analyzers
microsoft code analysis 2019
roslyn analyzer tutorial

What is best way to obtain Roslyn's SyntaxTree from EnvDTE.ProjectItem? I found some method for the other way (Roslyn's Document into ProjectItem).

I got VSIX command called from opened document and I'd like to experiment with Roslyn's syntax tree there.

This code works, but looks awkward to me:

    var pi = GetProjectItem();
    var piName = pi.get_FileNames(1);

    var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel));
    var workspace = componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>();
    var ids = workspace.GetOpenDocumentIds();
    var id1 = ids.First(id => workspace.GetFilePath(id) == piName);

        Microsoft.CodeAnalysis.Solution sln = workspace.CurrentSolution;
        var doc = sln.GetDocument(id1);
        //var w = await doc.GetSyntaxTreeAsync();
        Microsoft.CodeAnalysis.SyntaxTree syntaxTree;
        if (doc.TryGetSyntaxTree(out syntaxTree))

Is there better way to get Roslyn's Document from active document?

You can use workspace.CurrentSolution.GetDocumentIdsWithFilePath() to get the DocumentId(s) matching a file path. From that you can get the document itself using workspace.CurrentSolution.GetDocument()

private Document GetActiveDocument()
    var dte = Package.GetGlobalService(typeof(DTE)) as DTE;
    var activeDocument = dte?.ActiveDocument;
    if (activeDocument == null) return null;

    var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
    var workspace = (Workspace) componentModel.GetService<VisualStudioWorkspace>();

    var documentid = workspace.CurrentSolution.GetDocumentIdsWithFilePath(activeDocument.FullName).FirstOrDefault();
    if (documentid == null) return null;

    return workspace.CurrentSolution.GetDocument(documentid);

Code analysis using Roslyn analyzers, NET Compiler Platform ("Roslyn") code analyzers inspect your C# or Visual Basic code You can configure the scope of live code analysis to execute for the current document NuGet package versus VSIX extension To have rules enforced at build time, including through the command line or as part of� You can find the VSIX project template in the New Project dialog by searching for "vsix". When the project opens, add a custom command item template named FirstCommand . In the Solution Explorer , right-click the project node and select Add > New Item .

Install Roslyn analyzers, NET Compiler Platform (Roslyn) analyzers. The page for each analyzer package shows you the command to paste into the and then close all instances of Visual Studio to launch the VSIX Installer. Expand Online in the left pane, and then select Visual Studio Marketplace.. In the search box, type the name of the analyzer extension you want to install. For example, you may want to install the Microsoft FxCop analyzers to check your code for security and performance issues, among others.

If you can figure out how to get from a ProjectItem to an editor ITextSnapshot, then it would be better to use snapshot.AsText().GetOpenDocumentInCurrentContextWithChanges().

Note also that in your code above, by using TryGetSyntaxTree you are relying on someone else having requested the parse tree before you.

How to create a Roslyn Analyzer project for C# — roslyn-analyzers , This is a step by step guide on how to create a C# Roslyn Analyzer project for will be able to see a template called Analyzer with Code Fix (Nuget + VSIX). Under Start options the Command line arguments should be set to /rootsuffix Exp � I really don't want to debug Roslyn, I want to debug my VSIX that calls into Roslyn DLL's. I have been getting less than helpful errors mostly in the forms of ex{"OperatorToken"} where OperatorToken changes. I understand the usability issue is being addressed in 1.1 but I still want to be able to source step into Roslyn like I can into .Net.

How to debug an analyzer — roslyn-analyzers-docs 1.0.0 , vsixmanifest and change the version number on the top right corner. Launch a different experimental instance of Visual Studio: In the Command line arguments � Normally a .VSIX file is installed just double-clicking it, so that the VSIXInstaller.exe application is launched. But if you want to install an extension programatically, the VSIXInstaller.exe application offers a few command-line switches:

visual-studio-2015 - 从VSIX命令调用Roslyn, calling Roslyn from VSIX command. What is best way to obtain Roslyn's SyntaxTree from EnvDTE.ProjectItem? I found some method for the other way ( Roslyn's� Optional. A string containing the same arguments you would supply if you were invoking the command from the Command window. If a string is supplied, it is passed to the command line as the command's first argument and is parsed to form the various arguments for the command. This is similar to how commands are invoked in the Command window.

Publishing NuGet package and VSIX for an analyzer project , Analyzers installed via Analyzer VSIX packages execute only during live code solution in Visual Studio by executing the Build | Build Solution command. VS2017 - AD1000 An instance of analyzer Microsoft.NetCore.Analyzers.Runtime.TestForNaNCorrectlyAnalyzer cannot be created from Microsoft.NetCore.Analyzers.dll dotnet/roslyn-analyzers#1459 Closed heejaechang mentioned this issue Dec 13, 2017

  • AsText extension method looks very useful. Unfortunatelly I didn't found a way to get that ITextSnapshot yet. Any clue?
  • found this link