本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.ReplaceTrivia方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.ReplaceTrivia方法的具体用法?C# SyntaxNode.ReplaceTrivia怎么用?C# SyntaxNode.ReplaceTrivia使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.ReplaceTrivia方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReplaceTabWithFourSpacesAsync
private Task<Document> ReplaceTabWithFourSpacesAsync(Document document, SyntaxNode root, SyntaxTrivia tabTrivia
, CancellationToken cancellationToken)
{
SyntaxTrivia fourSpacesTrivia = SyntaxFactory.Whitespace(new string(' ', 4 * tabTrivia.Span.Length));
SyntaxNode newRoot = root.ReplaceTrivia(tabTrivia, fourSpacesTrivia);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
示例2: RemoveImproperDocumentationTrivia
private static SyntaxNode RemoveImproperDocumentationTrivia(SyntaxNode node)
{
IEnumerable<SyntaxTrivia> commentTrivia = from t in node.DescendantTrivia()
where t.Kind() == SyntaxKind.DocumentationCommentTrivia
select t;
SyntaxNode newRoot = node.ReplaceTrivia(commentTrivia, (t1, t2) => default(SyntaxTrivia));
return newRoot;
}
示例3: RemoveComment
private Task<Document> RemoveComment(Document document, SyntaxNode root, SyntaxTrivia comment, CancellationToken cancellationToken)
{
var line = comment.Token.LeadingTrivia.SplitLines().First(n => n.Contains(comment));
var newRoot = root.ReplaceTrivia(line, (_, __) => default(SyntaxTrivia));
var newDocument = document.WithSyntaxRoot(newRoot);
return Task.FromResult(newDocument);
}
示例4: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxTrivia trivia)
{
string text = trivia.ToFullString();
if (!text.StartsWith("//"))
{
return Task.FromResult(document);
}
string correctedText = "// " + text.Substring(2).TrimStart(' ');
SyntaxTrivia corrected = SyntaxFactory.Comment(correctedText).WithoutFormatting();
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceTrivia(trivia, corrected));
return Task.FromResult(updatedDocument);
}
示例5: ProcessTriviaList
private static SyntaxNode ProcessTriviaList(SyntaxNode syntaxRoot, SyntaxTriviaList triviaList, SyntaxTrivia singleLineComment)
{
var index = triviaList.IndexOf(singleLineComment);
if (index == -1)
{
return null;
}
index--;
while (index >= 0)
{
switch (triviaList[index].Kind())
{
case SyntaxKind.WhitespaceTrivia:
index--;
break;
default:
return syntaxRoot.ReplaceTrivia(triviaList[index], new[] { triviaList[index], SyntaxFactory.CarriageReturnLineFeed });
}
}
return syntaxRoot.ReplaceTrivia(triviaList[0], new[] { SyntaxFactory.CarriageReturnLineFeed, triviaList[0] });
}
示例6: ProcessTriviaList
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));
}
示例7: RemoveComments
private Task<Document> RemoveComments(Document document, SyntaxNode root, SyntaxTrivia comment, CancellationToken cancellationToken)
{
var startCommentText = comment.ToFullString().Substring(2).Trim(); // skip "//"
var endCommentText = Regex.Replace(startCommentText, @"\bstart\b", "end", RegexOptions.IgnoreCase);
var endCommentRegex = new Regex(string.Join(@"\s+", Regex.Split(endCommentText, @"\s+").Select(n => Regex.Escape(n))), RegexOptions.IgnoreCase);
var lines = new List<SyntaxTrivia>();
var node = comment.Token.Parent.AncestorsAndSelf()
.Where(n => n is BlockSyntax || n is BaseTypeDeclarationSyntax)
.First();
var tokens = node.ChildNodesAndTokens();
foreach (var line in tokens.Where(t => t.HasLeadingTrivia).SelectMany(t => t.GetLeadingTrivia().SplitLines()).SkipWhile(n => !n.Contains(comment)))
{
//skip outside of declaration.
if (line[0].SpanStart < node.SpanStart || line[0].SpanStart > node.Span.End) continue;
// find line contains SingleLineComment only.
foreach (var firstTrivia in line.SkipWhile(n => n.Kind() == SyntaxKind.WhitespaceTrivia).Take(1))
{
if (firstTrivia.Kind() == SyntaxKind.SingleLineCommentTrivia)
{
lines.AddRange(line);
var commentText = firstTrivia.ToFullString();
if (endCommentRegex.IsMatch(commentText))
{
goto EOL;
}
}
}
}
EOL:
var newRoot = root.ReplaceTrivia(lines, (_, __) => default(SyntaxTrivia));
var newDocument = document.WithSyntaxRoot(newRoot);
return Task.FromResult(newDocument);
}