private static SyntaxTriviaList FixTriviaList(SyntaxTriviaList triviaList, IEnumerable <SyntaxTrivia> commentTrivias) { foreach (var singleLineComment in commentTrivias) { int commentLocation = triviaList.IndexOf(singleLineComment); if (commentLocation == -1) { continue; } int index = commentLocation + 1; index++; while (index < triviaList.Count && index > 0) { switch (triviaList[index].Kind()) { case SyntaxKind.EndOfLineTrivia: case SyntaxKind.WhitespaceTrivia: index++; break; default: if (triviaList[index - 1].IsKind(SyntaxKind.WhitespaceTrivia)) { index--; } triviaList = SyntaxTriviaList.Empty.AddRange(triviaList.Take(commentLocation + 2).Concat(triviaList.Skip(index))); // We found the trivia so we don't have to loop any longer index = -1; break; } } if (index == triviaList.Count) { if (triviaList[index - 1].IsKind(SyntaxKind.WhitespaceTrivia)) { index--; } triviaList = SyntaxTriviaList.Empty.AddRange(triviaList.Take(commentLocation + 2).Concat(triviaList.Skip(index))); } } return(triviaList); }
internal static MemberDeclarationSyntax RemoveSingleLineDocumentationComment(MemberDeclarationSyntax declaration) { if (declaration == null) { throw new ArgumentNullException(nameof(declaration)); } SyntaxTriviaList leadingTrivia = declaration.GetLeadingTrivia(); SyntaxTriviaList.Reversed.Enumerator en = leadingTrivia.Reverse().GetEnumerator(); int i = 0; while (en.MoveNext()) { SyntaxKind kind = en.Current.Kind(); if (kind == SyntaxKind.WhitespaceTrivia || kind == SyntaxKind.EndOfLineTrivia) { i++; } else if (kind == SyntaxKind.SingleLineDocumentationCommentTrivia) { return(declaration.WithLeadingTrivia(leadingTrivia.Take(leadingTrivia.Count - (i + 1)))); } else { return(declaration); } } return(declaration); }
private static T AddAttributeLists <T>( this T node, bool keepDocumentationCommentOnTop, AttributeListSyntax[] attributeLists, Func <T, bool> hasAttributeLists, Func <T, SyntaxList <AttributeListSyntax>, T> withAttributeLists, Func <T, AttributeListSyntax[], T> addAttributeLists) where T : SyntaxNode { if (keepDocumentationCommentOnTop && !hasAttributeLists(node) && attributeLists.Length > 0) { SyntaxTriviaList leadingTrivia = node.GetLeadingTrivia(); for (int i = 0; i < leadingTrivia.Count; i++) { if (leadingTrivia[i].IsDocumentationCommentTrivia()) { attributeLists[0] = attributeLists[0].PrependToLeadingTrivia(leadingTrivia.Take(i + 1)); node = node.WithLeadingTrivia(leadingTrivia.Skip(i + 1)); return(withAttributeLists(node, List(attributeLists))); } } } return(addAttributeLists(node, attributeLists)); }
internal static MemberDeclarationSyntax RemoveSingleLineDocumentationComment(MemberDeclarationSyntax member) { if (member == null) { throw new ArgumentNullException(nameof(member)); } SyntaxTriviaList leadingTrivia = member.GetLeadingTrivia(); SyntaxTriviaList.Reversed.Enumerator en = leadingTrivia.Reverse().GetEnumerator(); int i = 0; while (en.MoveNext()) { if (en.Current.IsWhitespaceOrEndOfLineTrivia()) { i++; } else if (en.Current.IsKind(SyntaxKind.SingleLineDocumentationCommentTrivia)) { return(member.WithLeadingTrivia(leadingTrivia.Take(leadingTrivia.Count - (i + 1)))); } else { return(member); } } return(member); }
private (SyntaxTriviaList beforeMacro, SyntaxTriviaList macro) SplitByMacro(SyntaxTriviaList triviaList) { for (int i = 1; i <= triviaList.Count; i++) { if (triviaList[i - 1].ToString().Contains("macros.")) { return(SyntaxTriviaList.Empty.AddRange(triviaList.Take(i - 1)), SyntaxTriviaList.Empty.AddRange(triviaList.Skip(i - 1))); } } return(triviaList, SyntaxTriviaList.Empty); }
public static SyntaxTriviaList TrimEnd(this SyntaxTriviaList list) { for (int i = list.Count - 1; i >= 0; i--) { if (list[i].IsKind(SyntaxKind.WhitespaceTrivia)) { continue; } if (list[i].IsKind(SyntaxKind.EndOfLineTrivia)) { continue; } return(TriviaList(list.Take(i + 1))); } return(SyntaxTriviaList.Empty); }
public static SyntaxTriviaList TrimEnd(this SyntaxTriviaList list) { for (int i = list.Count - 1; i >= 0; i--) { if (!list[i].IsWhitespaceOrEndOfLineTrivia()) { if (i < list.Count - 1) { return(TriviaList(list.Take(i + 1))); } else { return(list); } } } return(SyntaxTriviaList.Empty); }
private static SyntaxTriviaList FixTriviaList(SyntaxTriviaList triviaList, IEnumerable<SyntaxTrivia> commentTrivias) { foreach (var singleLineComment in commentTrivias) { int commentLocation = triviaList.IndexOf(singleLineComment); if (commentLocation == -1) { continue; } int index = commentLocation + 1; index++; while (index < triviaList.Count && index > 0) { switch (triviaList[index].Kind()) { case SyntaxKind.EndOfLineTrivia: case SyntaxKind.WhitespaceTrivia: index++; break; default: if (triviaList[index - 1].IsKind(SyntaxKind.WhitespaceTrivia)) { index--; } triviaList = SyntaxTriviaList.Empty.AddRange(triviaList.Take(commentLocation + 2).Concat(triviaList.Skip(index))); // We found the trivia so we don't have to loop any longer index = -1; break; } } if (index == triviaList.Count) { if (triviaList[index - 1].IsKind(SyntaxKind.WhitespaceTrivia)) { index--; } triviaList = SyntaxTriviaList.Empty.AddRange(triviaList.Take(commentLocation + 2).Concat(triviaList.Skip(index))); } } return triviaList; }
/// <summary> /// Strips all leading whitespace trivia from the trivia list until a non-whitespace trivia is encountered. /// </summary> /// <param name="triviaList">The trivia list to strip of its leading whitespace.</param> /// <param name="endOfLineIsWhitespace"><see langword="true"/> to treat <see cref="SyntaxKind.EndOfLineTrivia"/> /// as whitespace; otherwise, <see langword="false"/>.</param> /// <returns>The modified triviaList.</returns> internal static SyntaxTriviaList WithoutLeadingWhitespace(this SyntaxTriviaList triviaList, bool endOfLineIsWhitespace = true) { var nonWhitespaceIndex = IndexOfFirstNonWhitespaceTrivia(triviaList, endOfLineIsWhitespace); return((nonWhitespaceIndex >= 0) ? SyntaxFactory.TriviaList(triviaList.Take(nonWhitespaceIndex)) : SyntaxFactory.TriviaList()); }
/// <summary> /// Strips all trailing whitespace trivia from the trivia list until a non-whitespace trivia is encountered. /// </summary> /// <param name="triviaList">The trivia list to strip of its trailing whitespace.</param> /// <returns>The modified triviaList.</returns> internal static SyntaxTriviaList WithoutTrailingWhitespace(this SyntaxTriviaList triviaList) { var trailingWhitespaceIndex = IndexOfTrailingWhitespace(triviaList); return((trailingWhitespaceIndex >= 0) ? SyntaxFactory.TriviaList(triviaList.Take(trailingWhitespaceIndex)) : triviaList); }