private static DeclarationExpressionSyntax GetDeclarationExpression( SourceText sourceText, IdentifierNameSyntax identifier, TypeSyntax newType, VariableDeclaratorSyntax declaratorOpt) { newType = newType.WithoutTrivia().WithAdditionalAnnotations(Formatter.Annotation); var designation = SyntaxFactory.SingleVariableDesignation(identifier.Identifier); if (declaratorOpt != null) { // We're removing a single declarator. Copy any comments it has to the out-var. // // Note: this is tricky due to comment ownership. We want the comments that logically // belong to the declarator, even if our syntax model attaches them to other tokens. var precedingTrivia = declaratorOpt.GetAllPrecedingTriviaToPreviousToken( sourceText, includePreviousTokenTrailingTriviaOnlyIfOnSameLine: true); if (precedingTrivia.Any(t => t.IsSingleOrMultiLineComment())) { designation = designation.WithPrependedLeadingTrivia(MassageTrivia(precedingTrivia)); } if (declaratorOpt.GetTrailingTrivia().Any(t => t.IsSingleOrMultiLineComment())) { designation = designation.WithAppendedTrailingTrivia(MassageTrivia(declaratorOpt.GetTrailingTrivia())); } } return(SyntaxFactory.DeclarationExpression(newType, designation)); }
private static DeclarationExpressionSyntax GetDeclarationExpression( SourceText sourceText, IdentifierNameSyntax identifier, TypeSyntax newType, VariableDeclaratorSyntax declaratorOpt) { newType = newType.WithoutTrivia().WithAdditionalAnnotations(Formatter.Annotation); var designation = SyntaxFactory.SingleVariableDesignation(identifier.Identifier); if (declaratorOpt != null) { // We're removing a single declarator. Copy any comments it has to the out-var. // // Note: this is tricky due to comment ownership. We want hte comments that logically // belong to the declarator, even if our syntax model attaches them to other tokens. var precedingTrivia = declaratorOpt.GetAllPrecedingTriviaToPreviousToken( sourceText, includePreviousTokenTrailingTriviaOnlyIfOnSameLine: true); if (precedingTrivia.Any(t => t.IsSingleOrMultiLineComment())) { designation = designation.WithPrependedLeadingTrivia(MassageTrivia(precedingTrivia)); } if (declaratorOpt.GetTrailingTrivia().Any(t => t.IsSingleOrMultiLineComment())) { designation = designation.WithAppendedTrailingTrivia(MassageTrivia(declaratorOpt.GetTrailingTrivia())); } } return SyntaxFactory.DeclarationExpression(newType, designation); }