Beispiel #1
0
        /// <summary>
        /// Return <see cref="DiagnosticAnalyzer.SupportedDiagnostics"/> of given <paramref name="analyzer"/>.
        /// </summary>
        public ImmutableArray <DiagnosticDescriptor> GetSupportedDiagnosticDescriptors(
            DiagnosticAnalyzer analyzer,
            AnalyzerExecutor analyzerExecutor)
        {
            var descriptors = _descriptorCache.GetValue(analyzer, key =>
            {
                var supportedDiagnostics = ImmutableArray <DiagnosticDescriptor> .Empty;

                // Catch Exception from analyzer.SupportedDiagnostics
                analyzerExecutor.ExecuteAndCatchIfThrows(analyzer, () => { supportedDiagnostics = analyzer.SupportedDiagnostics; });

                var handler = new EventHandler <Exception>((sender, ex) =>
                {
                    var diagnostic = AnalyzerExecutor.GetAnalyzerDiagnostic(analyzer, ex);
                    analyzerExecutor.OnAnalyzerException?.Invoke(ex, analyzer, diagnostic);
                });

                // Subscribe for exceptions from lazily evaluated localizable strings in the descriptors.
                foreach (var descriptor in supportedDiagnostics)
                {
                    descriptor.Title.OnException         += handler;
                    descriptor.MessageFormat.OnException += handler;
                    descriptor.Description.OnException   += handler;
                }

                return(Tuple.Create(supportedDiagnostics, handler));
            });

            return(descriptors.Item1);
        }