Last active
July 28, 2020 09:04
-
-
Save xavierdecoster/a1064240dcd356f1793a to your computer and use it in GitHub Desktop.
Roslyn - SyntaxRewriter to remove region directives
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CSharpRemoveRegionRewriter : SyntaxRewriter | |
{ | |
public CSharpRemoveRegionRewriter() | |
: base(true) | |
{ | |
} | |
public override SyntaxNode VisitRegionDirectiveTrivia(RegionDirectiveTriviaSyntax node) | |
{ | |
return Syntax.SkippedTokensTrivia(); | |
} | |
public override SyntaxNode VisitEndRegionDirectiveTrivia(EndRegionDirectiveTriviaSyntax node) | |
{ | |
return Syntax.SkippedTokensTrivia(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VbRemoveRegionRewriter : SyntaxRewriter | |
{ | |
public VbRemoveRegionRewriter() | |
: base(true) | |
{ | |
} | |
public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia) | |
{ | |
var updatedTrivia = base.VisitTrivia(trivia); | |
var directiveTrivia = trivia.GetStructure() as DirectiveTriviaSyntax; | |
if (directiveTrivia != null) | |
{ | |
if (directiveTrivia.Kind == SyntaxKind.DirectiveTrivia) | |
{ | |
if (directiveTrivia.Directive.Kind == SyntaxKind.RegionDirective || | |
directiveTrivia.Directive.Kind == SyntaxKind.EndRegionDirective) | |
{ | |
updatedTrivia = default(SyntaxTrivia); | |
} | |
} | |
} | |
return updatedTrivia; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ImaginaryDevelopment apparently, the
SyntaxFactory
was once calledSyntax
. The methodSkippedTokensTrivia
exists in theSyntaxFactory
class.