本文整理汇总了C#中StatementSyntax.GetTrailingTrivia方法的典型用法代码示例。如果您正苦于以下问题:C# StatementSyntax.GetTrailingTrivia方法的具体用法?C# StatementSyntax.GetTrailingTrivia怎么用?C# StatementSyntax.GetTrailingTrivia使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StatementSyntax
的用法示例。
在下文中一共展示了StatementSyntax.GetTrailingTrivia方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateUsingStatement
private static UsingStatementSyntax CreateUsingStatement(StatementSyntax statement, BlockSyntax block)
{
return SyntaxFactory.UsingStatement(block)
.WithLeadingTrivia(statement.GetLeadingTrivia())
.WithTrailingTrivia(statement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
}
示例2: AddWatchNode
SyntaxNode AddWatchNode(StatementSyntax node, ExpressionSyntax expr)
{
var id = Guid.NewGuid().ToString();
var c = node.GetTrailingTrivia().First(t => t.Kind() == SyntaxKind.SingleLineCommentTrivia && t.ToString().StartsWith("//="));
var p = c.GetLocation().GetLineSpan().StartLinePosition;
var wv = new WatchVariable {
Id = id,
Expression = expr.ToString(),
ExplicitExpression = "",
FilePath = path,
FileLine = p.Line + 1, // 0-based index
FileColumn = p.Character + 1, // 0-based index
};
WatchVariables.Add(wv);
var wi = GetWatchInstrument(id, expr);
// creating a block and removing the open/close braces is a bit of a hack but
// lets us replace one node with two...
return
SyntaxFactory
.Block(node, wi)
.WithOpenBraceToken(SyntaxFactory.MissingToken(SyntaxKind.OpenBraceToken))
.WithCloseBraceToken(SyntaxFactory.MissingToken(SyntaxKind.CloseBraceToken))
.WithTrailingTrivia(SyntaxFactory.EndOfLine("\r\n"));
}
示例3: CreateNewBlockParent
private static BlockSyntax CreateNewBlockParent(StatementSyntax statement, SemanticModel semanticModel, ObjectCreationExpressionSyntax objectCreationExpression, ISymbol variableSymbol)
{
var blockParent = statement.FirstAncestorOrSelf<BlockSyntax>();
var assignmentExpressions = ObjectInitializerAnalyzer.FindAssignmentExpressions(semanticModel, statement, variableSymbol);
var newBlockParent = SyntaxFactory.Block()
.WithLeadingTrivia(blockParent.GetLeadingTrivia())
.WithTrailingTrivia(blockParent.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
for (int i = 0; i < blockParent.Statements.Count; i++)
{
var blockStatement = blockParent.Statements[i];
if (blockStatement.Equals(statement))
{
var initializationExpressions = new List<AssignmentExpressionSyntax>();
foreach (var expressionStatement in assignmentExpressions)
{
var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax;
var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax;
var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax;
var newAssignmentExpression = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, propertyIdentifier, assignmentExpression.Right);
initializationExpressions.Add(newAssignmentExpression);
}
if (objectCreationExpression.Initializer != null)
{
var existentInitilizers = objectCreationExpression.Initializer.Expressions.Cast<AssignmentExpressionSyntax>()
.Where(ae =>
{
var propertyIdentifier = ae.Left.ToFullString().Trim();
return initializationExpressions.All(ie => ie.Left.ToFullString().Trim() != propertyIdentifier);
})
.Select(ae => ae.WithoutTrivia())
.ToList();
initializationExpressions.InsertRange(0, existentInitilizers);
}
var initializers = SyntaxFactory.SeparatedList<ExpressionSyntax>(initializationExpressions);
var newObjectCreationExpression = objectCreationExpression.WithInitializer(
SyntaxFactory.InitializerExpression(
SyntaxKind.ObjectInitializerExpression,
SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.OpenBraceToken, SyntaxFactory.ParseTrailingTrivia(Environment.NewLine)),
initializers,
SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.CloseBraceToken, SyntaxFactory.ParseTrailingTrivia(""))
))
.WithLeadingTrivia(objectCreationExpression.GetLeadingTrivia())
.WithTrailingTrivia(objectCreationExpression.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
if (newObjectCreationExpression.ArgumentList?.Arguments.Count == 0)
{
newObjectCreationExpression = newObjectCreationExpression.WithArgumentList(null);
}
var newLocalDeclarationStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression)
.WithLeadingTrivia(statement.GetLeadingTrivia())
.WithTrailingTrivia(statement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
newBlockParent = newBlockParent.AddStatements(newLocalDeclarationStatement);
i += initializationExpressions.Count;
}
else
{
newBlockParent = newBlockParent.AddStatements(blockStatement
.WithLeadingTrivia(blockStatement.GetLeadingTrivia())
.WithTrailingTrivia(blockStatement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation));
}
}
return newBlockParent;
}
示例4: CreateNewBlockParent
private static BlockSyntax CreateNewBlockParent(StatementSyntax statement, SemanticModel semanticModel, ObjectCreationExpressionSyntax objectCreationExpression, ISymbol variableSymbol)
{
var blockParent = statement.FirstAncestorOrSelf<BlockSyntax>();
var assignmentExpressions = ObjectInitializerAnalyzer.FindAssignmentExpressions(semanticModel, statement, variableSymbol);
var newBlockParent = SyntaxFactory.Block()
.WithLeadingTrivia(blockParent.GetLeadingTrivia())
.WithTrailingTrivia(blockParent.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
var newAssignmentExpressions = new List<ExpressionStatementSyntax>();
for (int i = 0; i < blockParent.Statements.Count; i++)
{
var blockStatement = blockParent.Statements[i];
if (blockStatement.Equals(statement))
{
var initializationExpressions = new List<AssignmentExpressionSyntax>();
foreach (var expressionStatement in assignmentExpressions)
{
var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax;
var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax;
var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax;
initializationExpressions.Add(SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, propertyIdentifier, assignmentExpression.Right));
}
var initializers = SyntaxFactory.SeparatedList<ExpressionSyntax>(initializationExpressions);
var newObjectCreationExpression = objectCreationExpression.WithInitializer(
SyntaxFactory.InitializerExpression(
SyntaxKind.ObjectInitializerExpression,
SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.OpenBraceToken, SyntaxFactory.ParseTrailingTrivia("\n")),
initializers,
SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.CloseBraceToken, SyntaxFactory.ParseTrailingTrivia(""))
))
.WithLeadingTrivia(objectCreationExpression.GetLeadingTrivia())
.WithTrailingTrivia(objectCreationExpression.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
var newLocalDeclarationStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression)
.WithLeadingTrivia(statement.GetLeadingTrivia())
.WithTrailingTrivia(statement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
newBlockParent = newBlockParent.AddStatements(newLocalDeclarationStatement);
i += initializationExpressions.Count;
}
else
{
newBlockParent = newBlockParent.AddStatements(blockStatement
.WithLeadingTrivia(blockStatement.GetLeadingTrivia())
.WithTrailingTrivia(blockStatement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation));
}
}
return newBlockParent;
}
示例5: Trivia
public static StatementSyntax Trivia(this StatementSyntax node, StatementSyntax that)
{
return node.WithLeadingTrivia(that.GetLeadingTrivia()).WithTrailingTrivia(that.GetTrailingTrivia());
}
示例6: CreateNewBlockParent
private static BlockSyntax CreateNewBlockParent(StatementSyntax statement, SemanticModel semanticModel, ObjectCreationExpressionSyntax objectCreationExpression, ISymbol variableSymbol)
{
var blockParent = statement.FirstAncestorOrSelf<BlockSyntax>();
var assignmentExpressions = ObjectInitializerAnalyzer.FindAssignmentExpressions(semanticModel, statement, variableSymbol);
var newBlockParent = SyntaxFactory.Block()
.WithLeadingTrivia(blockParent.GetLeadingTrivia())
.WithTrailingTrivia(blockParent.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
for (int i = 0; i < blockParent.Statements.Count; i++)
{
var blockStatement = blockParent.Statements[i];
if (blockStatement.Equals(statement))
{
var initializationExpressions = new List<AssignmentExpressionSyntax>();
foreach (var expressionStatement in assignmentExpressions)
{
var assignmentExpression = expressionStatement.Expression as AssignmentExpressionSyntax;
var memberAccess = assignmentExpression.Left as MemberAccessExpressionSyntax;
var propertyIdentifier = memberAccess.Name as IdentifierNameSyntax;
var newAssignmentExpression = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, propertyIdentifier, assignmentExpression.Right);
initializationExpressions.Add(newAssignmentExpression.WithTriviaFrom(expressionStatement));
}
if (objectCreationExpression.Initializer != null)
{
var existentInitilizers = objectCreationExpression.Initializer.Expressions.Cast<AssignmentExpressionSyntax>()
.Where(ae =>
{
var propertyIdentifier = ae.Left.ToFullString().Trim();
return initializationExpressions.All(ie => ie.Left.ToFullString().Trim() != propertyIdentifier);
})
.Select(ae => ae.WithoutTrivia())
.ToList();
initializationExpressions.InsertRange(0, existentInitilizers);
}
// Trailing trivia will be added before the separator if a simple separator list is used. This builds the separator token for expression
// such that the trailing trivia from the original expression is added after the comma on the same line.
var initializerSeparators = initializationExpressions.Take(initializationExpressions.Count - 1).Select(expr => SyntaxFactory.Token(SyntaxKind.CommaToken).WithTrailingTrivia(expr.GetTrailingTrivia())).ToList();
var lastInitializer = initializationExpressions.Last(); // Preserve the last initializer before rebuilding the list.
// Get all but the last initializer without the trailing trivia. Trivia will be added after the separator from the list above.
initializationExpressions = initializationExpressions.Take(initializationExpressions.Count - 1).Select(expr => expr.WithoutTrailingTrivia()).ToList();
initializationExpressions.Add(lastInitializer); // Add the last initializer with all of its trivia.
var initializers = SyntaxFactory.SeparatedList<ExpressionSyntax>(initializationExpressions, initializerSeparators);
var newObjectCreationExpression = objectCreationExpression.WithInitializer(
SyntaxFactory.InitializerExpression(
SyntaxKind.ObjectInitializerExpression,
SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.OpenBraceToken, SyntaxFactory.ParseTrailingTrivia(Environment.NewLine)),
initializers,
SyntaxFactory.Token(SyntaxFactory.ParseLeadingTrivia(" "), SyntaxKind.CloseBraceToken, SyntaxFactory.ParseTrailingTrivia(""))
))
.WithLeadingTrivia(objectCreationExpression.GetLeadingTrivia())
.WithTrailingTrivia(objectCreationExpression.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
if (newObjectCreationExpression.ArgumentList?.Arguments.Count == 0)
{
newObjectCreationExpression = newObjectCreationExpression.WithArgumentList(null);
}
var newLocalDeclarationStatement = statement.ReplaceNode(objectCreationExpression, newObjectCreationExpression)
.WithLeadingTrivia(statement.GetLeadingTrivia())
.WithTrailingTrivia(statement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
newBlockParent = newBlockParent.AddStatements(newLocalDeclarationStatement);
i += initializationExpressions.Count;
}
else
{
newBlockParent = newBlockParent.AddStatements(blockStatement
.WithLeadingTrivia(blockStatement.GetLeadingTrivia())
.WithTrailingTrivia(blockStatement.GetTrailingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation));
}
}
return newBlockParent;
}