1

Coming from this question, I've managed to get all CompletionItem instances available for a specific offset using completionService.GetCompletionsAsync(document, offset);.

So, after querying for completions of "MyString".Len, I get a CompletionItem for the Length method and can then, using the CompletionService, call service.GetDescriptionAsync(document, completionItem) to retrieve "int string.Length { get; }".

But, how can I get the comments for Length, e.g. "Gets the number of characters in the current String object."? And, if easily possible, other information regarding potential overloads?

mike
  • 1,627
  • 1
  • 14
  • 37

1 Answers1

0

Assuming that you're adding references to the assemblies using MetadataReference.CreateFromFile method, you should pass an DocumentationProvider instance as an additional parameter, like this:

  MetadataReference.CreateFromFile(path, MetadataReferenceProperties.Assembly, new MyDocumentationProvider(path));

DocumentationProvider is an abstract class, we ended up implementing our own by overriding GetDocumentationForSymbol method and locating appropriate XML node inside XML document.

Looking at Roslyn source code, there is XmlDocumentationProvider class which has an abstract method GetSourceStream (where you're supposed to pass a content of .xml file that stores documentation for .NET assemblies).

Please note that for this feature to work there should be an .xml file with descriptions file next to the assembly (which is normally produced from the source code when you have compile an assembly with Documentation File option set).

For .NET assemblies these files are included as part of SDK, and normally can be found at: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\vxxx

We use this approach in our Roslyn-based parsers in our AlterNET Studio product. You may find a bit more information about these parsers here.

GetDescriptionAsync can only return a number of overloads, to get list of overloads available at the same position you might need to use Recommender API like this:

var model = document.GetSemanticModelAsync().Result
var symbols = Microsoft.CodeAnalysis.Recommendations.Recommender.GetRecommendedSymbolsAtPositionAsync(model, pos, workspace).Result;

This API will return a separate symbol for every overload.

We asked a while ago whether it's possible to retrieve additional information (such as underlying symbol) from CompletionItem and the short answer is no. You may refer to the discussion here:

https://github.com/dotnet/roslyn/issues/57677

Dmitry
  • 61
  • 3
  • thanks for your response (and sorry for the late feedback). i understand the roslyn team's reasoning, but apparently this takes more steps than i had hoped :o/ – mike Feb 27 '22 at 19:58
  • Looks like we no longer have to implement our own DocumentationProvider. Instead, XmlDocumentationProvider has a few static methods, [CreateFromFile](https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.xmldocumentationprovider.createfromfile?view=roslyn-dotnet-4.2.0) and [CreateFromBytes](https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.xmldocumentationprovider.createfrombytes?view=roslyn-dotnet-4.2.0). – Dan Jun 10 '22 at 12:39
  • Indeed, XmlDocumentationProvider.CreateFromFile does the job. Looks like it was missing in early versions of Roslyn. – Dmitry Jun 28 '22 at 04:48