Esempio n. 1
0
 private void LoadAnalyzers(AnalyzerAssembly analyzerAssembly, string language, ref ImmutableArray <DiagnosticAnalyzer> .Builder builder)
 {
     if (analyzerAssembly.AnalyzersByLanguage.TryGetValue(language, out ImmutableArray <DiagnosticAnalyzer> analyzers))
     {
         foreach (DiagnosticAnalyzer analyzer in analyzers)
         {
             if (ShouldIncludeAnalyzer(analyzer))
             {
                 builder.Add(analyzer);
             }
         }
     }
 }
Esempio n. 2
0
 private void LoadFixers(AnalyzerAssembly analyzerAssembly, string language, ref ImmutableArray <CodeFixProvider> .Builder builder)
 {
     if (analyzerAssembly.FixersByLanguage.TryGetValue(language, out ImmutableArray <CodeFixProvider> fixers))
     {
         foreach (CodeFixProvider fixer in fixers)
         {
             if (ShouldIncludeFixer(fixer))
             {
                 builder.Add(fixer);
             }
         }
     }
 }
Esempio n. 3
0
        private (ImmutableArray <DiagnosticAnalyzer> analyzers, ImmutableArray <CodeFixProvider> fixers) GetAnalyzersAndFixers(
            Project project,
            bool loadFixers = true)
        {
            string language = project.Language;

            ImmutableArray <DiagnosticAnalyzer> .Builder analyzers = ImmutableArray.CreateBuilder <DiagnosticAnalyzer>();
            ImmutableArray <CodeFixProvider> .Builder    fixers    = ImmutableArray.CreateBuilder <CodeFixProvider>();

            if (_defaultAnalyzers.TryGetValue(language, out ImmutableArray <DiagnosticAnalyzer> defaultAnalyzers))
            {
                analyzers.AddRange(defaultAnalyzers);
            }
            else
            {
                foreach (AnalyzerAssembly analyzerAssembly in DefaultAssemblies)
                {
                    LoadAnalyzers(analyzerAssembly, language, ref analyzers);
                }

                _defaultAnalyzers.Add(language, analyzers.ToImmutableArray());
            }

            if (loadFixers)
            {
                if (_defaultFixers.TryGetValue(language, out ImmutableArray <CodeFixProvider> defaultFixers))
                {
                    fixers.AddRange(defaultFixers);
                }
                else
                {
                    foreach (AnalyzerAssembly analyzerAssembly in DefaultAssemblies)
                    {
                        LoadFixers(analyzerAssembly, language, ref fixers);
                    }

                    _defaultFixers.Add(language, fixers.ToImmutableArray());
                }
            }

            if (!Options.IgnoreAnalyzerReferences)
            {
                foreach (Assembly assembly in project.AnalyzerReferences
                         .Distinct()
                         .OfType <AnalyzerFileReference>()
                         .Select(f => f.GetAssembly())
                         .Where(f => !_defaultAssemblies.ContainsKey(f.FullName)))
                {
                    if (!_cache.TryGetValue(assembly.FullName, out AnalyzerAssembly analyzerAssembly))
                    {
                        analyzerAssembly = AnalyzerAssembly.Load(assembly);
                        _cache.Add(analyzerAssembly.FullName, analyzerAssembly);

                        OnAnalyzerAssemblyAdded(new AnalyzerAssemblyEventArgs(analyzerAssembly));
                    }

                    LoadAnalyzers(analyzerAssembly, language, ref analyzers);

                    if (loadFixers)
                    {
                        LoadFixers(analyzerAssembly, language, ref fixers);
                    }
                }
            }

            return(analyzers.ToImmutableArray(), fixers.ToImmutableArray());
        }
Esempio n. 4
0
 public AnalyzerAssemblyEventArgs(AnalyzerAssembly analyzerAssembly)
 {
     AnalyzerAssembly = analyzerAssembly;
 }