Roslyn Language Service - Can I get the semantic model for a document?

Topics: APIs
May 26 at 3:07 PM

I would like to develop a classifier on top of roslyn. Is it possible to get the semantic information from the language service?

Best Regards
May 27 at 6:52 PM
If you mean a Roslyn Document class, then you can just call the document.GetSemanticModelAsync() method.

If you mean a document as in a VS ITextBuffer, then you can get the document via:

var container = textBuffer.AsTextContainer();
Workspace.TryGetWorkspace(container, out workspace);
var docIds = workspace.GetRelatedDocumentIds(container); // VS is nuts, multiple docs can share the same buffer
var docId = workspace.GetDocumentIdInCurrentContext(container); // applies some host specific rule to choose which document
var doc = workspace.CurrentSolution.GetDocument(docId);

If you mean a document as in some VS solution explorer item id.
I don't think we've exposed a way to do that yet.
May 28 at 12:54 PM
Thanks. This works fine,