calling Roslyn from VSIX command
Asked Answered
R

3

10

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?

Ricotta answered 4/8, 2015 at 11:59 Comment(0)
S
12

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);
}
Seeger answered 18/11, 2015 at 13:36 Comment(1)
The .FirstOrDefault(); would not give you the document in the active context (in case of multi-target projects or shared projects).Santana
D
8

Frank's answer works great. I found it hard to figure out what the type-names are, so here's Frank's code with fully qualified typenames:

using System.Linq;

var dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
var activeDocument = dte?.ActiveDocument;
if (activeDocument != null)
{
    var componentModel = (Microsoft.VisualStudio.ComponentModelHost.IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(Microsoft.VisualStudio.ComponentModelHost.SComponentModel));
    var workspace = (Microsoft.CodeAnalysis.Workspace)componentModel.GetService<Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace>();
    var documentId = workspace.CurrentSolution.GetDocumentIdsWithFilePath(activeDocument.FullName).FirstOrDefault();
    if (documentId != null)
    {
        var document = workspace.CurrentSolution.GetDocument(documentId);
    }
}

And here are the references to find those types:

I hope the two framework references can be replaced with NuGet references to VSSDK.DTE and VSSDK.ComponentModelHost, but when I tried, it gave build warnings about assembly version mismatches so I gave up.

Dolora answered 26/6, 2016 at 4:35 Comment(1)
The .FirstOrDefault(); would not give you the document in the active context (in case of multi-target projects or shared projects).Santana
P
1

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.

Poi answered 4/8, 2015 at 14:46 Comment(2)
AsText extension method looks very useful. Unfortunatelly I didn't found a way to get that ITextSnapshot yet. Any clue?Ricotta
found this linkRicotta

© 2022 - 2024 — McMap. All rights reserved.