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?
.FirstOrDefault();
would not give you the document in the active context (in case of multi-target projects or shared projects). – Santana