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) ); }
SyntaxTriviaList Insert_Space_Before_Comment_Text(SyntaxTriviaList syntaxTrivias, SyntaxTrivia currentTrivia) { if (CheckOption((int)CleanupTypes.Insert_Space_Before_Comment_Text)) { if (currentTrivia.IsKind(SyntaxKind.SingleLineCommentTrivia)) { var commentText = currentTrivia.ToFullString().Trim(); if (commentText.Length > 2 && commentText[2] != ' ') { commentText = $"{commentText.Substring(0, 2)} {commentText.Substring(2)}"; syntaxTrivias = syntaxTrivias.Replace(currentTrivia, SyntaxFactory.Comment(commentText)); } } } return(syntaxTrivias); }
public static SyntaxTriviaList ReplaceAt(this SyntaxTriviaList triviaList, int index, SyntaxTrivia newTrivia) { return(triviaList.Replace(triviaList[index], newTrivia)); }
protected SyntaxTriviaList ProcessSpecialTrivias(SyntaxTriviaList syntaxTrivias, bool itsForCloseBrace) { if (CheckShortSyntax(syntaxTrivias, itsForCloseBrace)) { return(syntaxTrivias); } var specialTriviasCount = syntaxTrivias.Count(t => !t.IsKind(SyntaxKind.EndOfLineTrivia) && !t.IsKind(SyntaxKind.WhitespaceTrivia)); var outputTriviasList = new List <SyntaxTrivia>(); var specialTiviasCount = 0; var bAddedBlankLine = false; for (var i = 0; i < syntaxTrivias.Count; i++) { var countOfChars = 0; if (specialTiviasCount == specialTriviasCount) { if (itsForCloseBrace) { i += RemoveBlankDuplication(syntaxTrivias, SyntaxKind.EndOfLineTrivia, i) + 1; if (RemoveBlankDuplication(syntaxTrivias, SyntaxKind.WhitespaceTrivia, i) != -1) { outputTriviasList.Add(syntaxTrivias[i]); } i = syntaxTrivias.Count; continue; } } if ( ( syntaxTrivias[i].IsKind(SyntaxKind.EndOfLineTrivia) || syntaxTrivias[i].IsKind(SyntaxKind.WhitespaceTrivia) || syntaxTrivias[i].IsKind(SyntaxKind.SingleLineCommentTrivia) || syntaxTrivias[i].IsKind(SyntaxKind.MultiLineCommentTrivia) ) == false ) { outputTriviasList.Add(syntaxTrivias[i]); specialTiviasCount++; continue; } if (syntaxTrivias[i].IsKind(SyntaxKind.SingleLineCommentTrivia) || syntaxTrivias[i].IsKind(SyntaxKind.MultiLineCommentTrivia)) { if (syntaxTrivias[i].IsKind(SyntaxKind.SingleLineCommentTrivia)) { var commentText = syntaxTrivias[i].ToFullString().Trim(); if (commentText.Length > 2 && commentText[2] != ' ') { commentText = $"{commentText.Substring(0, 2)} {commentText.Substring(2)}"; } syntaxTrivias = syntaxTrivias.Replace(syntaxTrivias[i], SyntaxFactory.Comment(commentText)); } outputTriviasList.Add(syntaxTrivias[i]); i++; if (i < syntaxTrivias.Count && syntaxTrivias[i].IsKind(SyntaxKind.EndOfLineTrivia)) { outputTriviasList.Add(syntaxTrivias[i]); } specialTiviasCount++; continue; } if ((countOfChars = RemoveBlankDuplication(syntaxTrivias, SyntaxKind.EndOfLineTrivia, i)) != -1) { outputTriviasList.Add(syntaxTrivias[i]); i += countOfChars + 1; bAddedBlankLine = true; } if ((countOfChars = RemoveBlankDuplication(syntaxTrivias, SyntaxKind.WhitespaceTrivia, i)) != -1) { outputTriviasList.Add(syntaxTrivias[i]); i += countOfChars; } else if (bAddedBlankLine) { i--; } bAddedBlankLine = false; } return(outputTriviasList.ToSyntaxTriviaList()); }
private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document document, SyntaxNode root, StyleCopSettings settings, int commentIndex, XmlFileHeader header) { SyntaxTriviaList trivia = root.GetLeadingTrivia(); var commentTrivia = trivia[commentIndex]; // Is the comment pushed in by a prefix? var commentIndentation = string.Empty; if (commentIndex > 0) { var prefixTrivia = trivia[commentIndex - 1]; if (prefixTrivia.IsKind(SyntaxKind.WhitespaceTrivia)) { commentIndentation = prefixTrivia.ToFullString(); } } var triviaString = commentTrivia.ToFullString(); var startIndex = triviaString.IndexOf("/*", StringComparison.Ordinal) + 2; var endIndex = triviaString.LastIndexOf("*/", StringComparison.Ordinal); var commentContext = triviaString.Substring(startIndex, endIndex - startIndex).Trim(' ', '\t').TrimEnd(); var triviaStringParts = commentContext.Replace("\r\n", "\n").Split('\n'); // Assume we have comments that have a leading * string interlinePadding = " *"; int minExpectedLength = (commentIndentation + interlinePadding).Length; string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp); // Examine second line to see if we should have stars or not if it's blank // set the interline padding to be blank also. if ((triviaStringParts.Length > 2) && (triviaStringParts[1].Length > minExpectedLength) && string.IsNullOrWhiteSpace(triviaStringParts[1].Substring(0, minExpectedLength))) { interlinePadding = " "; } // Pad line that used to be next to a /* triviaStringParts[0] = commentIndentation + interlinePadding + " " + triviaStringParts[0]; StringBuilder sb = StringBuilderPool.Allocate(); string fileName = GetFileName(document); var copyrightText = GetCopyrightText(commentIndentation + interlinePadding, settings.DocumentationRules.GetCopyrightText(fileName), newLineText); var newHeader = WrapInXmlComment(commentIndentation + interlinePadding, copyrightText, fileName, settings, newLineText); sb.Append(commentIndentation); sb.Append("/*"); if (header.GetElement("copyright") == null) { // No copyright element at the moment so add us. sb.Append(newHeader.Substring(minExpectedLength)); sb.Append(newLineText); // Append the original stuff foreach (var oldLine in triviaStringParts) { sb.Append(oldLine.TrimEnd()); sb.Append(newLineText); } } else { bool firstLine = true; bool inCopyright = false; foreach (var oldLine in triviaStringParts) { var openingTag = oldLine.Contains("<copyright "); var closingTag = oldLine.Contains("</copyright>") || (openingTag && oldLine.Trim().EndsWith("/>")); if (openingTag) { inCopyright = !closingTag; sb.Append(newHeader.Substring(firstLine ? minExpectedLength : 0)); sb.Append(newLineText); } if (inCopyright) { inCopyright = !closingTag; } else { sb.Append(oldLine.Substring(firstLine ? minExpectedLength : 0)); sb.Append(newLineText); } firstLine = false; } } sb.Append(commentIndentation); sb.Append(" */"); // Get rid of any trailing spaces. var lines = sb.ToString().Split(new string[] { newLineText }, StringSplitOptions.None); sb.Clear(); for (int i = 0; i < lines.Length; i++) { sb.Append((i == 0 ? string.Empty : newLineText) + lines[i].TrimEnd()); } var newTrivia = SyntaxFactory.SyntaxTrivia(SyntaxKind.MultiLineCommentTrivia, StringBuilderPool.ReturnAndFree(sb)); return(root.WithLeadingTrivia(trivia.Replace(commentTrivia, newTrivia))); }
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)); }