コード例 #1
0
        /// <summary>
        /// Executes the syntax node actions on the given syntax nodes.
        /// </summary>
        /// <param name="actions"><see cref="AnalyzerActions"/> whose code block start actions and end actions are to be executed.</param>
        /// <param name="nodes">Syntax nodes to be analyzed.</param>
        /// <param name="semanticModel">SemanticModel to be used in the analysis.</param>
        /// <param name="analyzerOptions">Analyzer options.</param>
        /// <param name="addDiagnostic">Delegate to add diagnostics.</param>
        /// <param name="continueOnAnalyzerException">Predicate to decide if exceptions from the action should be handled or not.</param>
        /// <param name="getKind">Delegate to compute language specific syntax kind for a syntax node.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        public static void ExecuteSyntaxNodeActions <TLanguageKindEnum>(
            AnalyzerActions actions,
            IEnumerable <SyntaxNode> nodes,
            SemanticModel semanticModel,
            AnalyzerOptions analyzerOptions,
            Action <Diagnostic> addDiagnostic,
            Func <Exception, DiagnosticAnalyzer, bool> continueOnAnalyzerException,
            Func <SyntaxNode, TLanguageKindEnum> getKind,
            CancellationToken cancellationToken)
            where TLanguageKindEnum : struct
        {
            VerifyArguments(nodes, getKind, semanticModel, actions, analyzerOptions, addDiagnostic, continueOnAnalyzerException);

            var syntaxNodeActions = actions.GetSyntaxNodeActions <TLanguageKindEnum>();

            foreach (var syntaxNodeAction in syntaxNodeActions)
            {
                var action = syntaxNodeAction.Action;
                var kinds  = syntaxNodeAction.Kinds;

                foreach (var node in nodes)
                {
                    if (kinds.Contains(getKind(node)))
                    {
                        ExecuteSyntaxNodeAction(action, node, syntaxNodeAction.Analyzer, semanticModel, analyzerOptions, addDiagnostic, continueOnAnalyzerException, cancellationToken);
                    }
                }
            }
        }
コード例 #2
0
            private static ImmutableDictionary <DiagnosticAnalyzer, ImmutableDictionary <TLanguageKindEnum, ImmutableArray <SyntaxNodeAnalyzerAction <TLanguageKindEnum> > > > CreateNodeActionsByKind(
                AnalyzerActions analyzerActions)
            {
                var nodeActions = analyzerActions.GetSyntaxNodeActions <TLanguageKindEnum>();
                ImmutableDictionary <DiagnosticAnalyzer, ImmutableDictionary <TLanguageKindEnum, ImmutableArray <SyntaxNodeAnalyzerAction <TLanguageKindEnum> > > > analyzerActionsByKind;

                if (!nodeActions.IsEmpty)
                {
                    var nodeActionsByAnalyzers = nodeActions.GroupBy(a => a.Analyzer);
                    var builder = ImmutableDictionary.CreateBuilder <DiagnosticAnalyzer, ImmutableDictionary <TLanguageKindEnum, ImmutableArray <SyntaxNodeAnalyzerAction <TLanguageKindEnum> > > >();
                    foreach (var analyzerAndActions in nodeActionsByAnalyzers)
                    {
                        ImmutableDictionary <TLanguageKindEnum, ImmutableArray <SyntaxNodeAnalyzerAction <TLanguageKindEnum> > > actionsByKind;
                        if (analyzerAndActions.Any())
                        {
                            actionsByKind = AnalyzerExecutor.GetNodeActionsByKind(analyzerAndActions);
                        }
                        else
                        {
                            actionsByKind = ImmutableDictionary <TLanguageKindEnum, ImmutableArray <SyntaxNodeAnalyzerAction <TLanguageKindEnum> > > .Empty;
                        }

                        builder.Add(analyzerAndActions.Key, actionsByKind);
                    }

                    analyzerActionsByKind = builder.ToImmutable();
                }
                else
                {
                    analyzerActionsByKind = ImmutableDictionary <DiagnosticAnalyzer, ImmutableDictionary <TLanguageKindEnum, ImmutableArray <SyntaxNodeAnalyzerAction <TLanguageKindEnum> > > > .Empty;
                }

                return(analyzerActionsByKind);
            }
コード例 #3
0
        /// <summary>
        /// Executes the syntax node actions on the given syntax nodes.
        /// </summary>
        /// <param name="actions"><see cref="AnalyzerActions"/> whose code block start actions and end actions are to be executed.</param>
        /// <param name="nodes">Syntax nodes to be analyzed.</param>
        /// <param name="semanticModel">SemanticModel to be used in the analysis.</param>
        /// <param name="getKind">Delegate to compute language specific syntax kind for a syntax node.</param>
        public void ExecuteSyntaxNodeActions <TLanguageKindEnum>(
            AnalyzerActions actions,
            IEnumerable <SyntaxNode> nodes,
            SemanticModel semanticModel,
            Func <SyntaxNode, TLanguageKindEnum> getKind)
            where TLanguageKindEnum : struct
        {
            var syntaxNodeActions = actions.GetSyntaxNodeActions <TLanguageKindEnum>();

            foreach (var syntaxNodeAction in syntaxNodeActions)
            {
                foreach (var node in nodes)
                {
                    if (syntaxNodeAction.Kinds.Contains(getKind(node)))
                    {
                        ExecuteSyntaxNodeAction(syntaxNodeAction, node, semanticModel);
                    }
                }
            }
        }