本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.ChildNodes方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.ChildNodes方法的具体用法?C# SyntaxNode.ChildNodes怎么用?C# SyntaxNode.ChildNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.ChildNodes方法的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: GetActions
private static IEnumerable<CodeAction> GetActions(Document document, SyntaxNode root, SyntaxNode node, CSharpSyntaxNode nullableType, CSharpSyntaxNode objectType)
{
var returnType = (CSharpSyntaxNode)nullableType ?? objectType;
if (returnType == null)
yield break;
var bodyStatement = node.ChildNodes().OfType<BlockSyntax>().FirstOrDefault();
if (bodyStatement == null)
yield break;
if (HasReturnContract(bodyStatement, returnType.ToString()))
yield break;
yield return CreateAction(
node.Span
,t2 => {
var newBody = bodyStatement.WithStatements(SyntaxFactory.List<StatementSyntax>(new[] { CreateContractEnsuresCall(returnType.ToString()) }.Concat(bodyStatement.Statements)));
var newRoot = (CompilationUnitSyntax)root.ReplaceNode((SyntaxNode)bodyStatement, newBody);
if (UsingStatementNotPresent(newRoot)) newRoot = AddUsingStatement(node, newRoot);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
,"Add a Contract to specify the return value must not be null"
);
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:27,代码来源:ContractEnsuresNotNullReturnRefactoringProvider.cs
示例3: Calculate
public IEnumerable<IHalsteadMetrics> Calculate(SyntaxNode root)
{
var analyzer = new HalsteadAnalyzer();
var childNodes = root.ChildNodes().AsArray();
var types = childNodes.Where(n => n.IsKind(SyntaxKind.ClassDeclaration) || n.IsKind(SyntaxKind.StructDeclaration))
.AsArray();
var methods = types.SelectMany(n => n.ChildNodes().Where(_isMethod));
var getProperties = types.SelectMany(n => n.ChildNodes().Where(IsGetProperty));
var setProperties = types.SelectMany(n => n.ChildNodes().Where(IsSetProperty));
var looseMethods = childNodes.Where(_isMethod);
var looseGetProperties = childNodes.Where(IsGetProperty);
var looseSetProperties = childNodes.Where(IsSetProperty);
var members = methods.Concat(getProperties)
.Concat(setProperties)
.Concat(looseMethods)
.Concat(looseGetProperties)
.Concat(looseSetProperties)
.AsArray();
if (members.Any())
{
return members.Select(analyzer.Calculate);
}
var statements = childNodes.Length == 0
? root.DescendantNodesAndTokens().Select(x => SyntaxFactory.ParseStatement(x.ToFullString(), 0, new CSharpParseOptions(kind: SourceCodeKind.Script, preprocessorSymbols: new string[0])))
: childNodes.Select(x => SyntaxFactory.ParseStatement(x.ToFullString(), 0, new CSharpParseOptions(kind: SourceCodeKind.Script, preprocessorSymbols: new string[0])));
var fakeMethod = SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), "fake")
.WithBody(SyntaxFactory.Block(statements));
return new[]
{
analyzer.Calculate(fakeMethod)
};
}
示例4: SyntaxNodeMatchesDeclaration
public bool SyntaxNodeMatchesDeclaration(SyntaxNode syntaxNode)
{
if (string.IsNullOrWhiteSpace(_declarationFilter))
{
return true;
}
// The semantics of CSharp and Visual Basic appear to be different (I.e., VB puts everything inside a "Block")
if (syntaxNode.SyntaxTree is Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree)
{
if (SyntaxNodeWrapper.Get(syntaxNode).GetKind().EndsWith("Declaration"))
{
return InternalsHelper.GetIdentifierTokenValueText(syntaxNode) == _declarationFilter;
}
}
else if (syntaxNode.SyntaxTree is Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree)
{
if (SyntaxNodeWrapper.Get(syntaxNode).GetKind().EndsWith("Block"))
{
SyntaxNode firstChild = syntaxNode.ChildNodes().FirstOrDefault();
if (firstChild != null && SyntaxNodeWrapper.Get(firstChild).GetKind().EndsWith("Statement"))
{
return InternalsHelper.GetIdentifierTokenValueText(firstChild) == _declarationFilter;
}
}
}
return false;
}
示例5: ProcessChildNodes
static string ProcessChildNodes(SyntaxNode node, int depth, StringBuilder prepend)
{
var sb = new StringBuilder();
foreach (var childNode in node.ChildNodes())
{
sb.Append(ProcessNode(childNode, depth + 1, prepend));
}
return sb.ToString();
}
示例6: FindExpressionForConfigureAwait
public static InvocationExpressionSyntax FindExpressionForConfigureAwait(SyntaxNode node)
{
foreach (var item in node.ChildNodes())
{
if (item is InvocationExpressionSyntax)
return (InvocationExpressionSyntax)item;
return FindExpressionForConfigureAwait(item);
}
return null;
}
示例7: CountNesting
static int CountNesting(SyntaxNode node)
{
int nesting = 0;
foreach (var child in node.ChildNodes())
{
if(!(child is BlockSyntax) && child is StatementSyntax) nesting = Math.Max(CountNesting(child) + 1, nesting);
else nesting = Math.Max(CountNesting(child), nesting);
}
return nesting;
}
示例8: GetTargetNode
protected override SyntaxNode GetTargetNode(SyntaxNode node)
{
if (node.IsKind(SyntaxKind.MemberBindingExpression))
{
var nameNode = node.ChildNodes().FirstOrDefault(n => n.IsKind(SyntaxKind.IdentifierName));
if (nameNode != null)
{
return nameNode;
}
}
return base.GetTargetNode(node);
}
示例9: Recurse
private void Recurse(SyntaxNode node, bool isInSwitch)
{
if (node is ContinueStatementSyntax)
HasContinue = true;
else if (node is BreakStatementSyntax && !isInSwitch)
//ignore break statements in a switch, since they apply to breaking the switch and not the loop
HasBreak = true;
else
{
foreach (var child in node.ChildNodes())
{
if (!IsLoopSyntax(child))
//any breaks or continues in child loops will belong to that loop, so we can skip recusing into them.
Recurse(child, isInSwitch || child is SwitchStatementSyntax);
}
}
}
示例10: ParseSyntaxNode
protected override void ParseSyntaxNode(ITextSnapshot snapshot, SyntaxNode parentSyntaxNode, CodeBlock parentCodeBlockNode, CancellationToken token, int level)
{
if (token.IsCancellationRequested)
{
throw new TaskCanceledException();
}
else
{
foreach (var childnode in parentSyntaxNode.ChildNodes())
{
BlockType type = BlockType.Unknown;
int startPosition = 0;
int endPosition = 0;
if (TryAsNamespace(childnode, ref type, ref startPosition, ref endPosition) ||
TryAsType(childnode, ref type, ref startPosition, ref endPosition) ||
TryAsEnum(childnode, ref type, ref startPosition, ref endPosition) ||
TryAsSwitch(childnode, ref type, ref startPosition, ref endPosition) ||
TryAsSwitchSection(childnode, snapshot, ref type, ref startPosition, ref endPosition) ||
TryAsProperty(childnode, ref type, ref startPosition, ref endPosition))
{
var statementStart = childnode.SpanStart;
string statement = StatementFromSpan(snapshot, statementStart, startPosition);
CodeBlock child = new CodeBlock(parentCodeBlockNode, type, statement, new SnapshotSpan(snapshot, Span.FromBounds(startPosition, endPosition)), statementStart, level + 1);
ParseSyntaxNode(snapshot, childnode, child, token, level + 1);
}
else if (TryAsBlock(childnode, parentSyntaxNode, ref type, ref startPosition, ref endPosition))
{
int statementStart = type == BlockType.Unknown ? startPosition : parentSyntaxNode.SpanStart;
string statement = StatementFromSpan(snapshot, statementStart, startPosition);
CodeBlock child = new CodeBlock(parentCodeBlockNode, type, statement, new SnapshotSpan(snapshot, Span.FromBounds(startPosition, endPosition)), statementStart, level + 1);
ParseSyntaxNode(snapshot, childnode, child, token, level + 1);
}
else
{
ParseSyntaxNode(snapshot, childnode, parentCodeBlockNode, token, level);
}
}
}
}
示例11: SearchUsingInsertionPoint
static int SearchUsingInsertionPoint (SyntaxNode parent)
{
var result = 0;
foreach (SyntaxNode node in parent.ChildNodes ()) {
if (node.IsKind (Microsoft.CodeAnalysis.CSharp.SyntaxKind.UsingDirective)) {
result = node.FullSpan.End;
continue;
}
SyntaxTrivia last = new SyntaxTrivia ();
foreach (var trivia in node.GetLeadingTrivia ()) {
if (last.IsKind (Microsoft.CodeAnalysis.CSharp.SyntaxKind.SingleLineCommentTrivia)||
last.IsKind (Microsoft.CodeAnalysis.CSharp.SyntaxKind.DefineDirectiveTrivia) ||
last.IsKind (Microsoft.CodeAnalysis.CSharp.SyntaxKind.MultiLineCommentTrivia) ||
last.IsKind (Microsoft.CodeAnalysis.CSharp.SyntaxKind.SingleLineDocumentationCommentTrivia))
result = trivia.Span.End;
last = trivia;
}
break;
}
return result;
}
示例12: CastResursiveMethod
private static SyntaxNode CastResursiveMethod(SyntaxNode tree, SemanticModel semanticModel, GenRef genRef, Dictionary<SyntaxNode, SyntaxNode> castChanges)
{
var change = new Dictionary<SyntaxNode, SyntaxNode>();
foreach (var node in tree.ChildNodes())
{
ITypeSymbol ts = null;
// if invocation -> ITypeSymbol
// -------------------------
if (node is InvocationExpressionSyntax)
{
ISymbol invokedSymbol = semanticModel.GetSymbolInfo(node).Symbol;
// if is generic method
if (genRef.Methods.Contains(invokedSymbol.OriginalDefinition))
{
ts = ((IMethodSymbol)invokedSymbol).ReturnType;
}
}
else if ((node is MemberAccessExpressionSyntax) && !(node.Parent is AssignmentExpressionSyntax))
{
ISymbol invokedSymbol = semanticModel.GetSymbolInfo(node).Symbol;
// if is generic property
if (genRef.Properties.Contains(invokedSymbol.OriginalDefinition))
{
ts = ((IPropertySymbol)invokedSymbol).Type;
}
}
// recurse for changed node
var casted = CastResursiveMethod(node, semanticModel, genRef, castChanges);
if (ts != null)
{
// do cast
casted = Helpers.CastTo((ExpressionSyntax)casted, ts);
if (node.Parent is MemberAccessExpressionSyntax)
casted = ((ExpressionSyntax)casted).Parenthesize();
castChanges.Add(node, casted);
}
// add for replace
if (node != casted)
change.Add(node, casted);
}
if (change.Any())
tree = tree.ReplaceNodes(change.Keys, (x, y) => change[x]);
return tree;
}
示例13: Visit
public override void Visit(SyntaxNode node)
{
var padding = node.Ancestors().Count();
var prepend = node.ChildNodes().Any() ? "[-]" : "[.]";
var nodetype = node.GetType().FullName;
if (nodetype.StartsWith(prefix)) nodetype = nodetype.Substring(prefix.Length);
var line = new string(' ', padding) + prepend + " " + nodetype;
Console.WriteLine(line);
//var decl = node as ClassDeclarationSyntax;
//if (decl != null && decl.BaseList != null)
//{
// Console.Write(new string(' ', padding + 4) + decl.Identifier);
// foreach (var n in decl.BaseList.Types.OfType<IdentifierNameSyntax>())
// {
// Console.Write(" " + n.Identifier);
// }
// Console.WriteLine();
//}
var attr = node as AttributeSyntax;
if (attr != null)
{
Console.WriteLine(new string(' ', padding + 4) + "> " + attr.Name);
foreach (var arg in attr.ArgumentList.Arguments)
{
var expr = arg.Expression as LiteralExpressionSyntax;
//Console.WriteLine(new string(' ', padding + 4) + "> " + arg.NameColon + " " + arg.NameEquals);
Console.WriteLine(new string(' ', padding + 4) + "> " + (expr == null ? null : expr.Token.Value));
}
}
var attr2 = node as IdentifierNameSyntax;
if (attr2 != null)
{
Console.WriteLine(new string(' ', padding + 4) + "T " + attr2.Identifier.GetType());
Console.WriteLine(new string(' ', padding + 4) + "V " + attr2.Identifier);
}
var x = node as TypeSyntax;
if (x != null)
{
var xtype = x.GetType().FullName;
if (xtype.StartsWith(prefix)) xtype = nodetype.Substring(prefix.Length);
Console.WriteLine(new string(' ', padding + 4) + "> " + xtype);
}
base.Visit(node);
}
示例14: DefaultVisit
public override bool DefaultVisit(SyntaxNode node)
{
foreach (var child in node.ChildNodes())
{
if (Visit(child))
return true;
}
return false;
}
示例15: AddUsings
void AddUsings (SyntaxNode parent)
{
SyntaxNode firstChild = null, lastChild = null;
foreach (var child in parent.ChildNodes ()) {
if (child is UsingDirectiveSyntax) {
if (firstChild == null) {
firstChild = child;
}
lastChild = child;
continue;
}
if (firstChild != null)
break;
}
if (firstChild != null && firstChild != lastChild) {
var first = firstChild.GetLocation ().GetLineSpan ();
var last = lastChild.GetLocation ().GetLineSpan ();
Foldings.Add (new FoldingRegion (new DocumentRegion (first.StartLinePosition, last.EndLinePosition), FoldType.Undefined));
}
}