本文整理汇总了C#中StatementSyntax.Ancestors方法的典型用法代码示例。如果您正苦于以下问题:C# StatementSyntax.Ancestors方法的具体用法?C# StatementSyntax.Ancestors怎么用?C# StatementSyntax.Ancestors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StatementSyntax
的用法示例。
在下文中一共展示了StatementSyntax.Ancestors方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReplaceLocationAsync
//replace an incorrect diagnostic location statement
private async Task<Document> ReplaceLocationAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string ifStatementIdentifier = (methodDeclaration.Body.Statements[0] as LocalDeclarationStatementSyntax).Declaration.Variables[0].Identifier.Text;
string spanIdentifier = (methodDeclaration.Body.Statements[6] as LocalDeclarationStatementSyntax).Declaration.Variables[0].Identifier.Text;
SyntaxNode location = CodeFixNodeCreator.CreateLocation(generator, ifStatementIdentifier, spanIdentifier);
return await ReplaceNode(declaration, location.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// Uses the span created above to create a location for the diagnostic squiggle to appear within the syntax tree passed in as an argument").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例2: ReplaceEndSpanAsync
//replace an incorrect end span statement
private async Task<Document> ReplaceEndSpanAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string identifierString = (methodDeclaration.Body.Statements[3] as LocalDeclarationStatementSyntax).Declaration.Variables[0].Identifier.Text;
SyntaxNode endSpan = CodeFixNodeCreator.CreateEndOrStartSpan(generator, identifierString, "endDiagnosticSpan");
return await ReplaceNode(declaration, endSpan.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// Determines the end of the span of the diagnostic that will be reported").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例3: ReplaceSpanAsync
//replaces an incorrect diagnostic span statement
private async Task<Document> ReplaceSpanAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string startIdentifier = (methodDeclaration.Body.Statements[4] as LocalDeclarationStatementSyntax).Declaration.Variables[0].Identifier.Text;
string endIdentifier = (methodDeclaration.Body.Statements[5] as LocalDeclarationStatementSyntax).Declaration.Variables[0].Identifier.Text;
SyntaxNode span = CodeFixNodeCreator.CreateSpan(generator, startIdentifier, endIdentifier);
return await ReplaceNode(declaration, span.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// The span is the range of integers that define the position of the characters the red squiggle will underline").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例4: ReplaceDiagnosticAsync
private async Task<Document> ReplaceDiagnosticAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
var methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
var classDeclaration = methodDeclaration.Ancestors().OfType<ClassDeclarationSyntax>().First();
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string locationName = (methodDeclaration.Body.Statements[7] as LocalDeclarationStatementSyntax).Declaration.Variables[0].Identifier.Text;
string ruleName = CodeFixNodeCreator.GetFirstRuleName(classDeclaration);
var diagnostic = CodeFixNodeCreator.CreateDiagnostic(generator, locationName, ruleName);
return await ReplaceNode(declaration, diagnostic.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// Holds the diagnostic and all necessary information to be reported").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例5: ReplaceOpenParenAsync
//replaces an incorrect open parenthsis statement
private async Task<Document> ReplaceOpenParenAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string expressionString = (methodDeclaration.Body.Statements[0] as LocalDeclarationStatementSyntax).Declaration.Variables[0].Identifier.Text;
SyntaxNode openParen = CodeFixNodeCreator.CreateOpenParen(generator, expressionString);
return await ReplaceNode(declaration, openParen.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// Extracts the opening parenthesis of the if-statement condition").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例6: ReplaceStartSpanAsync
// replaces the start span statement
private async Task<Document> ReplaceStartSpanAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string identifierString = CodeFixHelper.GetIfKeywordName(methodDeclaration.Body);
SyntaxNode startSpan = CodeFixHelper.CreateEndOrStartSpan(generator, identifierString, "startDiagnosticSpan");
return await ReplaceNode(declaration, startSpan.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Determines the start of the span of the diagnostic that will be reported, ie the start of the squiggle").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例7: ReplaceDiagnosticReportAsync
private async Task<Document> ReplaceDiagnosticReportAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
var methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string argumentName = (methodDeclaration.Body.Statements[8] as LocalDeclarationStatementSyntax).Declaration.Variables[0].Identifier.Text;
string contextName = (methodDeclaration.ParameterList.Parameters[0].Identifier.Text);
SyntaxNode diagnosticReport = CodeFixNodeCreator.CreateDiagnosticReport(generator, argumentName, contextName);
return await ReplaceNode(declaration, diagnosticReport.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// Sends diagnostic information to the IDE to be shown to the user").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例8: RemoveStatement
// removes the provided statement from the method that it is in
protected internal static SyntaxNode RemoveStatement(StatementSyntax statement)
{
MethodDeclarationSyntax initializeDeclaration = statement.Ancestors().OfType<MethodDeclarationSyntax>().First();
MethodDeclarationSyntax newInitializeDeclaration = initializeDeclaration.RemoveNode(statement, 0);
return newInitializeDeclaration as SyntaxNode;
}
示例9: IncorrectIfAsync
// replaces the if statement variable
private async Task<Document> IncorrectIfAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
MethodDeclarationSyntax methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string name = CodeFixHelper.GetContextParameter(methodDeclaration);
SyntaxNode ifStatement = CodeFixHelper.IfHelper(generator, name);
return await ReplaceNode(declaration, ifStatement.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// The SyntaxNode found by the Initialize method should be cast to the expected type. Here, this type is IfStatementSyntax").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例10: ReplaceDiagnosticReportAsync
// replaces the diagnostic report statement
private async Task<Document> ReplaceDiagnosticReportAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
var methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string argumentName = CodeFixHelper.GetDiagnosticName(methodDeclaration);
string contextName = CodeFixHelper.GetContextParameter(methodDeclaration);
SyntaxNode diagnosticReport = CodeFixHelper.CreateDiagnosticReport(generator, argumentName, contextName);
return await ReplaceNode(declaration, diagnosticReport.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Sends diagnostic information to the IDE to be shown to the user").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例11: InvalidStatementAsync
// removes the invalid statement from the method
private async Task<Document> InvalidStatementAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxNode newInitializeDeclaration = CodeFixHelper.RemoveStatement(declaration);
return await ReplaceNode(declaration.Ancestors().OfType<MethodDeclarationSyntax>().First(), newInitializeDeclaration, document);
}
示例12: ReplaceDiagnosticAsync
// replace the diagnostic creation statement
private async Task<Document> ReplaceDiagnosticAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
var methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
var classDeclaration = methodDeclaration.Ancestors().OfType<ClassDeclarationSyntax>().First();
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string locationName = CodeFixHelper.GetLocationName(methodDeclaration);
string ruleName = CodeFixHelper.GetFirstRuleName(classDeclaration);
if (ruleName == null)
{
return document;
}
SyntaxNode diagnostic = CodeFixHelper.CreateDiagnostic(generator, locationName, ruleName);
return await ReplaceNode(declaration, diagnostic.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Holds the diagnostic and all necessary information to be reported").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document);
}
示例13: ReplaceOpenParenAsync
// replaces the open paren statement
private async Task<Document> ReplaceOpenParenAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
MethodDeclarationSyntax methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string expressionString = CodeFixHelper.GetIfStatementName(methodDeclaration.Body);
SyntaxNode openParen = CodeFixHelper.CreateOpenParen(generator, expressionString);
return await ReplaceNode(declaration, openParen.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Extracts the opening parenthesis of the if-statement condition").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document).ConfigureAwait(false);
}
示例14: ReplaceLocationAsync
// replaces the location statement
private async Task<Document> ReplaceLocationAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
MethodDeclarationSyntax methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string ifStatementIdentifier = CodeFixHelper.GetIfStatementName(methodDeclaration.Body);
string spanIdentifier = CodeFixHelper.GetSpanName(methodDeclaration);
SyntaxNode location = CodeFixHelper.CreateLocation(generator, ifStatementIdentifier, spanIdentifier);
return await ReplaceNode(declaration, location.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Uses the span created above to create a location for the diagnostic squiggle to appear within the syntax tree passed in as an argument").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document).ConfigureAwait(false);
}
示例15: ReplaceSpanAsync
// replaces the span statement
private async Task<Document> ReplaceSpanAsync(Document document, StatementSyntax declaration, CancellationToken c)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
MethodDeclarationSyntax methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string startIdentifier = CodeFixHelper.GetStartSpanName(methodDeclaration);
string endIdentifier = CodeFixHelper.GetEndSpanName(methodDeclaration);
SyntaxNode span = CodeFixHelper.CreateSpan(generator, startIdentifier, endIdentifier);
return await ReplaceNode(declaration, span.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// The span is the range of integers that define the position of the characters the red squiggle will underline").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document).ConfigureAwait(false);
}