Пример #1
0
 private static string GetPluralName(RenameScope scope)
 {
     return(scope switch
     {
         RenameScope.Type => "types",
         RenameScope.Member => "members",
         RenameScope.Local => "locals",
         _ => throw new InvalidOperationException($"Unknown enum value '{scope}'."),
     });
Пример #2
0
        public async Task <IEnumerable <ISymbol> > GetSymbolsAsync(
            Project project,
            RenameScope scope,
            CancellationToken cancellationToken = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var compilationWithAnalyzersOptions = new CompilationWithAnalyzersOptions(
                options: default(AnalyzerOptions),
                onAnalyzerException: default(Action <Exception, DiagnosticAnalyzer, Diagnostic>),
                concurrentAnalysis: true,
                logAnalyzerExecutionTime: false,
                reportSuppressedDiagnostics: false);

            Compilation compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);

            ImmutableArray <SymbolKind> symbolKinds = scope switch
            {
                RenameScope.Type => ImmutableArray.Create(SymbolKind.NamedType),
                RenameScope.Member => ImmutableArray.Create(SymbolKind.Field, SymbolKind.Event, SymbolKind.Method, SymbolKind.Property),
                RenameScope.Local => ImmutableArray.Create(SymbolKind.Field, SymbolKind.Event, SymbolKind.Method, SymbolKind.Property),
                _ => throw new InvalidOperationException(),
            };

            var analyzer = new Analyzer(
                symbolKinds,
                IncludeGeneratedCode);

            var compilationWithAnalyzers = new CompilationWithAnalyzers(
                compilation,
                ImmutableArray.Create((DiagnosticAnalyzer)analyzer),
                compilationWithAnalyzersOptions);

            ImmutableArray <Diagnostic> _ = await compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync(cancellationToken).ConfigureAwait(false);

            return(analyzer.Symbols);
        }