public static Task <Document> RefactorAsync( Document document, MemberDeclarationSyntax declaration, TextSpan span, CancellationToken cancellationToken) { MemberDeclarationSyntax newDeclaration = declaration; ImmutableArray <string> comments; SyntaxTriviaList leadingTrivia = declaration.GetLeadingTrivia(); if (leadingTrivia.Span.Contains(span)) { comments = leadingTrivia .Where(f => span.Contains(f.Span) && f.Kind() == SyntaxKind.SingleLineCommentTrivia) .Select(f => _leadingSlashesRegex.Replace(f.ToString(), "")) .ToImmutableArray(); TextSpan spanToRemove = TextSpan.FromBounds(span.Start, declaration.SpanStart); newDeclaration = declaration.WithLeadingTrivia(leadingTrivia.Where(f => !spanToRemove.Contains(f.Span))); } else { SyntaxTrivia trivia = declaration.FindTrivia(span.Start); Debug.Assert(trivia != default(SyntaxTrivia)); SyntaxToken token = trivia.Token; SyntaxTriviaList trailingTrivia = token.TrailingTrivia; Debug.Assert(trailingTrivia.Contains(trivia)); for (int i = 0; i < trailingTrivia.Count; i++) { if (trailingTrivia[i].Span == span) { comments = ImmutableArray.Create(_leadingSlashesRegex.Replace(trailingTrivia[i].ToString(), "")); SyntaxToken newToken = token.WithTrailingTrivia(trailingTrivia.Skip(i + 1)); newDeclaration = newDeclaration.ReplaceToken(token, newToken); break; } } } var settings = new DocumentationCommentGeneratorSettings(comments); newDeclaration = newDeclaration.WithNewSingleLineDocumentationComment(settings); return(document.ReplaceNodeAsync(declaration, newDeclaration, cancellationToken)); }
public override SyntaxToken VisitToken(SyntaxToken token) { if (_separators.Contains(token)) { SyntaxTriviaList triviaList = token.TrailingTrivia; if (!triviaList.Contains(SyntaxKind.EndOfLineTrivia)) { return(token.WithTrailingTrivia(triviaList.TrimEnd().Add(CSharpFactory.NewLine()))); } } return(base.VisitToken(token)); }
public static Task <Document> RefactorAsync( Document document, ElseClauseSyntax elseClause, CancellationToken cancellationToken = default(CancellationToken)) { var ifStatement = (IfStatementSyntax)elseClause.Parent; List <StatementSyntax> newStatements = null; if (elseClause.Statement is BlockSyntax block) { SyntaxList <StatementSyntax> statements = block.Statements; if (!statements.Any()) { return(document.RemoveNodeAsync(elseClause, SyntaxRemoveOptions.KeepExteriorTrivia, cancellationToken)); } newStatements = new List <StatementSyntax>() { ifStatement.WithElse(null) }; StatementSyntax lastStatement = statements.Last(); SyntaxTriviaList leadingTrivia = lastStatement.GetLeadingTrivia(); if (!leadingTrivia.Contains(SyntaxKind.EndOfLineTrivia)) { statements = statements.Replace(lastStatement, lastStatement.WithLeadingTrivia(leadingTrivia.Insert(0, NewLine()))); } foreach (StatementSyntax statement in statements) { newStatements.Add(statement.WithFormatterAnnotation()); } } else { StatementSyntax statement = elseClause.Statement; SyntaxTriviaList leadingTrivia = statement.GetLeadingTrivia(); if (!leadingTrivia.Contains(SyntaxKind.EndOfLineTrivia)) { statement = statement.WithLeadingTrivia(leadingTrivia.Insert(0, NewLine())); } statement = statement.WithFormatterAnnotation(); newStatements = new List <StatementSyntax>() { ifStatement.WithElse(null), statement }; } if (ifStatement.IsEmbedded()) { BlockSyntax newBlock = Block(newStatements).WithFormatterAnnotation(); return(document.ReplaceNodeAsync(ifStatement, newBlock, cancellationToken)); } else { return(document.ReplaceNodeAsync(ifStatement, newStatements, cancellationToken)); } }