private bool TryRewriteWhitespace(CSharpCodeBlockSyntax codeBlock, out CSharpCodeBlockSyntax rewritten, out SyntaxNode whitespaceLiteral) { // Rewrite any whitespace represented as code at the start of a line preceding an expression block. // We want it to be rendered as Markup. rewritten = null; whitespaceLiteral = null; var children = codeBlock.ChildNodes(); if (children.Count < 2) { return(false); } if (children[0] is CSharpStatementLiteralSyntax literal && (children[1] is CSharpExplicitExpressionSyntax || children[1] is CSharpImplicitExpressionSyntax)) { var containsNonWhitespace = literal.DescendantNodes() .Where(n => n.IsToken) .Cast <SyntaxToken>() .Any(t => !string.IsNullOrWhiteSpace(t.Content)); if (!containsNonWhitespace) { // Literal node is all whitespace. Can rewrite. whitespaceLiteral = SyntaxFactory.MarkupTextLiteral(literal.LiteralTokens); rewritten = codeBlock.ReplaceNode(literal, newNode: null); return(true); } } return(false); }
public override void VisitCSharpCodeBlock(CSharpCodeBlockSyntax node) { if (node.Parent is CSharpStatementBodySyntax || node.Parent is CSharpExplicitExpressionBodySyntax || node.Parent is CSharpImplicitExpressionBodySyntax || node.Parent is RazorDirectiveBodySyntax || (_currentBlockKind == BlockKindInternal.Directive && node.Children.Count == 1 && node.Children[0] is CSharpStatementLiteralSyntax)) { base.VisitCSharpCodeBlock(node); return; } WriteBlock(node, BlockKindInternal.Statement, base.VisitCSharpCodeBlock); }
public override void VisitCSharpCodeBlock(CSharpCodeBlockSyntax node) { if (node.Parent is CSharpStatementBodySyntax || node.Parent is CSharpExplicitExpressionBodySyntax || node.Parent is CSharpImplicitExpressionBodySyntax || node.Parent is RazorDirectiveBodySyntax || (_currentBlockKind == FormattingBlockKind.Directive && node.Parent?.Parent is RazorDirectiveBodySyntax)) { // If we get here, it means we don't want this code block to be considered significant. // Without this, we would have double indentation in places where // CSharpCodeBlock is used as a wrapper block in the syntax tree. if (!(node.Parent is RazorDirectiveBodySyntax)) { _currentRazorIndentationLevel++; } var isInCodeBlockDirective = node.Parent?.Parent?.Parent is RazorDirectiveSyntax directive && directive.DirectiveDescriptor.Kind == DirectiveKind.CodeBlock; if (isInCodeBlockDirective) { // This means this is the code portion of an @code or @functions kind of block. _isInClassBody = true; } base.VisitCSharpCodeBlock(node); if (isInCodeBlockDirective) { // Finished visiting the code portion. We are no longer in it. _isInClassBody = false; } if (!(node.Parent is RazorDirectiveBodySyntax)) { _currentRazorIndentationLevel--; } return; } WriteBlock(node, FormattingBlockKind.Statement, base.VisitCSharpCodeBlock); }
public override void VisitCSharpCodeBlock(CSharpCodeBlockSyntax node) { if (node.Parent is CSharpStatementBodySyntax || node.Parent is CSharpExplicitExpressionBodySyntax || node.Parent is CSharpImplicitExpressionBodySyntax || node.Parent is RazorDirectiveBodySyntax || (_currentBlockKind == FormattingBlockKind.Directive && node.Children.Count == 1 && node.Children[0] is CSharpStatementLiteralSyntax)) { if (!(node.Parent is RazorDirectiveBodySyntax)) { _currentIndentationLevel++; } var isInCodeBlockDirective = node.Parent?.Parent?.Parent is RazorDirectiveSyntax directive && directive.DirectiveDescriptor.Kind == DirectiveKind.CodeBlock; if (isInCodeBlockDirective) { // This means this is the code portion of an @code or @functions kind of block. _isInClassBody = true; } base.VisitCSharpCodeBlock(node); if (isInCodeBlockDirective) { // Finished visiting the code portion. We are no longer in it. _isInClassBody = false; } if (!(node.Parent is RazorDirectiveBodySyntax)) { _currentIndentationLevel--; } return; } WriteBlock(node, FormattingBlockKind.Statement, base.VisitCSharpCodeBlock); }
public override void VisitCSharpCodeBlock(CSharpCodeBlockSyntax node) { if (node.Parent is CSharpStatementBodySyntax || node.Parent is CSharpExplicitExpressionBodySyntax || node.Parent is CSharpImplicitExpressionBodySyntax || node.Parent is RazorDirectiveBodySyntax || (_currentBlockKind == FormattingBlockKind.Directive && node.Children.Count == 1 && node.Children[0] is CSharpStatementLiteralSyntax)) { if (!(node.Parent is RazorDirectiveBodySyntax)) { _currentIndentationLevel++; } base.VisitCSharpCodeBlock(node); if (!(node.Parent is RazorDirectiveBodySyntax)) { _currentIndentationLevel--; } return; } WriteBlock(node, FormattingBlockKind.Statement, base.VisitCSharpCodeBlock); }