private static Task <Document> RefactorAsync( Document document, EnumDeclarationSyntax enumDeclaration, TextSpan span, CancellationToken cancellationToken) { SyntaxTrivia trivia = enumDeclaration.FindTrivia(span.Start); SyntaxToken token = trivia.Token; EnumMemberDeclarationSyntax enumMemberDeclaration = token .GetPreviousToken() .Parent .FirstAncestorOrSelf <EnumMemberDeclarationSyntax>(); int enumMemberIndex = enumDeclaration.Members.IndexOf(enumMemberDeclaration); SyntaxTriviaList trailingTrivia = token.TrailingTrivia; SyntaxToken newToken = token.WithTrailingTrivia(trailingTrivia.Skip(trailingTrivia.IndexOf(trivia) + 1)); EnumDeclarationSyntax newEnumDeclaration = enumDeclaration.ReplaceToken(token, newToken); var settings = new DocumentationCommentGeneratorSettings(ImmutableArray.Create(_leadingSlashesRegex.Replace(trivia.ToString(), ""))); EnumMemberDeclarationSyntax newEnumMemberDeclaration = newEnumDeclaration.Members[enumMemberIndex].WithNewSingleLineDocumentationComment(settings); newEnumDeclaration = newEnumDeclaration.WithMembers(newEnumDeclaration.Members.ReplaceAt(enumMemberIndex, newEnumMemberDeclaration)); return(document.ReplaceNodeAsync(enumDeclaration, newEnumDeclaration, cancellationToken)); }
public static SyntaxTriviaList TrimWhitespace(this SyntaxTriviaList list) { int startIndex = 0; for (int i = 0; i < list.Count; i++) { if (!list[i].IsKind(SyntaxKind.WhitespaceTrivia) && !list[i].IsKind(SyntaxKind.EndOfLineTrivia)) { startIndex = i; } } int endIndex = list.Count; for (int i = list.Count - 1; i >= 0; i--) { if (!list[i].IsKind(SyntaxKind.WhitespaceTrivia) && !list[i].IsKind(SyntaxKind.EndOfLineTrivia)) { endIndex = i; } } return(SyntaxFactory.TriviaList(list.Skip(startIndex).Take(endIndex + 1 - startIndex))); }
public static List <SyntaxTriviaList> SplitBy(this SyntaxTriviaList list, SyntaxKind triviaKind, bool keepSplitTrivia = false) { var result = new List <SyntaxTriviaList>(); if (list.Count < 2) { result.Add(list); return(result); } IEnumerable <SyntaxTrivia> currentList = null; var currentCount = 0; while (currentCount < list.Count) { currentList = list.Skip(currentCount).TakeWhile(x => x.IsKind(triviaKind) == false); var triviaList = currentList.ToSyntaxTriviaList(); currentCount += triviaList.Count + 1; if (keepSplitTrivia && currentCount < list.Count) { triviaList = triviaList.Add(list[currentCount - 1]); } if (currentList.Any()) { result.Add(triviaList); } } return(result); }
public static SyntaxTriviaList Trim(this SyntaxTriviaList list) { int startIndex = 0; for (int i = 0; i < list.Count; i++) { if (!list[i].IsWhitespaceOrEndOfLineTrivia()) { startIndex = i; break; } } int endIndex = -1; for (int i = list.Count - 1; i > startIndex; i--) { if (!list[i].IsWhitespaceOrEndOfLineTrivia()) { endIndex = i; break; } } if (startIndex > 0 || endIndex >= 0) { return(TriviaList(list.Skip(startIndex).Take(endIndex + 1 - startIndex))); } else { return(list); } }
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)); }
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)); }
protected SyntaxTriviaList CleanUpList(SyntaxTriviaList newList) { var lineBreaksAtBeginning = newList.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > 1) { newList = newList.Skip(lineBreaksAtBeginning - 1).ToSyntaxTriviaList(); } return(newList); }
int RemoveBlankDuplication(SyntaxTriviaList syntaxTrivias, SyntaxKind kind, int iterationIndex) { if (iterationIndex >= syntaxTrivias.Count) { return(-1); } var lineBreaksAtBeginning = syntaxTrivias.Skip(iterationIndex).TakeWhile(t => t.IsKind(kind)).Count(); return(lineBreaksAtBeginning - 1); }
public override SyntaxTriviaList VisitList(SyntaxTriviaList list) { list = base.VisitList(list); var lineBreaksAtBeginning = list.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > 1) { list = SyntaxFactory.TriviaList(list.Skip(lineBreaksAtBeginning - 1)); } return list; }
public override SyntaxTriviaList VisitList(SyntaxTriviaList list) { list = base.VisitList(list); var lineBreaksAtBeginning = list.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > 1) { list = SyntaxFactory.TriviaList(list.Skip(lineBreaksAtBeginning - 1)); } return(list); }
public static async Task <Document> RefactorAsync( Document document, NamespaceDeclarationSyntax namespaceDeclaration, CancellationToken cancellationToken = default(CancellationToken)) { if (document == null) { throw new ArgumentNullException(nameof(document)); } if (namespaceDeclaration == null) { throw new ArgumentNullException(nameof(namespaceDeclaration)); } SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var compilationUnit = (CompilationUnitSyntax)root; SyntaxList <UsingDirectiveSyntax> usings = namespaceDeclaration.Usings; CompilationUnitSyntax newCompilationUnit = compilationUnit .RemoveNodes(usings, SyntaxRemoveOptions.KeepUnbalancedDirectives); if (!compilationUnit.Usings.Any()) { SyntaxTriviaList leadingTrivia = compilationUnit.GetLeadingTrivia(); SyntaxTrivia[] topTrivia = GetTopSingleLineComments(leadingTrivia).ToArray(); if (topTrivia.Length > 0) { newCompilationUnit = newCompilationUnit.WithoutLeadingTrivia(); usings = usings.Replace( usings.First(), usings.First().WithLeadingTrivia(topTrivia)); usings = usings.Replace( usings.Last(), usings.Last().WithTrailingTrivia(leadingTrivia.Skip(topTrivia.Length))); } } newCompilationUnit = newCompilationUnit.AddUsings(usings.Select(f => f.WithFormatterAnnotation())); return(document.WithSyntaxRoot(newCompilationUnit)); }
SyntaxTriviaList CleanUpList(SyntaxTriviaList newList, CleanupTypes?option = null) { if (option.HasValue && CheckOption((int)option.Value) == false) { return(newList); } var lineBreaksAtBeginning = newList.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > 1) { newList = newList.Skip(lineBreaksAtBeginning - 1).ToSyntaxTriviaList(); } return(newList); }
private static string GetIndent(SyntaxTriviaList leadingTrivia) { if (leadingTrivia.Any()) { int index = leadingTrivia.Count; while (index >= 1 && leadingTrivia[index - 1].IsWhitespaceTrivia()) { index--; } return(string.Concat(leadingTrivia.Skip(index))); } return(""); }
public static SyntaxTriviaList TrimStart(this SyntaxTriviaList list) { for (int i = 0; i < list.Count; i++) { if (list[i].IsKind(SyntaxKind.WhitespaceTrivia)) { continue; } if (list[i].IsKind(SyntaxKind.EndOfLineTrivia)) { continue; } return(TriviaList(list.Skip(i))); } return(SyntaxTriviaList.Empty); }
public static DocumentationCommentInserter Create(SyntaxTriviaList leadingTrivia) { if (leadingTrivia.Any()) { int index = leadingTrivia.Count; while (index >= 1 && leadingTrivia[index - 1].IsWhitespaceTrivia()) { index--; } string indent = string.Concat(leadingTrivia.Skip(index)); return(new DocumentationCommentInserter(leadingTrivia, index, indent)); } return(Default); }
public static SyntaxTriviaList TrimStart(this SyntaxTriviaList list) { for (int i = 0; i < list.Count; i++) { if (!list[i].IsWhitespaceOrEndOfLineTrivia()) { if (i > 0) { return(TriviaList(list.Skip(i))); } else { return(list); } } } return(SyntaxTriviaList.Empty); }
public static MemberDeclarationSyntax GenerateAndAttach(MemberDeclarationSyntax memberDeclaration, DocumentationCommentGeneratorSettings settings = null) { if (memberDeclaration == null) { throw new ArgumentNullException(nameof(memberDeclaration)); } SyntaxTriviaList leadingTrivia = memberDeclaration.GetLeadingTrivia(); int index = 0; string indent = ""; if (leadingTrivia.Any()) { index = leadingTrivia.Count - 1; for (int i = leadingTrivia.Count - 1; i >= 0; i--) { if (leadingTrivia[i].IsWhitespaceTrivia()) { index = i; } else { break; } } indent = string.Concat(leadingTrivia.Skip(index)); } settings = settings ?? DocumentationCommentGeneratorSettings.Default; settings = settings.WithIndent(indent); SyntaxTriviaList comment = Generate(memberDeclaration, settings); SyntaxTriviaList newLeadingTrivia = leadingTrivia.InsertRange(index, comment); return(memberDeclaration.WithLeadingTrivia(newLeadingTrivia)); }
protected SyntaxTriviaList CleanUpList(SyntaxTriviaList syntaxTrivias, int exactNumberOfBlanks) { var lineBreaksAtBeginning = syntaxTrivias.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > exactNumberOfBlanks) { syntaxTrivias = syntaxTrivias.Skip(lineBreaksAtBeginning - exactNumberOfBlanks) .ToSyntaxTriviaList(); } else if (lineBreaksAtBeginning < exactNumberOfBlanks) { var newList = syntaxTrivias.ToList(); for (var i = lineBreaksAtBeginning; i < exactNumberOfBlanks; i++) { newList.Insert(0, _endOfLineTrivia); } syntaxTrivias = new SyntaxTriviaList().AddRange(newList); } return(syntaxTrivias); }
private static SyntaxNode ProcessTriviaList(SyntaxNode syntaxRoot, SyntaxTriviaList triviaList, SyntaxTrivia singleLineComment) { var index = triviaList.IndexOf(singleLineComment); if (index == -1) { return(null); } do { index++; }while (!triviaList[index].IsKind(SyntaxKind.EndOfLineTrivia)); var startOfBlankLine = index; do { index++; }while (!triviaList[index].IsKind(SyntaxKind.EndOfLineTrivia)); return(syntaxRoot.ReplaceTrivia(triviaList.Skip(startOfBlankLine).Take(index - startOfBlankLine), (t1, t2) => default(SyntaxTrivia))); }
private static SyntaxNode ProcessTriviaList(SyntaxNode syntaxRoot, SyntaxTriviaList triviaList, SyntaxTrivia singleLineComment) { var index = triviaList.IndexOf(singleLineComment); if (index == -1) { return null; } do { index++; } while (!triviaList[index].IsKind(SyntaxKind.EndOfLineTrivia)); var startOfBlankLine = index; do { index++; } while (!triviaList[index].IsKind(SyntaxKind.EndOfLineTrivia)); return syntaxRoot.ReplaceTrivia(triviaList.Skip(startOfBlankLine).Take(index - startOfBlankLine), (t1, t2) => default(SyntaxTrivia)); }
/// <summary> /// Remove any copyright header that already exists. /// </summary> private SyntaxTriviaList RemoveExistingHeader(SyntaxTriviaList oldList) { var foundHeader = false; var i = 0; MovePastBlankLines(oldList, ref i); while (i < oldList.Count && IsLineComment(oldList[i])) { if (oldList[i].ToFullString().IndexOf("copyright", StringComparison.OrdinalIgnoreCase) >= 0) { foundHeader = true; } i++; } if (!foundHeader) { return(oldList); } MovePastBlankLines(oldList, ref i); return(CreateTriviaList(oldList.Skip(i))); }
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; }
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); }
private static SyntaxTriviaList StripFileHeader(SyntaxTriviaList newLeadingTrivia) { var fileHeader = GetFileHeader(newLeadingTrivia); return SyntaxTriviaList.Empty.AddRange(newLeadingTrivia.Skip(fileHeader.Count)); }
public static CompilationUnitSyntax AddUsings(this CompilationUnitSyntax compilationUnit, bool keepSingleLineCommentsOnTop, params UsingDirectiveSyntax[] usings) { if (compilationUnit == null) { throw new ArgumentNullException(nameof(compilationUnit)); } if (usings == null) { throw new ArgumentNullException(nameof(usings)); } if (keepSingleLineCommentsOnTop && usings.Length > 0 && !compilationUnit.Usings.Any()) { SyntaxTriviaList leadingTrivia = compilationUnit.GetLeadingTrivia(); SyntaxTrivia[] topTrivia = GetTopSingleLineComments(leadingTrivia).ToArray(); if (topTrivia.Length > 0) { compilationUnit = compilationUnit.WithoutLeadingTrivia(); usings[0] = usings[0].WithLeadingTrivia(topTrivia); usings[usings.Length - 1] = usings[usings.Length - 1].WithTrailingTrivia(leadingTrivia.Skip(topTrivia.Length)); } } return(compilationUnit.AddUsings(usings)); }
/// <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.Skip(nonWhitespaceIndex)) : SyntaxFactory.TriviaList()); }
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); }
private static List <SyntaxTrivia> StripFileHeader(SyntaxTriviaList leadingTrivia) { var fileHeader = GetFileHeader(leadingTrivia); return(leadingTrivia.Skip(fileHeader.Count).ToList()); }
private static SyntaxTriviaList StripFileHeader(SyntaxTriviaList newLeadingTrivia) { var fileHeader = GetFileHeader(newLeadingTrivia); return(SyntaxTriviaList.Empty.AddRange(newLeadingTrivia.Skip(fileHeader.Count))); }
private static List<SyntaxTrivia> StripFileHeader(SyntaxTriviaList leadingTrivia) { var fileHeader = GetFileHeader(leadingTrivia); return leadingTrivia.Skip(fileHeader.Count).ToList(); }
public static SyntaxTriviaList WithoutLeadingBlankLines(this SyntaxTriviaList triviaList) { var triviaInLeadingBlankLines = GetLeadingBlankLines(triviaList).SelectMany(l => l); return(new SyntaxTriviaList(triviaList.Skip(triviaInLeadingBlankLines.Count()))); }
/// <summary> /// Remove any copyright header that already exists. /// </summary> private SyntaxTriviaList RemoveExistingHeader(SyntaxTriviaList oldList) { var foundHeader = false; var i = 0; MovePastBlankLines(oldList, ref i); while (i < oldList.Count && IsLineComment(oldList[i])) { if (oldList[i].ToFullString().IndexOf("copyright", StringComparison.OrdinalIgnoreCase) >= 0) { foundHeader = true; } i++; } if (!foundHeader) { return oldList; } MovePastBlankLines(oldList, ref i); return CreateTriviaList(oldList.Skip(i)); }