本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.DescendantNodesAndSelf方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.DescendantNodesAndSelf方法的具体用法?C# SyntaxNode.DescendantNodesAndSelf怎么用?C# SyntaxNode.DescendantNodesAndSelf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.DescendantNodesAndSelf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetComplexity
public override int GetComplexity(SyntaxNode node)
{
var possibleNodes = node
.DescendantNodesAndSelf()
.Where(n =>
!n.Parent.IsKind(SyntaxKind.InterfaceBlock) &&
ComplexityIncreasingKinds.Contains(n.Kind()));
return possibleNodes.Count(n => !IsFunctionLikeLastReturnStatement(n));
}
示例2: GetDescendantMemberAccessExpressionNodes
public override IEnumerable<SyntaxNode> GetDescendantMemberAccessExpressionNodes(SyntaxNode node)
{
IEnumerable<SyntaxNode> empty = Enumerable.Empty<SyntaxNode>();
if (node == null)
{
return empty;
}
return node.DescendantNodesAndSelf().OfType<MemberAccessExpressionSyntax>();
}
示例3: Complexity
public static int Complexity(SyntaxNode node)
{
return node
.DescendantNodesAndSelf()
.Count(
n =>
// TODO What about the null coalescing operator?
ComplexityIncreasingKinds.Contains(n.Kind()) ||
IsReturnButNotLast(n) ||
IsFunctionAndHasBlock(n));
}
示例4: RemoveUnusedImportDirectives
private static SyntaxNode RemoveUnusedImportDirectives(SemanticModel semanticModel, SyntaxNode root, CancellationToken cancellationToken)
{
var oldUsings = root.DescendantNodesAndSelf().Where(s => s is UsingDirectiveSyntax);
var unusedUsings = GetUnusedImportDirectives(semanticModel, cancellationToken);
SyntaxTriviaList leadingTrivia = root.GetLeadingTrivia();
root = root.RemoveNodes(oldUsings, SyntaxRemoveOptions.KeepNoTrivia);
var newUsings = SyntaxFactory.List(oldUsings.Except(unusedUsings));
root = ((CompilationUnitSyntax)root)
.WithUsings(newUsings)
.NormalizeWhitespace()
.WithLeadingTrivia(leadingTrivia);
return root;
}
示例5: IsEmpty
internal static bool IsEmpty(SyntaxNode node)
{
//don't include self
return node == null || node.DescendantNodesAndSelf().All(d => (d is EmptyStatementSyntax || d is BlockSyntax));
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:5,代码来源:ConvertIfStatementToNullCoalescingExpressionAction.cs
示例6: GetElementAccessExpressions
private static IEnumerable<ElementAccessExpressionSyntax> GetElementAccessExpressions(SyntaxNode node)
{
return node == null ?
SpecializedCollections.EmptyEnumerable<ElementAccessExpressionSyntax>() :
node.DescendantNodesAndSelf().Where(s => s.IsKind(SyntaxKind.ElementAccessExpression)).Cast<ElementAccessExpressionSyntax>();
}
示例7: IsSingleType
static bool IsSingleType (SyntaxNode root)
{
return root.DescendantNodesAndSelf (c => !(c is BaseTypeDeclarationSyntax)).OfType<BaseTypeDeclarationSyntax> ().Count () == 1;
}
示例8: GetReferencedTypes
private static IEnumerable<ITypeSymbol> GetReferencedTypes(SyntaxNode classDeclaration, ISymbol sourceSymbol, SemanticModel semanticModel)
{
var typeSyntaxes = classDeclaration.DescendantNodesAndSelf().OfType<TypeSyntax>();
var commonSymbolInfos = typeSyntaxes.Select(x => semanticModel.GetSymbolInfo(x)).AsArray();
var members = commonSymbolInfos
.Select(x => x.Symbol)
.Where(x => x != null)
.Select(x =>
{
var typeSymbol = x as ITypeSymbol;
return typeSymbol == null ? x.ContainingType : x;
})
.Cast<ITypeSymbol>()
.WhereNotNull()
.DistinctBy(x => x.ToDisplayString())
.Where(x => x != sourceSymbol)
.AsArray();
return members;
}
示例9: GetEfferentCoupling
private int GetEfferentCoupling(SyntaxNode classDeclaration, ISymbol sourceSymbol)
{
var typeSyntaxes = classDeclaration.DescendantNodesAndSelf().OfType<TypeSyntax>();
var commonSymbolInfos = typeSyntaxes.Select(x => Model.GetSymbolInfo(x)).AsArray();
var members = commonSymbolInfos
.Select(x => x.Symbol)
.Where(x => x != null)
.Select(x =>
{
var typeSymbol = x as ITypeSymbol;
return typeSymbol == null ? x.ContainingType : x;
})
.Cast<ITypeSymbol>()
.WhereNotNull()
.DistinctBy(x => x.ToDisplayString())
.Count(x => !x.Equals(sourceSymbol));
return members;
}
示例10: FindUnsupportedV7Switch
private SyntaxNode FindUnsupportedV7Switch(SemanticModel model, SyntaxNode syntaxNode, List<RudeEditDiagnostic> diagnostics)
{
foreach (var node in syntaxNode.DescendantNodesAndSelf().Where(n => n.Kind() == SyntaxKind.SwitchStatement))
{
var switchExpression = ((SwitchStatementSyntax)node).Expression;
ITypeSymbol governingType = model.GetTypeInfo(switchExpression).Type;
if (!IsValidV6SwitchGoverningType(governingType))
{
return node;
}
}
return null;
}
示例11: GetElementAccessExpressions
private static IEnumerable<ElementAccessExpressionSyntax> GetElementAccessExpressions(SyntaxNode node)
{
return node == null ?
new ElementAccessExpressionSyntax[0] :
node.DescendantNodesAndSelf().Where(s => s.IsKind(SyntaxKind.ElementAccessExpression)).Cast<ElementAccessExpressionSyntax>();
}
示例12:
SyntaxNode ISyntaxRule.Process(SyntaxNode root)
{
root.DescendantNodesAndSelf()
.Where(n => n.IsKind(SyntaxKind.IfStatement) || n.IsKind(SyntaxKind.ElseClause));
return root;
}
示例13: AddNewMember
// public static IUnresolvedMember AddCodeDomMember (MonoDevelop.Projects.Project project, IUnresolvedTypeDefinition type, CodeTypeMember newMember)
// {
// bool isOpen;
// var data = TextFileProvider.Instance.GetTextEditorData (type.Region.FileName, out isOpen);
// var parsedDocument = TypeSystemService.ParseFile (data.FileName, data.MimeType, data.Text);
//
// var insertionPoints = GetInsertionPoints (data, parsedDocument, type);
//
// var suitableInsertionPoint = GetSuitableInsertionPoint (insertionPoints, type, newMember);
//
// var dotNetProject = project as DotNetProject;
// if (dotNetProject == null) {
// LoggingService.LogError ("Only .NET projects are supported.");
// return null;
// }
//
// var generator = dotNetProject.LanguageBinding.GetCodeDomProvider ();
// StringWriter sw = new StringWriter ();
// var options = new CodeGeneratorOptions ();
// options.IndentString = data.GetLineIndent (type.Region.BeginLine) + "\t";
// if (newMember is CodeMemberMethod)
// options.BracingStyle = "C";
// generator.GenerateCodeFromMember (newMember, sw, options);
//
// var code = sw.ToString ();
// if (!string.IsNullOrEmpty (code))
// suitableInsertionPoint.Insert (data, code);
// if (!isOpen) {
// try {
// File.WriteAllText (type.Region.FileName, data.Text);
// } catch (Exception e) {
// LoggingService.LogError (string.Format ("Failed to write file '{0}'.", type.Region.FileName), e);
// MessageService.ShowError (GettextCatalog.GetString ("Failed to write file '{0}'.", type.Region.FileName));
// }
// }
// var newDocument = TypeSystemService.ParseFile (data.FileName, data.MimeType, data.Text);
// return newDocument.ParsedFile.GetMember (suitableInsertionPoint.Location.Line, int.MaxValue);
// }
public static async Task AddNewMember (Projects.Project project, ITypeSymbol type, Location part, SyntaxNode newMember, CancellationToken cancellationToken = default(CancellationToken))
{
if (project == null)
throw new ArgumentNullException (nameof (project));
if (type == null)
throw new ArgumentNullException (nameof (type));
if (newMember == null)
throw new ArgumentNullException (nameof (newMember));
if (!type.IsDefinedInSource ())
throw new ArgumentException ("The given type needs to be defined in source code.", nameof (type));
var ws = MonoDevelop.Ide.TypeSystem.TypeSystemService.GetWorkspace (project.ParentSolution);
var projectId = ws.GetProjectId (project);
var docId = ws.GetDocumentId (projectId, part.SourceTree.FilePath);
var document = ws.GetDocument (docId, cancellationToken);
var root = await document.GetSyntaxRootAsync (cancellationToken).ConfigureAwait (false);
var typeDecl = (ClassDeclarationSyntax)root.FindNode (part.SourceSpan);
// for some reason the reducer doesn't reduce this
var systemVoid = newMember.DescendantNodesAndSelf ().OfType<QualifiedNameSyntax> ().FirstOrDefault (ma => ma.ToString () == "System.Void");
if (systemVoid != null) newMember = newMember.ReplaceNode (systemVoid, SyntaxFactory.ParseTypeName ("void"));
var newRoot = root.ReplaceNode (typeDecl, typeDecl.AddMembers ((MemberDeclarationSyntax)newMember.WithAdditionalAnnotations (Simplifier.Annotation, Formatter.Annotation)));
document = document.WithSyntaxRoot (newRoot);
var policy = project.Policies.Get<CSharpFormattingPolicy> ("text/x-csharp");
var textPolicy = project.Policies.Get<TextStylePolicy> ("text/x-csharp");
var projectOptions = policy.CreateOptions (textPolicy);
document = await Formatter.FormatAsync (document, Formatter.Annotation, projectOptions, cancellationToken).ConfigureAwait (false);
document = await Simplifier.ReduceAsync (document, Simplifier.Annotation, projectOptions, cancellationToken).ConfigureAwait (false);
var text = await document.GetTextAsync (cancellationToken).ConfigureAwait (false);
var newSolution = ws.CurrentSolution.WithDocumentText (docId, text);
ws.TryApplyChanges (newSolution);
}
示例14: InsertMemberWithCursor
public static async Task InsertMemberWithCursor (string operation, Projects.Project project, ITypeSymbol type, Location part, SyntaxNode newMember, CancellationToken cancellationToken = default(CancellationToken))
{
if (operation == null)
throw new ArgumentNullException (nameof (operation));
if (project == null)
throw new ArgumentNullException (nameof (project));
if (type == null)
throw new ArgumentNullException (nameof (type));
if (newMember == null)
throw new ArgumentNullException (nameof (newMember));
var ws = MonoDevelop.Ide.TypeSystem.TypeSystemService.GetWorkspace (project.ParentSolution);
var projectId = ws.GetProjectId (project);
var docId = ws.GetDocumentId (projectId, part.SourceTree.FilePath);
var document = ws.GetDocument (docId, cancellationToken);
var root = await document.GetSyntaxRootAsync (cancellationToken).ConfigureAwait (false);
var typeDecl = (ClassDeclarationSyntax)root.FindNode (part.SourceSpan);
// for some reason the reducer doesn't reduce this
var systemVoid = newMember.DescendantNodesAndSelf ().OfType<QualifiedNameSyntax> ().FirstOrDefault (ma => ma.ToString () == "System.Void");
if (systemVoid != null) newMember = newMember.ReplaceNode (systemVoid, SyntaxFactory.ParseTypeName ("void"));
var newRoot = root.ReplaceNode (typeDecl, typeDecl.AddMembers ((MemberDeclarationSyntax)newMember.WithAdditionalAnnotations (Simplifier.Annotation, Formatter.Annotation, insertedMemberAnnotation)));
var doc = await IdeApp.Workbench.OpenDocument (part.SourceTree.FilePath, project, true);
var policy = project.Policies.Get<CSharpFormattingPolicy> ("text/x-csharp");
var textPolicy = project.Policies.Get<TextStylePolicy> ("text/x-csharp");
var projectOptions = policy.CreateOptions (textPolicy);
document = document.WithSyntaxRoot (newRoot);
document = await Formatter.FormatAsync (document, Formatter.Annotation, projectOptions, cancellationToken).ConfigureAwait (false);
document = await Simplifier.ReduceAsync (document, Simplifier.Annotation, projectOptions, cancellationToken).ConfigureAwait (false);
root = await document.GetSyntaxRootAsync (cancellationToken).ConfigureAwait (false);
var node = root.GetAnnotatedNodes (insertedMemberAnnotation).Single ();
Application.Invoke (async delegate {
var insertionPoints = InsertionPointService.GetInsertionPoints (
doc.Editor,
await doc.UpdateParseDocument (),
type,
part.SourceSpan.Start
);
var options = new InsertionModeOptions (
operation,
insertionPoints,
point => {
if (!point.Success)
return;
var text = node.ToString ();
point.InsertionPoint.Insert (doc.Editor, doc, text);
}
);
doc.Editor.StartInsertionMode (options);
});
}
示例15: AffectedExpressions
private static IEnumerable<SyntaxNode> AffectedExpressions(SyntaxNode node)
{
return node
.DescendantNodesAndSelf()
.Where(n => SideEffectExpressions.Any(s => s.Kinds.Any(n.IsKind)))
.Select(n => SideEffectExpressions.Single(s => s.Kinds.Any(n.IsKind)).AffectedExpression(n));
}