Reputation: 271
I want to look into Roslyn SyntaxTrivia and view the pieces in VB code but I don't see anyway to pick apart the pieces of the various types. There are many types, just for C# I found the types below (not complete). Usually in Roslyn you can cast an object to a more specific type and then look at the language specific parts. I don't see how to do that with Trivia.
DocumentationCommentExteriorTrivia
EndOfLineTrivia
EndRegionDirectiveTrivia
MultiLineCommentTrivia
MultiLineDocumentationCommentTrivia
RegionDirectiveTrivia
SingleLineCommentTrivia
WhitespaceTrivia
Upvotes: 0
Views: 1231
Reputation: 1010
Most Trivia are just that - trivia without further information. However, some trivia contain SyntaxNodes which you can parse und process as usual with a small difference - you need to tell your methods that you want to find them. These are not SyntaxTrivias, but StructuredTriviaSyntax elements and you can find them as follows:
// C#
var structuredTriviaSyntax = root.DescendantNodes(
descendIntoTrivia: true)
.OfType<StructuredTriviaSyntax>();
// VB
Dim structuredTriviaSyntax = root.DescendantNodes(
descendIntoTrivia:=True)
.OfType(Of StructuredTriviaSyntax)
You can get the StructuredTrivia from a SyntaxTrivia Object by calling the GetStructure method of a SyntaxTrivia object like this:
// C#
var leadingTrivia = anySyntaxNode.GetLeadingTrivia();
foreach(var trivia in leadingTrivia){
var structure = trivia.GetStructure();
}
// VB
Dim leadingTrivia = anySyntaxNode.GetLeadingTrivia
For Each trivia In leadingTrivia
Dim structureNode = trivia.GetStructure
Next
This does not work for all types of trivia, as most trivia kinds do not have a SyntaxNode representation, but for instance DocumentationCommentTriviaSyntax as well as RegionDirectiveTriviaSyntax and EndRegionDirectiveTriviaSyntax are provided with more specific types which can be analyzed that way.
If you work with a SyntaxWalker or a SyntaxRewriter you need to pass "true" as a parameter in the constructor to search through trivia.
A DocumentationCommentTriviaSyntax for instance can then be analyzed by taking a look at the DescendandNodes which are various kinds of Xml[...]Syntax elements.
Upvotes: 3