本文整理汇总了C#中SyntaxNode.Kind方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.Kind方法的具体用法?C# SyntaxNode.Kind怎么用?C# SyntaxNode.Kind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.Kind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsLambda
/// <summary>
/// Returns true if the specified node represents a lambda.
/// </summary>
public static bool IsLambda(SyntaxNode node)
{
switch (node.Kind())
{
case SyntaxKind.ParenthesizedLambdaExpression:
case SyntaxKind.SimpleLambdaExpression:
case SyntaxKind.AnonymousMethodExpression:
case SyntaxKind.LetClause:
case SyntaxKind.WhereClause:
case SyntaxKind.AscendingOrdering:
case SyntaxKind.DescendingOrdering:
case SyntaxKind.JoinClause:
case SyntaxKind.GroupClause:
case SyntaxKind.LocalFunctionStatement:
return true;
case SyntaxKind.SelectClause:
var selectClause = (SelectClauseSyntax)node;
return !IsReducedSelectOrGroupByClause(selectClause, selectClause.Expression);
case SyntaxKind.FromClause:
// The first from clause of a query expression is not a lambda.
return !node.Parent.IsKind(SyntaxKind.QueryExpression);
}
return false;
}
示例2: GetUpdatedDocumentAsync
internal override Task<Document> GetUpdatedDocumentAsync(Document document, SemanticModel model, SyntaxNode root, SyntaxNode nodeToFix, Diagnostic diagnostic, CancellationToken cancellationToken)
{
// if nothing can be fixed, return the unchanged node
var newRoot = root;
var kind = nodeToFix.Kind();
var syntaxFactoryService = document.Project.LanguageServices.GetService<SyntaxGenerator>();
switch (kind)
{
case SyntaxKind.Argument:
// StringComparison.CurrentCulture => StringComparison.Ordinal
// StringComparison.CurrentCultureIgnoreCase => StringComparison.OrdinalIgnoreCase
var argument = (ArgumentSyntax)nodeToFix;
var memberAccess = argument.Expression as MemberAccessExpressionSyntax;
if (memberAccess != null)
{
// preserve the "IgnoreCase" suffix if present
bool isIgnoreCase = memberAccess.Name.GetText().ToString().EndsWith(CA1309DiagnosticAnalyzer.IgnoreCaseText);
var newOrdinalText = isIgnoreCase ? CA1309DiagnosticAnalyzer.OrdinalIgnoreCaseText : CA1309DiagnosticAnalyzer.OrdinalText;
var newIdentifier = syntaxFactoryService.IdentifierName(newOrdinalText);
var newMemberAccess = memberAccess.WithName((SimpleNameSyntax)newIdentifier).WithAdditionalAnnotations(Formatter.Annotation);
newRoot = root.ReplaceNode(memberAccess, newMemberAccess);
}
break;
case SyntaxKind.IdentifierName:
// string.Equals(a, b) => string.Equals(a, b, StringComparison.Ordinal)
// string.Compare(a, b) => string.Compare(a, b, StringComparison.Ordinal)
var identifier = (IdentifierNameSyntax)nodeToFix;
var invokeParent = identifier.Parent?.FirstAncestorOrSelf<InvocationExpressionSyntax>();
if (invokeParent != null)
{
var methodSymbol = model.GetSymbolInfo(identifier, cancellationToken).Symbol as IMethodSymbol;
if (methodSymbol != null && CanAddStringComparison(methodSymbol))
{
// append a new StringComparison.Ordinal argument
var newArg = syntaxFactoryService.Argument(CreateOrdinalMemberAccess(syntaxFactoryService, model))
.WithAdditionalAnnotations(Formatter.Annotation);
var newInvoke = invokeParent.AddArgumentListArguments((ArgumentSyntax)newArg).WithAdditionalAnnotations(Formatter.Annotation);
newRoot = root.ReplaceNode(invokeParent, newInvoke);
}
}
break;
case SyntaxKind.EqualsExpression:
case SyntaxKind.NotEqualsExpression:
// "a == b" => "string.Equals(a, b, StringComparison.Ordinal)"
// "a != b" => "!string.Equals(a, b, StringComparison.Ordinal)"
var binaryExpression = (BinaryExpressionSyntax)nodeToFix;
var invocation = CreateEqualsExpression(syntaxFactoryService, model, binaryExpression.Left, binaryExpression.Right, kind == SyntaxKind.EqualsExpression).WithAdditionalAnnotations(Formatter.Annotation);
newRoot = root.ReplaceNode(nodeToFix, invocation);
break;
}
if (newRoot == root)
{
return Task.FromResult(document);
}
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
示例3: SimplifyName
private static SyntaxNode SimplifyName(
SyntaxNode node,
SemanticModel semanticModel,
OptionSet optionSet,
CancellationToken cancellationToken)
{
SyntaxNode replacementNode;
TextSpan issueSpan;
if (node.Kind() == SyntaxKind.QualifiedCref)
{
var crefSyntax = (QualifiedCrefSyntax)node;
if (!crefSyntax.TryReduceOrSimplifyExplicitName(semanticModel, out var crefReplacement, out issueSpan, optionSet, cancellationToken))
{
return node;
}
replacementNode = crefReplacement;
}
else
{
var expressionSyntax = (ExpressionSyntax)node;
if (!expressionSyntax.TryReduceOrSimplifyExplicitName(semanticModel, out var expressionReplacement, out issueSpan, optionSet, cancellationToken))
{
return node;
}
replacementNode = expressionReplacement;
}
node = node.CopyAnnotationsTo(replacementNode).WithAdditionalAnnotations(Formatter.Annotation);
return node.WithoutAnnotations(Simplifier.Annotation);
}
示例4: PathSyntaxReference
public PathSyntaxReference(SyntaxNode node)
{
_tree = node.SyntaxTree;
_kind = node.Kind();
_textSpan = node.Span;
_pathFromRoot = ComputePathFromRoot(node);
}
示例5: TryGetMethodDeclarationBody
public static SyntaxNode TryGetMethodDeclarationBody(SyntaxNode node)
{
SyntaxNode result;
switch (node.Kind())
{
case SyntaxKind.MethodDeclaration:
var methodDeclaration = (MethodDeclarationSyntax)node;
result = (SyntaxNode)methodDeclaration.Body ?? methodDeclaration.ExpressionBody?.Expression;
break;
case SyntaxKind.ConversionOperatorDeclaration:
var conversionDeclaration = (ConversionOperatorDeclarationSyntax)node;
result = (SyntaxNode)conversionDeclaration.Body ?? conversionDeclaration.ExpressionBody?.Expression;
break;
case SyntaxKind.OperatorDeclaration:
var operatorDeclaration = (OperatorDeclarationSyntax)node;
result = (SyntaxNode)operatorDeclaration.Body ?? operatorDeclaration.ExpressionBody?.Expression;
break;
case SyntaxKind.SetAccessorDeclaration:
case SyntaxKind.AddAccessorDeclaration:
case SyntaxKind.RemoveAccessorDeclaration:
case SyntaxKind.GetAccessorDeclaration:
result = ((AccessorDeclarationSyntax)node).Body;
break;
case SyntaxKind.ConstructorDeclaration:
result = ((ConstructorDeclarationSyntax)node).Body;
break;
case SyntaxKind.DestructorDeclaration:
result = ((DestructorDeclarationSyntax)node).Body;
break;
case SyntaxKind.PropertyDeclaration:
var propertyDeclaration = (PropertyDeclarationSyntax)node;
if (propertyDeclaration.Initializer != null)
{
result = propertyDeclaration.Initializer.Value;
break;
}
return propertyDeclaration.ExpressionBody?.Expression;
case SyntaxKind.IndexerDeclaration:
return ((IndexerDeclarationSyntax)node).ExpressionBody?.Expression;
default:
return null;
}
if (result != null)
{
AssertIsBody(result, allowLambda: false);
}
return result;
}
示例6: PositionalSyntaxReference
public PositionalSyntaxReference(SyntaxNode node)
{
_tree = node.SyntaxTree;
_textSpan = node.Span;
_kind = node.Kind();
System.Diagnostics.Debug.Assert(_textSpan.Length > 0);
}
示例7: Create
internal static GlobalExpressionVariable Create(
SourceMemberContainerTypeSymbol containingType,
DeclarationModifiers modifiers,
TypeSyntax typeSyntax,
string name,
SyntaxNode syntax,
Location location,
FieldSymbol containingFieldOpt,
SyntaxNode nodeToBind)
{
Debug.Assert(nodeToBind.Kind() == SyntaxKind.VariableDeclarator
|| nodeToBind is ExpressionSyntax
|| nodeToBind.Kind() == SyntaxKind.VariableComponentAssignment);
var syntaxReference = syntax.GetReference();
return typeSyntax.IsVar
? new InferrableGlobalExpressionVariable(containingType, modifiers, typeSyntax, name, syntaxReference, location, containingFieldOpt, nodeToBind)
: new GlobalExpressionVariable(containingType, modifiers, typeSyntax, name, syntaxReference, location);
}
示例8: AddIndentBlockOperations
public override void AddIndentBlockOperations(List<IndentBlockOperation> list, SyntaxNode node, OptionSet optionSet, NextAction<IndentBlockOperation> nextOperation)
{
nextOperation.Invoke(list);
if (s_allowableKinds.Contains(node.Kind()))
{
AddChangeSignatureIndentOperation(list, node);
}
}
示例9: BuildMap
// methodsWithYields will contain all function-declaration-like CSharpSyntaxNodes with yield statements contained within them.
// Currently the types of these are restricted to only be whatever the syntax parameter is, plus any LocalFunctionStatementSyntax contained within it.
// This may change if the language is extended to allow iterator lambdas, in which case the lambda would also be returned.
// (lambdas currently throw a diagnostic in WithLambdaParametersBinder.GetIteratorElementType when a yield is used within them)
public static SmallDictionary<SyntaxNode, Binder> BuildMap(
Symbol containingMemberOrLambda,
SyntaxNode syntax,
Binder enclosing,
ArrayBuilder<SyntaxNode> methodsWithYields,
Func<Binder, SyntaxNode, Binder> rootBinderAdjusterOpt = null)
{
var builder = new LocalBinderFactory(containingMemberOrLambda, syntax, enclosing, methodsWithYields);
StatementSyntax statement;
var expressionSyntax = syntax as ExpressionSyntax;
if (expressionSyntax != null)
{
enclosing = new ExpressionVariableBinder(syntax, enclosing);
if ((object)rootBinderAdjusterOpt != null)
{
enclosing = rootBinderAdjusterOpt(enclosing, syntax);
}
builder.AddToMap(syntax, enclosing);
builder.Visit(expressionSyntax, enclosing);
}
else if (syntax.Kind() != SyntaxKind.Block && (statement = syntax as StatementSyntax) != null)
{
CSharpSyntaxNode embeddedScopeDesignator;
enclosing = builder.GetBinderForPossibleEmbeddedStatement(statement, enclosing, out embeddedScopeDesignator);
if ((object)rootBinderAdjusterOpt != null)
{
enclosing = rootBinderAdjusterOpt(enclosing, embeddedScopeDesignator);
}
if (embeddedScopeDesignator != null)
{
builder.AddToMap(embeddedScopeDesignator, enclosing);
}
builder.Visit(statement, enclosing);
}
else
{
if ((object)rootBinderAdjusterOpt != null)
{
enclosing = rootBinderAdjusterOpt(enclosing, null);
}
builder.Visit((CSharpSyntaxNode)syntax, enclosing);
}
// the other place this is possible is in a local function
if (builder._sawYield)
methodsWithYields.Add(syntax);
return builder._map;
}
示例10: GetMethodNameBasedOnExpression
private static string GetMethodNameBasedOnExpression(string methodName, SyntaxNode expression)
{
if (expression.Parent != null &&
expression.Parent.Kind() == SyntaxKind.EqualsValueClause &&
expression.Parent.Parent != null &&
expression.Parent.Parent.Kind() == SyntaxKind.VariableDeclarator)
{
var name = ((VariableDeclaratorSyntax)expression.Parent.Parent).Identifier.ValueText;
return (name != null && name.Length > 0) ? MakeMethodName("Get", name) : methodName;
}
if (expression is MemberAccessExpressionSyntax)
{
expression = ((MemberAccessExpressionSyntax)expression).Name;
}
if (expression is NameSyntax)
{
SimpleNameSyntax unqualifiedName;
switch (expression.Kind())
{
case SyntaxKind.IdentifierName:
case SyntaxKind.GenericName:
unqualifiedName = (SimpleNameSyntax)expression;
break;
case SyntaxKind.QualifiedName:
unqualifiedName = ((QualifiedNameSyntax)expression).Right;
break;
case SyntaxKind.AliasQualifiedName:
unqualifiedName = ((AliasQualifiedNameSyntax)expression).Name;
break;
default:
throw new System.NotSupportedException("Unexpected name kind: " + expression.Kind().ToString());
}
var unqualifiedNameIdentifierValueText = unqualifiedName.Identifier.ValueText;
return (unqualifiedNameIdentifierValueText != null && unqualifiedNameIdentifierValueText.Length > 0) ? MakeMethodName("Get", unqualifiedNameIdentifierValueText) : methodName;
}
return methodName;
}
开发者ID:Rickinio,项目名称:roslyn,代码行数:42,代码来源:CSharpMethodExtractor.CSharpCodeGenerator.ExpressionCodeGenerator.cs
示例11: HasChildren
private static bool HasChildren(SyntaxNode node)
{
// Leaves are labeled statements that don't have a labeled child.
// We also return true for non-labeled statements.
Label label = Classify(node.Kind(), out var isLeaf, ignoreVariableDeclarations: false);
// ignored should always be reported as leaves
Debug.Assert(label != Label.Ignored || isLeaf);
return !isLeaf;
}
示例12: AssertIsClosureScopeSyntax
private static void AssertIsClosureScopeSyntax(SyntaxNode syntaxOpt)
{
// See C# specification, chapter 3.7 Scopes.
// static lambdas technically have the class scope so the scope syntax is null
if (syntaxOpt == null)
{
return;
}
if (LambdaUtilities.IsClosureScope(syntaxOpt))
{
return;
}
throw ExceptionUtilities.UnexpectedValue(syntaxOpt.Kind());
}
示例13: GetDisplayNode
/// <summary>
/// Returns the node that should be displayed
/// </summary>
public SyntaxNode GetDisplayNode(SyntaxNode node)
{
while (true)
{
switch (node.Kind())
{
// LocalDeclarations do not have symbols themselves, you need a variable declarator
case SyntaxKind.LocalDeclarationStatement:
var localDeclarationNode = (LocalDeclarationStatementSyntax)node;
node = localDeclarationNode.Declaration.Variables.FirstOrDefault();
continue;
// Field and event declarations do not have symbols themselves, you need a variable declarator
case SyntaxKind.FieldDeclaration:
case SyntaxKind.EventFieldDeclaration:
var fieldNode = (BaseFieldDeclarationSyntax)node;
node = fieldNode.Declaration.Variables.FirstOrDefault();
continue;
// Variable is a field without access modifier. Parent is FieldDeclaration
case SyntaxKind.VariableDeclaration:
node = node.Parent;
continue;
// Built in types
case SyntaxKind.PredefinedType:
node = node.Parent;
continue;
case SyntaxKind.MultiLineDocumentationCommentTrivia:
case SyntaxKind.SingleLineDocumentationCommentTrivia:
// For DocumentationCommentTrivia node, node.Parent is null. Obtain parent through ParentTrivia.Token
if (node.IsStructuredTrivia)
{
var structuredTriviaSyntax = (StructuredTriviaSyntax)node;
node = structuredTriviaSyntax.ParentTrivia.Token.Parent;
continue;
}
return null;
default:
return node;
}
}
}
示例14: GetReplacementNode
private SyntaxNode GetReplacementNode(SyntaxNode statement)
{
switch (statement.Kind())
{
case SyntaxKind.IfStatement:
var ifSyntax = (IfStatementSyntax)statement;
return GetNewBlock(statement, ifSyntax.Statement);
case SyntaxKind.ElseClause:
var elseClause = (ElseClauseSyntax)statement;
return GetNewBlock(statement, elseClause.Statement);
case SyntaxKind.ForStatement:
var forSyntax = (ForStatementSyntax)statement;
return GetNewBlock(statement, forSyntax.Statement);
case SyntaxKind.ForEachStatement:
case SyntaxKind.ForEachVariableStatement:
var forEachSyntax = (CommonForEachStatementSyntax)statement;
return GetNewBlock(statement, forEachSyntax.Statement);
case SyntaxKind.WhileStatement:
var whileSyntax = (WhileStatementSyntax)statement;
return GetNewBlock(statement, whileSyntax.Statement);
case SyntaxKind.DoStatement:
var doSyntax = (DoStatementSyntax)statement;
return GetNewBlock(statement, doSyntax.Statement);
case SyntaxKind.UsingStatement:
var usingSyntax = (UsingStatementSyntax)statement;
return GetNewBlock(statement, usingSyntax.Statement);
case SyntaxKind.LockStatement:
var lockSyntax = (LockStatementSyntax)statement;
return GetNewBlock(statement, lockSyntax.Statement);
}
return default(SyntaxNode);
}
示例15: IsAttributeName
public static bool IsAttributeName(SyntaxNode node)
{
var parent = node.Parent;
if (parent == null || !IsName(node.Kind()))
{
return false;
}
switch (parent.Kind())
{
case QualifiedName:
var qn = (QualifiedNameSyntax)parent;
return qn.Right == node ? IsAttributeName(parent) : false;
case AliasQualifiedName:
var an = (AliasQualifiedNameSyntax)parent;
return an.Name == node ? IsAttributeName(parent) : false;
}
var p = node.Parent as AttributeSyntax;
return p != null && p.Name == node;
}