private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var curlyBracketToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start); var curlyBracketLine = curlyBracketToken.GetLineSpan().StartLinePosition.Line; var curlyBracketReplacementToken = curlyBracketToken; var indentationOptions = IndentationOptions.FromDocument(document); var indentationSteps = DetermineIndentationSteps(indentationOptions, curlyBracketToken); var previousToken = curlyBracketToken.GetPreviousToken(); var nextToken = curlyBracketToken.GetNextToken(); var rewriter = new Rewriter(); if (IsAccessorWithSingleLineBlock(previousToken, curlyBracketToken)) { var newTrailingTrivia = previousToken.TrailingTrivia .WithoutTrailingWhitespace() .Add(SyntaxFactory.Space); rewriter.AddReplacement(previousToken, previousToken.WithTrailingTrivia(newTrailingTrivia)); curlyBracketReplacementToken = curlyBracketReplacementToken.WithLeadingTrivia(curlyBracketToken.LeadingTrivia.WithoutLeadingWhitespace()); } else { // Check if we need to apply a fix before the curly bracket if (previousToken.GetLineSpan().StartLinePosition.Line == curlyBracketLine) { var sharedTrivia = curlyBracketReplacementToken.LeadingTrivia.WithoutTrailingWhitespace(); var previousTokenNewTrailingTrivia = previousToken.TrailingTrivia .WithoutTrailingWhitespace() .AddRange(sharedTrivia) .Add(SyntaxFactory.CarriageReturnLineFeed); rewriter.AddReplacement(previousToken, previousToken.WithTrailingTrivia(previousTokenNewTrailingTrivia)); curlyBracketReplacementToken = curlyBracketReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationOptions, indentationSteps)); } // Check if we need to apply a fix after the curly bracket // if a closing curly bracket is followed by a semi-colon or closing paren, no fix is needed. if ((nextToken.GetLineSpan().StartLinePosition.Line == curlyBracketLine) && (!curlyBracketToken.IsKind(SyntaxKind.CloseBraceToken) || !IsValidFollowingToken(nextToken))) { var sharedTrivia = nextToken.LeadingTrivia.WithoutTrailingWhitespace(); var newTrailingTrivia = curlyBracketReplacementToken.TrailingTrivia .WithoutTrailingWhitespace() .AddRange(sharedTrivia) .Add(SyntaxFactory.CarriageReturnLineFeed); int newIndentationSteps; if (curlyBracketToken.IsKind(SyntaxKind.OpenBraceToken)) { newIndentationSteps = indentationSteps + 1; } else if (nextToken.IsKind(SyntaxKind.CloseBraceToken)) { newIndentationSteps = Math.Max(0, indentationSteps - 1); } else { newIndentationSteps = indentationSteps; } rewriter.AddReplacement(nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationOptions, newIndentationSteps))); curlyBracketReplacementToken = curlyBracketReplacementToken.WithTrailingTrivia(newTrailingTrivia); } } rewriter.AddReplacement(curlyBracketToken, curlyBracketReplacementToken); var newSyntaxRoot = rewriter.Visit(syntaxRoot).WithoutFormatting(); return document.WithSyntaxRoot(newSyntaxRoot); }
private static async Task <Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) { var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var curlyBracketToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start); var curlyBracketLine = curlyBracketToken.GetLocation().GetLineSpan().StartLinePosition.Line; var curlyBracketReplacementToken = curlyBracketToken; var indentationOptions = IndentationOptions.FromDocument(document); var indentationSteps = DetermineIndentationSteps(indentationOptions, curlyBracketToken); var previousToken = curlyBracketToken.GetPreviousToken(); var nextToken = curlyBracketToken.GetNextToken(); var rewriter = new Rewriter(); if (IsAccessorWithSingleLineBlock(previousToken, curlyBracketToken)) { var newTrailingTrivia = previousToken.TrailingTrivia .WithoutTrailingWhitespace() .Add(SyntaxFactory.Space); rewriter.AddReplacement(previousToken, previousToken.WithTrailingTrivia(newTrailingTrivia)); curlyBracketReplacementToken = curlyBracketReplacementToken.WithLeadingTrivia(curlyBracketToken.LeadingTrivia.WithoutLeadingWhitespace()); } else { // Check if we need to apply a fix before the curly bracket if (previousToken.GetLocation().GetLineSpan().StartLinePosition.Line == curlyBracketLine) { var sharedTrivia = curlyBracketReplacementToken.LeadingTrivia.WithoutTrailingWhitespace(); var previousTokenNewTrailingTrivia = previousToken.TrailingTrivia .WithoutTrailingWhitespace() .AddRange(sharedTrivia) .Add(SyntaxFactory.CarriageReturnLineFeed); rewriter.AddReplacement(previousToken, previousToken.WithTrailingTrivia(previousTokenNewTrailingTrivia)); curlyBracketReplacementToken = curlyBracketReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationOptions, indentationSteps)); } // Check if we need to apply a fix after the curly bracket // if a closing curly bracket is followed by a semi-colon or closing paren, no fix is needed. if ((nextToken.GetLocation().GetLineSpan().StartLinePosition.Line == curlyBracketLine) && (!curlyBracketToken.IsKind(SyntaxKind.CloseBraceToken) || !IsValidFollowingToken(nextToken))) { var sharedTrivia = nextToken.LeadingTrivia.WithoutTrailingWhitespace(); var newTrailingTrivia = curlyBracketReplacementToken.TrailingTrivia .WithoutTrailingWhitespace() .AddRange(sharedTrivia) .Add(SyntaxFactory.CarriageReturnLineFeed); int newIndentationSteps; if (curlyBracketToken.IsKind(SyntaxKind.OpenBraceToken)) { newIndentationSteps = indentationSteps + 1; } else if (nextToken.IsKind(SyntaxKind.CloseBraceToken)) { newIndentationSteps = Math.Max(0, indentationSteps - 1); } else { newIndentationSteps = indentationSteps; } rewriter.AddReplacement(nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationOptions, newIndentationSteps))); curlyBracketReplacementToken = curlyBracketReplacementToken.WithTrailingTrivia(newTrailingTrivia); } } rewriter.AddReplacement(curlyBracketToken, curlyBracketReplacementToken); var newSyntaxRoot = rewriter.Visit(syntaxRoot).WithoutFormatting(); return(document.WithSyntaxRoot(newSyntaxRoot)); }