本文整理汇总了C#中StatementSyntax.GetFirstToken方法的典型用法代码示例。如果您正苦于以下问题:C# StatementSyntax.GetFirstToken方法的具体用法?C# StatementSyntax.GetFirstToken怎么用?C# StatementSyntax.GetFirstToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StatementSyntax
的用法示例。
在下文中一共展示了StatementSyntax.GetFirstToken方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddEmbeddedStatementsIndentationOperation
private void AddEmbeddedStatementsIndentationOperation(List<IndentBlockOperation> list, StatementSyntax statement)
{
var firstToken = statement.GetFirstToken(includeZeroWidth: true);
var lastToken = statement.GetLastToken(includeZeroWidth: true);
if (lastToken.IsMissing)
{
// embedded statement is not done, consider following as part of embedded statement
AddIndentBlockOperation(list, firstToken, lastToken);
}
else
{
// embedded statement is done
AddIndentBlockOperation(list, firstToken, lastToken, TextSpan.FromBounds(firstToken.FullSpan.Start, lastToken.FullSpan.End));
}
}
示例2: CheckChildStatement
private static void CheckChildStatement(SyntaxNodeAnalysisContext context, SyntaxNode node, StatementSyntax childStatement)
{
if (childStatement == null || childStatement.IsMissing)
{
return;
}
if (childStatement is BlockSyntax)
{
// BlockSyntax child statements are handled by HandleBlock
return;
}
// We are only interested in the first instance of this violation on a line.
if (!node.GetFirstToken().IsFirstInLine())
{
return;
}
// We are only interested in the case where statement and childStatement start on the same line. Use
// IsFirstInLine to detect this condition easily.
SyntaxToken firstChildToken = childStatement.GetFirstToken();
if (firstChildToken.IsMissingOrDefault() || firstChildToken.IsFirstInLine())
{
return;
}
if (!context.IsAnalyzerSuppressed(SA1519CurlyBracketsMustNotBeOmittedFromMultiLineChildStatement.DiagnosticId))
{
// diagnostics for multi-line statements is handled by SA1519, as long as it's not suppressed
FileLinePositionSpan lineSpan = childStatement.GetLineSpan();
if (lineSpan.StartLinePosition.Line != lineSpan.EndLinePosition.Line)
{
return;
}
}
context.ReportDiagnostic(Diagnostic.Create(Descriptor, childStatement.GetLocation()));
}
示例3: GetFirstIncludedToken
private static SyntaxToken GetFirstIncludedToken(StatementSyntax statement, bool inRecursiveCall = false)
{
Debug.Assert(statement != null);
switch (statement.Kind)
{
case SyntaxKind.Block:
return ((BlockSyntax)statement).OpenBraceToken;
case SyntaxKind.BreakStatement:
return ((BreakStatementSyntax)statement).BreakKeyword;
case SyntaxKind.CheckedStatement:
case SyntaxKind.UncheckedStatement:
return ((CheckedStatementSyntax)statement).Keyword;
case SyntaxKind.ContinueStatement:
return ((ContinueStatementSyntax)statement).ContinueKeyword;
case SyntaxKind.ExpressionStatement:
case SyntaxKind.LocalDeclarationStatement:
return statement.GetFirstToken();
case SyntaxKind.DoStatement:
return ((DoStatementSyntax)statement).DoKeyword;
case SyntaxKind.EmptyStatement:
return default(SyntaxToken); //The caller will have to check for this.
case SyntaxKind.FixedStatement:
return ((FixedStatementSyntax)statement).FixedKeyword;
case SyntaxKind.ForEachStatement:
// NB: iteration variable is only in scope in body.
ForEachStatementSyntax forEachSyntax = (ForEachStatementSyntax)statement;
if (inRecursiveCall)
{
return forEachSyntax.ForEachKeyword;
}
return GetFirstIncludedToken(forEachSyntax.Statement, inRecursiveCall: true);
case SyntaxKind.ForStatement:
// Section 8.8.3 of the spec says that the scope of the loop variable starts at
// its declaration. If it's not there, then the scope we are interested in is
// the loop body.
ForStatementSyntax forSyntax = (ForStatementSyntax)statement;
if (inRecursiveCall)
{
return forSyntax.ForKeyword;
}
VariableDeclarationSyntax declOpt = forSyntax.Declaration;
return declOpt == null ? GetFirstIncludedToken(forSyntax.Statement, inRecursiveCall: true) : declOpt.GetFirstToken();
case SyntaxKind.GotoDefaultStatement:
case SyntaxKind.GotoCaseStatement:
case SyntaxKind.GotoStatement:
return ((GotoStatementSyntax)statement).GotoKeyword;
case SyntaxKind.IfStatement:
return ((IfStatementSyntax)statement).IfKeyword;
case SyntaxKind.LabeledStatement:
return ((LabeledStatementSyntax)statement).Identifier;
case SyntaxKind.LockStatement:
return ((LockStatementSyntax)statement).LockKeyword;
case SyntaxKind.ReturnStatement:
return ((ReturnStatementSyntax)statement).ReturnKeyword;
case SyntaxKind.SwitchStatement:
return ((SwitchStatementSyntax)statement).OpenBraceToken;
case SyntaxKind.ThrowStatement:
return ((ThrowStatementSyntax)statement).ThrowKeyword;
case SyntaxKind.TryStatement:
return ((TryStatementSyntax)statement).TryKeyword;
case SyntaxKind.UnsafeStatement:
return ((UnsafeStatementSyntax)statement).UnsafeKeyword;
case SyntaxKind.UsingStatement:
return ((UsingStatementSyntax)statement).UsingKeyword;
case SyntaxKind.WhileStatement:
return ((WhileStatementSyntax)statement).WhileKeyword;
case SyntaxKind.YieldReturnStatement:
case SyntaxKind.YieldBreakStatement:
return ((YieldStatementSyntax)statement).YieldKeyword;
default:
throw ExceptionUtilities.UnexpectedValue(statement.Kind);
}
}
示例4: GetFirstIncludedToken
private static SyntaxToken GetFirstIncludedToken(StatementSyntax statement)
{
Debug.Assert(statement != null);
switch (statement.Kind())
{
case SyntaxKind.Block:
return ((BlockSyntax)statement).OpenBraceToken;
case SyntaxKind.BreakStatement:
return ((BreakStatementSyntax)statement).BreakKeyword;
case SyntaxKind.CheckedStatement:
case SyntaxKind.UncheckedStatement:
return ((CheckedStatementSyntax)statement).Keyword;
case SyntaxKind.ContinueStatement:
return ((ContinueStatementSyntax)statement).ContinueKeyword;
case SyntaxKind.ExpressionStatement:
case SyntaxKind.LocalDeclarationStatement:
case SyntaxKind.DeconstructionDeclarationStatement:
return statement.GetFirstToken();
case SyntaxKind.DoStatement:
return ((DoStatementSyntax)statement).DoKeyword;
case SyntaxKind.EmptyStatement:
return default(SyntaxToken); //The caller will have to check for this.
case SyntaxKind.FixedStatement:
return ((FixedStatementSyntax)statement).FixedKeyword;
case SyntaxKind.ForEachStatement:
case SyntaxKind.ForEachComponentStatement:
return ((CommonForEachStatementSyntax)statement).OpenParenToken.GetNextToken();
case SyntaxKind.ForStatement:
return ((ForStatementSyntax)statement).OpenParenToken.GetNextToken();
case SyntaxKind.GotoDefaultStatement:
case SyntaxKind.GotoCaseStatement:
case SyntaxKind.GotoStatement:
return ((GotoStatementSyntax)statement).GotoKeyword;
case SyntaxKind.IfStatement:
return ((IfStatementSyntax)statement).IfKeyword;
case SyntaxKind.LabeledStatement:
return ((LabeledStatementSyntax)statement).Identifier;
case SyntaxKind.LockStatement:
return ((LockStatementSyntax)statement).LockKeyword;
case SyntaxKind.ReturnStatement:
return ((ReturnStatementSyntax)statement).ReturnKeyword;
case SyntaxKind.SwitchStatement:
return ((SwitchStatementSyntax)statement).Expression.GetFirstToken();
case SyntaxKind.ThrowStatement:
return ((ThrowStatementSyntax)statement).ThrowKeyword;
case SyntaxKind.TryStatement:
return ((TryStatementSyntax)statement).TryKeyword;
case SyntaxKind.UnsafeStatement:
return ((UnsafeStatementSyntax)statement).UnsafeKeyword;
case SyntaxKind.UsingStatement:
return ((UsingStatementSyntax)statement).UsingKeyword;
case SyntaxKind.WhileStatement:
return ((WhileStatementSyntax)statement).WhileKeyword;
case SyntaxKind.YieldReturnStatement:
case SyntaxKind.YieldBreakStatement:
return ((YieldStatementSyntax)statement).YieldKeyword;
case SyntaxKind.LocalFunctionStatement:
return statement.GetFirstToken();
default:
throw ExceptionUtilities.UnexpectedValue(statement.Kind());
}
}
示例5: ReformatStatement
private static StatementSyntax ReformatStatement(Document document, StatementSyntax statement)
{
var indentationOptions = IndentationOptions.FromDocument(document);
var parentIndentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, GetStatementParent(statement.Parent));
// use one additional step of indentation for lambdas / anonymous methods
switch (statement.Parent.Kind())
{
case SyntaxKind.AnonymousMethodExpression:
case SyntaxKind.SimpleLambdaExpression:
case SyntaxKind.ParenthesizedLambdaExpression:
parentIndentationLevel++;
break;
}
var statementIndentationString = IndentationHelper.GenerateIndentationString(indentationOptions, parentIndentationLevel + 1);
var newFirstTokenLeadingTrivia = statement.GetFirstToken().LeadingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.Whitespace(statementIndentationString));
var newLastTokenTrailingTrivia = statement.GetLastToken().TrailingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.CarriageReturnLineFeed);
var firstToken = statement.GetFirstToken().WithLeadingTrivia(newFirstTokenLeadingTrivia);
var lastToken = statement.GetLastToken().WithTrailingTrivia(newLastTokenTrailingTrivia);
return statement.ReplaceTokens(
new[] { statement.GetFirstToken(), statement.GetLastToken() },
(originalToken, rewrittenToken) =>
{
if (originalToken == statement.GetFirstToken())
{
return firstToken;
}
else if (originalToken == statement.GetLastToken())
{
return lastToken;
}
else
{
return rewrittenToken;
}
});
}
示例6: ReformatStatementAndParent
private static SyntaxNode ReformatStatementAndParent(Document document, SyntaxNode syntaxRoot, StatementSyntax statement)
{
var parentLastToken = statement.GetFirstToken().GetPreviousToken();
var parentEndLine = parentLastToken.GetEndLine();
var statementStartLine = statement.GetFirstToken().GetLine();
var newParentLastToken = parentLastToken;
if (parentEndLine == statementStartLine)
{
var newTrailingTrivia = parentLastToken.TrailingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.CarriageReturnLineFeed);
newParentLastToken = newParentLastToken.WithTrailingTrivia(newTrailingTrivia);
}
var parentNextToken = statement.GetLastToken().GetNextToken();
var nextTokenLine = parentNextToken.GetLine();
var statementCloseLine = statement.GetLastToken().GetEndLine();
var newParentNextToken = parentNextToken;
if (nextTokenLine == statementCloseLine)
{
var indentationOptions = IndentationOptions.FromDocument(document);
var parentIndentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, GetStatementParent(statement.Parent));
var indentationString = IndentationHelper.GenerateIndentationString(indentationOptions, parentIndentationLevel);
newParentNextToken = newParentNextToken.WithLeadingTrivia(SyntaxFactory.Whitespace(indentationString));
}
var newStatement = ReformatStatement(document, statement);
var newSyntaxRoot = syntaxRoot.ReplaceSyntax(
new[] { statement },
(originalNode, rewrittenNode) => originalNode == statement ? newStatement : rewrittenNode,
new[] { parentLastToken, parentNextToken },
(originalToken, rewrittenToken) =>
{
if (originalToken == parentLastToken)
{
return newParentLastToken;
}
else if (originalToken == parentNextToken)
{
return newParentNextToken;
}
else
{
return rewrittenToken;
}
},
Enumerable.Empty<SyntaxTrivia>(),
(originalTrivia, rewrittenTrivia) => rewrittenTrivia);
return newSyntaxRoot.WithoutFormatting();
}