本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.Kind方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.Kind方法的具体用法?C# SyntaxNode.Kind怎么用?C# SyntaxNode.Kind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.Kind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessNode
static string ProcessNode(SyntaxNode node, int depth, StringBuilder prepend)
{
switch (node.Kind())
{
case SyntaxKind.CompilationUnit:
return ProcessChildNodes(node, depth, prepend);
case SyntaxKind.OptionStatement:
case SyntaxKind.ImportsStatement:
case SyntaxKind.ClassStatement:
case SyntaxKind.EndClassStatement:
case SyntaxKind.ImplementsStatement:
case SyntaxKind.EnumStatement:
case SyntaxKind.EndEnumStatement:
// ignore;
break;
case SyntaxKind.ClassBlock:
var className = ((ClassStatementSyntax)node.ChildNodes().First()).Identifier.Text;
var classResult = string.Format("class {0} {{\n{1}}}\n", className, ProcessChildNodes(node, depth, prepend));
if (depth == 0) return classResult;
prepend.Append(classResult);
return null;
case SyntaxKind.EnumBlock:
var enumName = ((EnumStatementSyntax)node.ChildNodes().First()).Identifier.Text;
var values = node.ChildNodes().OfType<EnumMemberDeclarationSyntax>().Select(n => n.Initializer == null ? n.Identifier.Text : n.Identifier.Text + " = " + n.Initializer.Value);
prepend.Append(string.Format("enum {0} {{{1}}};\n", enumName, string.Join(", ", values)));
return null;
case SyntaxKind.EnumMemberDeclaration:
return ((EnumMemberDeclarationSyntax)node).Identifier.Text;
default:
return string.Format("{0}// UNKNOWN {1}\n", Tabs(depth), node.Kind());
}
return null;
}
示例2: IsNameableNode
private static bool IsNameableNode(SyntaxNode node)
{
switch (node.Kind())
{
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ConstructorDeclaration:
case SyntaxKind.ConversionOperatorDeclaration:
case SyntaxKind.DelegateDeclaration:
case SyntaxKind.DestructorDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.EnumMemberDeclaration:
case SyntaxKind.EventDeclaration:
case SyntaxKind.IndexerDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.MethodDeclaration:
case SyntaxKind.NamespaceDeclaration:
case SyntaxKind.OperatorDeclaration:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.StructDeclaration:
return true;
case SyntaxKind.VariableDeclarator:
// Could be a regular field or an event field.
return node.FirstAncestorOrSelf<BaseFieldDeclarationSyntax>() != null;
default:
return false;
}
}
示例3: IsFunctionLikeLastReturnStatement
private static bool IsFunctionLikeLastReturnStatement(SyntaxNode node)
{
if (!BranchKinds.Contains(node.Kind()))
{
return false;
}
if (!node.IsKind(SyntaxKind.ReturnStatement))
{
return false;
}
var blockType = node.Parent.GetType();
if (!BlockTypes.Any(bType => bType.IsAssignableFrom(blockType)))
{
return false;
}
if (!IsFunctionLike(node.Parent))
{
return false;
}
var nextToken = node.GetLastToken().GetNextToken();
var nextNode = nextToken.Parent;
return nextToken.IsKind(SyntaxKind.EndKeyword) &&
node.Parent == nextNode.Parent;
}
示例4: AddNewModifier
SyntaxNode AddNewModifier(SyntaxNode node)
{
SyntaxToken newToken = SyntaxFactory.Token(SyntaxKind.NewKeyword);
switch (node.Kind())
{
//couldn't find a common base
case SyntaxKind.IndexerDeclaration:
var indexer = (IndexerDeclarationSyntax)node;
return indexer.AddModifiers(newToken);
case SyntaxKind.ClassDeclaration:
var classDecl = (ClassDeclarationSyntax)node;
return classDecl.AddModifiers(newToken);
case SyntaxKind.PropertyDeclaration:
var propDecl = (PropertyDeclarationSyntax)node;
return propDecl.AddModifiers(newToken);
case SyntaxKind.MethodDeclaration:
var methDecl = (MethodDeclarationSyntax)node;
return methDecl.AddModifiers(newToken);
case SyntaxKind.StructDeclaration:
var structDecl = (StructDeclarationSyntax)node;
return structDecl.AddModifiers(newToken);
case SyntaxKind.EnumDeclaration:
var enumDecl = (EnumDeclarationSyntax)node;
return enumDecl.AddModifiers(newToken);
case SyntaxKind.InterfaceDeclaration:
var intDecl = (InterfaceDeclarationSyntax)node;
return intDecl.AddModifiers(newToken);
case SyntaxKind.DelegateDeclaration:
var deleDecl = (DelegateDeclarationSyntax)node;
return deleDecl.AddModifiers(newToken);
default:
return node;
}
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:34,代码来源:CS0108UseNewKeywordIfHidingIntendedCodeFixProvider.cs
示例5: GetStartPoint
protected override VirtualTreePoint? GetStartPoint(SourceText text, SyntaxNode node, EnvDTE.vsCMPart part)
{
switch (node.Kind())
{
case SyntaxKind.ArrowExpressionClause:
return GetStartPoint(text, (ArrowExpressionClauseSyntax)node, part);
case SyntaxKind.Attribute:
return GetStartPoint(text, (AttributeSyntax)node, part);
case SyntaxKind.AttributeArgument:
return GetStartPoint(text, (AttributeArgumentSyntax)node, part);
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.StructDeclaration:
case SyntaxKind.EnumDeclaration:
return GetStartPoint(text, (BaseTypeDeclarationSyntax)node, part);
case SyntaxKind.MethodDeclaration:
case SyntaxKind.ConstructorDeclaration:
case SyntaxKind.DestructorDeclaration:
case SyntaxKind.OperatorDeclaration:
case SyntaxKind.ConversionOperatorDeclaration:
return GetStartPoint(text, (BaseMethodDeclarationSyntax)node, part);
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.IndexerDeclaration:
case SyntaxKind.EventDeclaration:
return GetStartPoint(text, (BasePropertyDeclarationSyntax)node, part);
case SyntaxKind.GetAccessorDeclaration:
case SyntaxKind.SetAccessorDeclaration:
case SyntaxKind.AddAccessorDeclaration:
case SyntaxKind.RemoveAccessorDeclaration:
return GetStartPoint(text, (AccessorDeclarationSyntax)node, part);
case SyntaxKind.DelegateDeclaration:
return GetStartPoint(text, (DelegateDeclarationSyntax)node, part);
case SyntaxKind.NamespaceDeclaration:
return GetStartPoint(text, (NamespaceDeclarationSyntax)node, part);
case SyntaxKind.UsingDirective:
return GetStartPoint(text, (UsingDirectiveSyntax)node, part);
case SyntaxKind.EnumMemberDeclaration:
return GetStartPoint(text, (EnumMemberDeclarationSyntax)node, part);
case SyntaxKind.VariableDeclarator:
return GetStartPoint(text, (VariableDeclaratorSyntax)node, part);
case SyntaxKind.Parameter:
return GetStartPoint(text, (ParameterSyntax)node, part);
default:
Debug.Fail("Unsupported node kind: " + node.Kind());
throw new NotSupportedException();
}
}
示例6: FixEquals
protected override async Task<Document> FixEquals(Document document, SyntaxGenerator generator, SyntaxNode root, SyntaxNode node, CancellationToken cancellationToken)
{
SemanticModel model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var binaryExpression = (BinaryExpressionSyntax)node;
SyntaxNode invocation = CreateEqualsExpression(generator, model, binaryExpression.Left, binaryExpression.Right, node.Kind() == SyntaxKind.EqualsExpression).WithAdditionalAnnotations(Formatter.Annotation);
SyntaxNode newRoot = root.ReplaceNode(node, invocation);
return document.WithSyntaxRoot(newRoot);
}
示例7: Visit
public override void Visit(SyntaxNode node)
{
base.Visit(node);
if (Contributors.Contains(node.Kind()))
{
_counter++;
}
}
示例8: Visit
public override void Visit(SyntaxNode node)
{
var t = node.Kind();
if (node.IsKind(SyntaxKind.IdentifierName))
{
var name = node.ToString();
string syntax;
if (_DefinedOutside.TryGetValue(name, out syntax))
{
_DefinedOutside.Remove(name);
sb.Append(syntax);
}
}
/*
var symbol = _model.GetSymbolInfo(node).Symbol;
var ca = _model.GetSymbolInfo(node).CandidateSymbols;
var declaredSymbol = _model.GetDeclaredSymbol(node);
if (symbol?.Kind == SymbolKind.Field)
{
var x = 3;
}
if (symbol != null)
{
var test = symbol.CanBeReferencedByName;
var sources = symbol.DeclaringSyntaxReferences;
if (sources.Any())
{
foreach (var source in sources)
{
if (source.Span.Start > _end
|| source.Span.End < _start)
{
//DefinedOutside.Add(source.GetSyntax().ToFullString());
}
}
}
var locations = symbol?.Locations;
if (locations.HasValue)
{
/*
foreach (var location in locations.Value)
{
if (location.SourceSpan.Start > _end
|| location.SourceSpan.End < _start)
{
DefinedOutside.Add(symbol);
}
}
}
}*/
base.Visit(node);
}
示例9: ShouldReportOnCollectionNode
internal static bool ShouldReportOnCollectionNode(SemanticModel semanticModel, SyntaxNode argument) {
const bool returnIfUnknown = false;
// if the variable is an invocation syntax we can assume that it was returned materialized, if it's not an extension method
switch(argument.Kind()) {
case SyntaxKind.InvocationExpression:
var methodCallInfo = semanticModel.GetSymbolInfo(argument);
if((methodCallInfo.Symbol != null) && (methodCallInfo.Symbol.Kind == SymbolKind.Method)) {
var mSymbol = (IMethodSymbol)methodCallInfo.Symbol;
var typeInfo = semanticModel.GetTypeInfo(argument);
// If the method is not an extension method, we assume it returned a materialized collection
return IsAbstractCollectionType(typeInfo) && mSymbol.IsExtensionMethod && mSymbol.ContainingNamespace.ToDisplayString().Equals("System.Linq");
}
break;
case SyntaxKind.IdentifierName:
var identifierInfo = semanticModel.GetSymbolInfo(argument);
// if this identifier came straight from a parameter, assume it is materialized
if(identifierInfo.Symbol != null && identifierInfo.Symbol.Kind == SymbolKind.Parameter) {
//TODO(2016-02-24, yurig): if parameter is modified locally, we should warn
return false;
}
// if this is a local identifier, look at where it is defined
if(identifierInfo.Symbol != null && identifierInfo.Symbol.Kind == SymbolKind.Local) {
var declaration = identifierInfo.Symbol.DeclaringSyntaxReferences.FirstOrDefault();
// if the declaration is an equals expression, dive into it.
var equalsExpression = declaration?.GetSyntax()
.ChildNodes()
.FirstOrDefault(x => x.Kind() == SyntaxKind.EqualsValueClause);
var firstChild = equalsExpression?.ChildNodes().FirstOrDefault();
if(firstChild != null) {
return ShouldReportOnCollectionNode(semanticModel, firstChild);
}
// if the variable was assigned somewhere else, find it
var containingClass = declaration?.GetSyntax().FirstAncestorOrSelf<MethodDeclarationSyntax>();
var localAssignment = containingClass?.DescendantNodes().OfType<AssignmentExpressionSyntax>()
.Where(x => x.Left.IsKind(SyntaxKind.IdentifierName))
.LastOrDefault(x => (x.Left as IdentifierNameSyntax).Identifier.Text.Equals(((IdentifierNameSyntax)argument).Identifier.Text));
if(localAssignment != null) {
return ShouldReportOnCollectionNode(semanticModel, localAssignment.Right);
}
}
break;
case SyntaxKind.SimpleMemberAccessExpression:
// Assume that member accesses are returned materialized
return false;
}
return returnIfUnknown;
}
示例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:pabloescribanoloza,项目名称:monodevelop,代码行数:42,代码来源:CSharpMethodExtractor.CSharpCodeGenerator.ExpressionCodeGenerator.cs
示例11: IsControlBlock
private bool IsControlBlock(SyntaxNode node)
{
var parent = node != null ? node.Parent : null;
return (node != null && node.Kind() == SyntaxKind.SwitchStatement) ||
(parent != null &&
(parent.Kind() == SyntaxKind.IfStatement || parent.Kind() == SyntaxKind.ElseClause ||
parent.Kind() == SyntaxKind.WhileStatement || parent.Kind() == SyntaxKind.DoStatement ||
parent.Kind() == SyntaxKind.ForEachStatement || parent.Kind() == SyntaxKind.UsingStatement ||
parent.Kind() == SyntaxKind.ForStatement || parent.Kind() == SyntaxKind.TryStatement ||
parent.Kind() == SyntaxKind.CatchClause || parent.Kind() == SyntaxKind.FinallyClause ||
parent.Kind() == SyntaxKind.LockStatement || parent.Kind() == SyntaxKind.CheckedStatement ||
parent.Kind() == SyntaxKind.UncheckedStatement || parent.Kind() == SyntaxKind.SwitchSection));
}
示例12: GetClassDeclarationTypeSymbol
public override ITypeSymbol GetClassDeclarationTypeSymbol(SyntaxNode node, SemanticModel semanticModel)
{
if (node == null)
{
return null;
}
SyntaxKind kind = node.Kind();
if (kind == SyntaxKind.ClassDeclaration)
{
return semanticModel.GetDeclaredSymbol((ClassDeclarationSyntax)node);
}
return null;
}
示例13: AsNode
private FileMemberElement AsNode(SyntaxNode node, string text, Location location)
{
var ret = new FileMemberElement();
var lineSpan = location.GetLineSpan();
ret.Projects = new List<string>();
ret.ChildNodes = new List<FileMemberElement>();
ret.Kind = node.Kind().ToString();
ret.Location = new QuickFix();
ret.Location.Text = text;
ret.Location.Line = 1 + lineSpan.StartLinePosition.Line;
ret.Location.Column = 1 + lineSpan.StartLinePosition.Character;
ret.Location.EndLine = 1 + lineSpan.EndLinePosition.Line;
ret.Location.EndColumn = 1 + lineSpan.EndLinePosition.Character;
return ret;
}
示例14: FindMemberDeclaration
/// <returns>
/// <see cref="BaseMethodDeclarationSyntax"/> for methods, operators, constructors, destructors and accessors.
/// <see cref="VariableDeclaratorSyntax"/> for field initializers.
/// <see cref="PropertyDeclarationSyntax"/> for property initializers and expression bodies.
/// <see cref="IndexerDeclarationSyntax"/> for indexer expression bodies.
/// </returns>
internal override SyntaxNode FindMemberDeclaration(SyntaxNode rootOpt, SyntaxNode node)
{
while (node != rootOpt)
{
switch (node.Kind())
{
case SyntaxKind.MethodDeclaration:
case SyntaxKind.ConversionOperatorDeclaration:
case SyntaxKind.OperatorDeclaration:
case SyntaxKind.SetAccessorDeclaration:
case SyntaxKind.AddAccessorDeclaration:
case SyntaxKind.RemoveAccessorDeclaration:
case SyntaxKind.GetAccessorDeclaration:
case SyntaxKind.ConstructorDeclaration:
case SyntaxKind.DestructorDeclaration:
return node;
case SyntaxKind.PropertyDeclaration:
// int P { get; } = [|initializer|];
Debug.Assert(((PropertyDeclarationSyntax)node).Initializer != null);
return node;
case SyntaxKind.FieldDeclaration:
case SyntaxKind.EventFieldDeclaration:
// Active statements encompassing modifiers or type correspond to the first initialized field.
// [|public static int F = 1|], G = 2;
return ((BaseFieldDeclarationSyntax)node).Declaration.Variables.First();
case SyntaxKind.VariableDeclarator:
// public static int F = 1, [|G = 2|];
Debug.Assert(node.Parent.IsKind(SyntaxKind.VariableDeclaration));
switch (node.Parent.Parent.Kind())
{
case SyntaxKind.FieldDeclaration:
case SyntaxKind.EventFieldDeclaration:
return node;
}
node = node.Parent;
break;
}
node = node.Parent;
}
return null;
}
示例15: CanSpeculateOnNode
public static bool CanSpeculateOnNode(SyntaxNode node)
{
return (node is StatementSyntax && node.Kind() != SyntaxKind.Block) ||
node is TypeSyntax ||
node is CrefSyntax ||
node.Kind() == SyntaxKind.Attribute ||
node.Kind() == SyntaxKind.ThisConstructorInitializer ||
node.Kind() == SyntaxKind.BaseConstructorInitializer ||
node.Kind() == SyntaxKind.EqualsValueClause ||
node.Kind() == SyntaxKind.ArrowExpressionClause;
}