public static Task <Document> RefactorAsync( Document document, ForStatementSyntax forStatement, CancellationToken cancellationToken) { ForStatementSyntax newForStatement = forStatement; if (forStatement .DescendantTrivia(TextSpan.FromBounds(forStatement.FirstSemicolonToken.Span.End, forStatement.SecondSemicolonToken.Span.Start)) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { newForStatement = forStatement.Update( forStatement.ForKeyword, forStatement.OpenParenToken, forStatement.Declaration, forStatement.Initializers, forStatement.FirstSemicolonToken.WithTrailingTrivia(SyntaxFactory.Space), default(ExpressionSyntax), forStatement.SecondSemicolonToken.WithoutLeadingTrivia(), forStatement.Incrementors, forStatement.CloseParenToken, forStatement.Statement); } else { newForStatement = forStatement.RemoveNode(forStatement.Condition, SyntaxRemoveOptions.KeepExteriorTrivia); } return(document.ReplaceNodeAsync(forStatement, newForStatement, cancellationToken)); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); ForStatementSyntax forStatement = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <ForStatementSyntax>(); if (forStatement == null) { return; } TextSpan span = TextSpan.FromBounds( forStatement.OpenParenToken.Span.End, forStatement.CloseParenToken.Span.Start); if (forStatement .DescendantTrivia(span) .All(f => f.IsWhitespaceOrEndOfLineTrivia())) { CodeAction codeAction = CodeAction.Create( "Use while statement to create an infinite loop", cancellationToken => { return(ConvertForStatementToWhileStatementAsync( context.Document, forStatement, cancellationToken)); }, DiagnosticIdentifiers.AvoidUsageOfForStatementToCreateInfiniteLoop); context.RegisterCodeFix(codeAction, context.Diagnostics); } }
public static Task <Document> RefactorAsync( Document document, ForStatementSyntax forStatement, CancellationToken cancellationToken) { LiteralExpressionSyntax trueLiteral = TrueLiteralExpression(); TextSpan span = TextSpan.FromBounds( forStatement.OpenParenToken.FullSpan.End, forStatement.CloseParenToken.FullSpan.Start); IEnumerable <SyntaxTrivia> trivia = forStatement.DescendantTrivia(span); if (!trivia.All(f => f.IsWhitespaceOrEndOfLineTrivia())) { trueLiteral = trueLiteral.WithTrailingTrivia(trivia); } WhileStatementSyntax whileStatement = WhileStatement( WhileKeyword().WithTriviaFrom(forStatement.ForKeyword), forStatement.OpenParenToken, trueLiteral, forStatement.CloseParenToken, forStatement.Statement); whileStatement = whileStatement .WithTriviaFrom(forStatement) .WithFormatterAnnotation(); return(document.ReplaceNodeAsync(forStatement, whileStatement, cancellationToken)); }
public static bool HasParenthesesOnSameLine(this ForStatementSyntax forStatement) { if (forStatement == null) { throw new ArgumentNullException(nameof(forStatement)); } TextSpan textSpan = TextSpan.FromBounds( forStatement.OpenParenToken.Span.Start, forStatement.CloseParenToken.Span.End); return(!forStatement .DescendantTrivia(textSpan) .ContainsEndOfLine()); }