/// <summary> /// Rewrites all members in the document. /// </summary> public SyntaxNode Process(SyntaxNode syntax, ProcessorContext context) { var virtualEvents = new HashSet <string>(); var semantic = context.Compilation.GetSemanticModel(syntax.SyntaxTree); if (semantic != null) { var events = new EventVisitor(); events.Visit(syntax); foreach (var symbol in events.Types.SelectMany(type => semantic .LookupNamespacesAndTypes(type.Span.Start, name: type.Identifier.ValueText) .OfType <INamedTypeSymbol>()).Where(x => x != null)) { var baseType = symbol.BaseType; while (baseType != null) { foreach (var e in baseType.GetMembers().OfType <IEventSymbol>().Where(e => e.IsVirtual)) { virtualEvents.Add(e.Name); } baseType = baseType.BaseType; } } } return(syntax = new CSharpRewriteVisitor(virtualEvents).Visit(syntax)); }
/// <summary> /// Rewrites all members in the document. /// </summary> public async Task <Document> ProcessAsync(Document document, CancellationToken cancellationToken = default) { var syntax = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); syntax = new CSharpRewriteVisitor(document).Visit(syntax); return(document.WithSyntaxRoot(syntax)); }
/// <summary> /// Removes and sorts namespaces. /// </summary> public async Task <Document> ProcessAsync(Document document, CancellationToken cancellationToken = default) { // This codefix is available for both C# and VB //document = await document.ApplyCodeFixAsync(CodeFixNames.All.RemoveUnnecessaryImports); var generator = SyntaxGenerator.GetGenerator(document); var syntax = await document.GetSyntaxRootAsync(); var imports = generator.GetNamespaceImports(syntax).Select(generator.GetName).ToArray(); Array.Sort(imports); if (document.Project.Language == LanguageNames.CSharp) { syntax = new CSharpRewriteVisitor().Visit(syntax); } else { syntax = new VisualBasicRewriteVisitor().Visit(syntax); } return(document.WithSyntaxRoot(generator.AddNamespaceImports(syntax, imports.Select(generator.NamespaceImportDeclaration)))); }