Skip to content

Obtaining the type under the cursor? #400

Answered by reduckted
digitaldias asked this question in Q&A
Discussion options

You must be logged in to vote

First, you'll need to install the Microsoft.VisualStudio.LanguageServices NuGet package.

Then you can get the document in Roslyn like this:

// Get the workspace.
Workspace workspace = await VS.GetMefServiceAsync<VisualStudioWorkspace>();

// Get the active Visual Studio document.
DocumentView? documentView = await VS.Documents.GetActiveDocumentViewAsync();

if (document is not null) {
    // Get the Roslyn document for that file.
    DocumentId? id = workspace.CurrentSolution.GetDocumentIdsWithFilePath(documentView.FilePath).FirstOrDefault();

    if (id is not null) {
        Document roslynDocument = workspace.CurrentSolution.GetDocument(id);
    }
}

If you know your way around Roslyn, …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@digitaldias
Comment options

Answer selected by digitaldias
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants