private void DoTestAddInsertRemoveReplaceOnEmptyList(SyntaxTriviaList list) { Assert.Equal(0, list.Count); var triviaD = SyntaxFactory.ParseLeadingTrivia("/*D*/")[0]; var triviaE = SyntaxFactory.ParseLeadingTrivia("/*E*/")[0]; var newList = list.Add(triviaD); Assert.Equal(1, newList.Count); Assert.Equal("/*D*/", newList.ToFullString()); newList = list.AddRange(new[] { triviaD, triviaE }); Assert.Equal(2, newList.Count); Assert.Equal("/*D*//*E*/", newList.ToFullString()); newList = list.Insert(0, triviaD); Assert.Equal(1, newList.Count); Assert.Equal("/*D*/", newList.ToFullString()); newList = list.InsertRange(0, new[] { triviaD, triviaE }); Assert.Equal(2, newList.Count); Assert.Equal("/*D*//*E*/", newList.ToFullString()); newList = list.Remove(triviaD); Assert.Equal(0, newList.Count); Assert.Equal(-1, list.IndexOf(triviaD)); Assert.Throws <ArgumentOutOfRangeException>(() => list.RemoveAt(0)); Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(1, triviaD)); Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(-1, triviaD)); Assert.Throws <ArgumentOutOfRangeException>( () => list.InsertRange(1, new[] { triviaD }) ); Assert.Throws <ArgumentOutOfRangeException>( () => list.InsertRange(-1, new[] { triviaD }) ); Assert.Throws <ArgumentOutOfRangeException>(() => list.Replace(triviaD, triviaE)); Assert.Throws <ArgumentOutOfRangeException>( () => list.ReplaceRange(triviaD, new[] { triviaE }) ); Assert.Throws <ArgumentOutOfRangeException>(() => list.Add(default(SyntaxTrivia))); Assert.Throws <ArgumentOutOfRangeException>(() => list.Insert(0, default(SyntaxTrivia))); Assert.Throws <ArgumentNullException>( () => list.AddRange((IEnumerable <SyntaxTrivia>)null) ); Assert.Throws <ArgumentNullException>( () => list.InsertRange(0, (IEnumerable <SyntaxTrivia>)null) ); }
private static IdentifierNameSyntax PadMemberAccess( SimpleLambdaExpressionSyntax node, IdentifierNameSyntax memberAccess) { // We want to make the new span var originalSpan = node.GetLocation().GetMappedLineSpan(); var charactersToExclude = memberAccess.Identifier.Text.Length; var triviaList = new SyntaxTriviaList(); // Go through each token and // 1. Append leading trivia // 2. Append the same number of whitespace as the length of the token text // 3. Append trailing trivia foreach (var token in node.DescendantTokens()) { if (token.HasLeadingTrivia) { triviaList = triviaList.AddRange(token.LeadingTrivia); } // Need to exclude the length of the member name from the padding. var padding = token.Text.Length; if (padding > charactersToExclude) { padding -= charactersToExclude; charactersToExclude = 0; } else { charactersToExclude -= padding; padding = 0; } if (padding > 0) { triviaList = triviaList.Add(SyntaxFactory.Whitespace(new string(' ', padding))); } if (token.HasTrailingTrivia) { triviaList = triviaList.AddRange(token.TrailingTrivia); } } return(memberAccess .WithLeadingTrivia(node.GetLeadingTrivia()) .WithTrailingTrivia(triviaList)); }
private static void AddIfNotWhiteSpaceOrEndOfLine(IEnumerable <SyntaxTrivia> trivia, ref SyntaxTriviaList triviaList) { if (trivia.Any(f => !f.IsWhitespaceOrEndOfLineTrivia())) { triviaList = triviaList.AddRange(trivia); } }
public static SyntaxToken WithPrependedLeadingTrivia(this SyntaxToken token, IEnumerable <SyntaxTrivia> trivia) { var list = new SyntaxTriviaList(); list = list.AddRange(trivia); return(token.WithPrependedLeadingTrivia(list)); }
public static T WithPrependedLeadingTrivia <T>(this T node, IEnumerable <SyntaxTrivia> trivia) where T : SyntaxNode { var list = new SyntaxTriviaList(); list = list.AddRange(trivia); return(node.WithPrependedLeadingTrivia(list)); }
public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { if (node.OperatorToken.TrailingTrivia.Count != 0) { SyntaxTriviaList trivia = node.Name.GetLeadingTrivia(); trivia = trivia.AddRange(node.OperatorToken.TrailingTrivia); node = node.WithName(node.Name.WithLeadingTrivia(trivia)); node = node.WithOperatorToken(node.OperatorToken.WithTrailingTrivia()); } return(base.VisitMemberAccessExpression(node)); }
public static async Task <Document> RefactorAsync( Document document, BinaryExpressionSyntax binaryExpression, CancellationToken cancellationToken) { ExpressionSyntax left = binaryExpression.Left; ExpressionSyntax right = binaryExpression.Right; SyntaxToken operatorToken = binaryExpression.OperatorToken; ExpressionSyntax newNode = binaryExpression; TextSpan span = TextSpan.FromBounds(left.Span.End, right.Span.Start); IEnumerable <SyntaxTrivia> trivia = binaryExpression.DescendantTrivia(span); bool isWhiteSpaceOrEndOfLine = trivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()); if (left.IsBooleanLiteralExpression()) { SyntaxTriviaList leadingTrivia = binaryExpression.GetLeadingTrivia(); if (!isWhiteSpaceOrEndOfLine) { leadingTrivia = leadingTrivia.AddRange(trivia); } newNode = right .Negate() .WithLeadingTrivia(leadingTrivia); } else if (right.IsBooleanLiteralExpression()) { SyntaxTriviaList trailingTrivia = binaryExpression.GetTrailingTrivia(); if (!isWhiteSpaceOrEndOfLine) { trailingTrivia = trailingTrivia.InsertRange(0, trivia); } newNode = left .Negate() .WithTrailingTrivia(trailingTrivia); } #if DEBUG else { Debug.Assert(false, binaryExpression.ToString()); } #endif return(await document.ReplaceNodeAsync(binaryExpression, newNode.WithFormatterAnnotation(), cancellationToken).ConfigureAwait(false)); }
private static SyntaxNode FormatNodeRecursive(SyntaxNode node, SyntaxTriviaList startTrivia, bool useTabs, int tabsSize, int depth) { var newTrivia = useTabs ? startTrivia.AddRange(Enumerable.Repeat(SyntaxFactory.Tab, depth)) : startTrivia.AddRange(Enumerable.Repeat(SyntaxFactory.Space, depth * tabsSize)); switch (node.Kind()) { case SyntaxKind.Block: return(FormatBlock(node as BlockSyntax, startTrivia, useTabs, tabsSize)); default: var newNode = node; if (node.HasLeadingTrivia && node.GetLeadingTrivia()[0].IsKind(SyntaxKind.EndOfLineTrivia)) { newNode = newNode.WithLeadingTrivia(startTrivia); } else { var prev = node.GetFirstToken().GetPreviousToken(); if (prev.TrailingTrivia.Count > 0 && prev.TrailingTrivia.Last().IsKind(SyntaxKind.EndOfLineTrivia)) { newNode = newNode.WithLeadingTrivia(startTrivia); } } newNode = newNode.ReplaceNodes(newNode.ChildNodes(), (old, potential) => FormatNodeRecursive(old, newTrivia, useTabs, tabsSize, depth++)); newNode = newNode .ReplaceTokens( newNode.ChildTokens().Where(x => x.LeadingTrivia.Count > 0 && x.LeadingTrivia[0].IsKind(SyntaxKind.EndOfLineTrivia)), (old, potential) => { return(old.WithLeadingTrivia(newTrivia)); }); return(newNode); } }
public static Task <Document> RefactorAsync( Document document, BinaryExpressionSyntax binaryExpression, CancellationToken cancellationToken) { ExpressionSyntax left = binaryExpression.Left; ExpressionSyntax right = binaryExpression.Right; ExpressionSyntax newNode = binaryExpression; TextSpan span = TextSpan.FromBounds(left.Span.End, right.SpanStart); IEnumerable <SyntaxTrivia> trivia = binaryExpression.DescendantTrivia(span); bool isWhiteSpaceOrEndOfLine = trivia.All(f => f.IsWhitespaceOrEndOfLineTrivia()); if (IsBooleanLiteralExpression(left.Kind())) { SyntaxTriviaList leadingTrivia = binaryExpression.GetLeadingTrivia(); if (!isWhiteSpaceOrEndOfLine) { leadingTrivia = leadingTrivia.AddRange(trivia); } newNode = right.WithLeadingTrivia(leadingTrivia); } else if (IsBooleanLiteralExpression(right.Kind())) { SyntaxTriviaList trailingTrivia = binaryExpression.GetTrailingTrivia(); if (!isWhiteSpaceOrEndOfLine) { trailingTrivia = trailingTrivia.InsertRange(0, trivia); } newNode = left.WithTrailingTrivia(trailingTrivia); } else { SyntaxDebug.Fail(binaryExpression); } return(document.ReplaceNodeAsync(binaryExpression, newNode.WithFormatterAnnotation(), cancellationToken)); }
private static BlockSyntax FormatBlock(BlockSyntax block, SyntaxTriviaList startTrivia, bool useTabs, int tabSize) { var statementTrivia = useTabs ? startTrivia.Add(SyntaxFactory.Tab) : startTrivia.AddRange(Enumerable.Repeat(SyntaxFactory.Space, tabSize)); var prevToken = block.OpenBraceToken.GetPreviousToken(); var newBlock = block.Update( block.OpenBraceToken .WithLeadingTrivia(startTrivia) .WithTrailingTrivia(block.OpenBraceToken.TrailingTrivia.AddTrailingEndOfLineIfNotExist()), SyntaxFactory.List(block.Statements.Select(st => IndentationHelper.FormatNodeRecursive(st, statementTrivia, useTabs, tabSize).WithTrailingTrivia(st.GetTrailingTrivia().AddTrailingEndOfLineIfNotExist()))), block.CloseBraceToken.WithLeadingTrivia(startTrivia)); if (prevToken != null && prevToken.TrailingTrivia.Any(SyntaxKind.EndOfLineTrivia) == false) { return(newBlock = newBlock.WithLeadingTrivia(newBlock.GetLeadingTrivia().AddLeadingEndOfLineIfNotExist())); } return(newBlock); }
private SyntaxTriviaList RemoveRedundantTrailingWhitespace(SyntaxTriviaList oldTriviaList) { var newTriviaList = new List <SyntaxTrivia>(); var outSyntaxTriviaList = new SyntaxTriviaList(); for (var i = 0; i < oldTriviaList.Count; ++i) { var isRedundantWhitespace = i + 1 < oldTriviaList.Count && oldTriviaList[i].IsKind(SyntaxKind.WhitespaceTrivia) && oldTriviaList[i + 1].IsKind(SyntaxKind.EndOfLineTrivia); if (!isRedundantWhitespace) { newTriviaList.Add(oldTriviaList[i]); } } outSyntaxTriviaList = outSyntaxTriviaList.AddRange(newTriviaList); return(outSyntaxTriviaList); }
private AttributeListSyntax CreateAttributeList( ISymbol targetSymbol, Diagnostic diagnostic, bool isAssemblyAttribute, SyntaxTriviaList leadingTrivia, bool needsLeadingEndOfLine) { var attributeArguments = CreateAttributeArguments(targetSymbol, diagnostic, isAssemblyAttribute); var attribute = SyntaxFactory.Attribute(SyntaxFactory.ParseName(SuppressMessageAttributeName), attributeArguments) .WithAdditionalAnnotations(Simplifier.Annotation); var attributes = new SeparatedSyntaxList<AttributeSyntax>().Add(attribute); AttributeListSyntax attributeList; if (isAssemblyAttribute) { var targetSpecifier = SyntaxFactory.AttributeTargetSpecifier(SyntaxFactory.Token(SyntaxKind.AssemblyKeyword)); attributeList = SyntaxFactory.AttributeList(targetSpecifier, attributes); } else { attributeList = SyntaxFactory.AttributeList(attributes); } var endOfLineTrivia = SyntaxFactory.EndOfLine(@" "); var triviaList = SyntaxFactory.TriviaList(); if (needsLeadingEndOfLine) { triviaList = triviaList.Add(endOfLineTrivia); } return attributeList .WithLeadingTrivia(leadingTrivia.AddRange(triviaList)) .WithAdditionalAnnotations(Formatter.Annotation); }
private AttributeListSyntax CreateAttributeList( ISymbol targetSymbol, Diagnostic diagnostic, SyntaxTriviaList leadingTrivia, bool needsLeadingEndOfLine) { var attributeArguments = CreateAttributeArguments(targetSymbol, diagnostic); var attribute = SyntaxFactory.Attribute(SyntaxFactory.ParseName(SuppressMessageAttributeName), attributeArguments); var attributes = new SeparatedSyntaxList<AttributeSyntax>().Add(attribute); var targetSpecifier = SyntaxFactory.AttributeTargetSpecifier(SyntaxFactory.Token(SyntaxKind.AssemblyKeyword)); var attributeList = SyntaxFactory.AttributeList(targetSpecifier, attributes); var endOfLineTrivia = SyntaxFactory.ElasticCarriageReturnLineFeed; var triviaList = SyntaxFactory.TriviaList(); if (needsLeadingEndOfLine) { triviaList = triviaList.Add(endOfLineTrivia); } return attributeList.WithLeadingTrivia(leadingTrivia.AddRange(triviaList)); }
private void DoTestAddInsertRemoveReplaceOnEmptyList(SyntaxTriviaList list) { Assert.Equal(0, list.Count); var triviaD = SyntaxFactory.ParseLeadingTrivia("/*D*/")[0]; var triviaE = SyntaxFactory.ParseLeadingTrivia("/*E*/")[0]; var newList = list.Add(triviaD); Assert.Equal(1, newList.Count); Assert.Equal("/*D*/", newList.ToFullString()); newList = list.AddRange(new[] { triviaD, triviaE }); Assert.Equal(2, newList.Count); Assert.Equal("/*D*//*E*/", newList.ToFullString()); newList = list.Insert(0, triviaD); Assert.Equal(1, newList.Count); Assert.Equal("/*D*/", newList.ToFullString()); newList = list.InsertRange(0, new[] { triviaD, triviaE }); Assert.Equal(2, newList.Count); Assert.Equal("/*D*//*E*/", newList.ToFullString()); newList = list.Remove(triviaD); Assert.Equal(0, newList.Count); Assert.Equal(-1, list.IndexOf(triviaD)); Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(0)); Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(1, triviaD)); Assert.Throws<ArgumentOutOfRangeException>(() => list.Insert(-1, triviaD)); Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRange(1, new[] { triviaD })); Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRange(-1, new[] { triviaD })); Assert.Throws<ArgumentException>(() => list.Replace(triviaD, triviaE)); Assert.Throws<ArgumentException>(() => list.ReplaceRange(triviaD, new[] { triviaE })); Assert.Throws<ArgumentException>(() => list.Add(default(SyntaxTrivia))); Assert.Throws<ArgumentException>(() => list.Insert(0, default(SyntaxTrivia))); Assert.Throws<ArgumentNullException>(() => list.AddRange((IEnumerable<SyntaxTrivia>)null)); Assert.Throws<ArgumentNullException>(() => list.InsertRange(0, (IEnumerable<SyntaxTrivia>)null)); }