Skip to content

Instantly share code, notes, and snippets.

@xavierdecoster
Last active July 28, 2020 09:04
Show Gist options
  • Save xavierdecoster/a1064240dcd356f1793a to your computer and use it in GitHub Desktop.
Save xavierdecoster/a1064240dcd356f1793a to your computer and use it in GitHub Desktop.
Roslyn - SyntaxRewriter to remove region directives
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();
}
}
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;
}
}
@ImaginaryDevelopment
Copy link

there aren't any usings on these files, Can you tell me where Syntax.SkippedTokensTrivia is?

@fl3pp
Copy link

fl3pp commented Jul 28, 2020

@ImaginaryDevelopment apparently, the SyntaxFactory was once called Syntax. The method SkippedTokensTrivia exists in the SyntaxFactory class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment