Example #1
0
            public SymbolReferenceFinder(
                AbstractAddImportFeatureService <TSimpleNameSyntax> owner,
                Document document, SemanticModel semanticModel,
                string diagnosticId, SyntaxNode node,
                ISymbolSearchService symbolSearchService,
                bool searchReferenceAssemblies,
                ImmutableArray <PackageSource> packageSources,
                CancellationToken cancellationToken)
            {
                _owner         = owner;
                _document      = document;
                _semanticModel = semanticModel;
                _diagnosticId  = diagnosticId;
                _node          = node;

                _symbolSearchService       = symbolSearchService;
                _searchReferenceAssemblies = searchReferenceAssemblies;
                _packageSources            = packageSources;

                if (_searchReferenceAssemblies || packageSources.Length > 0)
                {
                    Contract.ThrowIfNull(symbolSearchService);
                }

                _syntaxFacts = document.GetLanguageService <ISyntaxFactsService>();

                _namespacesInScope = GetNamespacesInScope(cancellationToken);
            }
Example #2
0
        public async Task <ImmutableArray <AddImportFixData> > GetFixesAsync(
            Document document, TextSpan span, string diagnosticId, ISymbolSearchService symbolSearchService,
            bool searchReferenceAssemblies, ImmutableArray <PackageSource> packageSources,
            CancellationToken cancellationToken)
        {
            var session = await document.Project.Solution.TryCreateCodeAnalysisServiceSessionAsync(
                AddImportOptions.OutOfProcessAllowed, WellKnownExperimentNames.RoslynFeatureOOP,
                new RemoteSymbolSearchService(symbolSearchService, cancellationToken), cancellationToken).ConfigureAwait(false);

            using (session)
            {
                if (session == null)
                {
                    return(await GetFixesInCurrentProcessAsync(
                               document, span, diagnosticId, symbolSearchService,
                               searchReferenceAssemblies, packageSources, cancellationToken).ConfigureAwait(false));
                }
                else
                {
                    return(await GetFixesInRemoteProcessAsync(
                               session, document, span, diagnosticId,
                               searchReferenceAssemblies, packageSources).ConfigureAwait(false));
                }
            }
        }
Example #3
0
        public async Task <ImmutableArray <AddImportFixData> > GetFixesAsync(
            Document document, TextSpan span, string diagnosticId, bool placeSystemNamespaceFirst,
            ISymbolSearchService symbolSearchService, bool searchReferenceAssemblies,
            ImmutableArray <PackageSource> packageSources, CancellationToken cancellationToken)
        {
            var callbackTarget = new RemoteSymbolSearchService(symbolSearchService, cancellationToken);
            var result         = await document.Project.Solution.TryRunCodeAnalysisRemoteAsync <ImmutableArray <AddImportFixData> >(
                RemoteFeatureOptions.AddImportEnabled,
                callbackTarget,
                nameof(IRemoteAddImportFeatureService.GetFixesAsync),
                new object[]
            {
                document.Id,
                span,
                diagnosticId,
                placeSystemNamespaceFirst,
                searchReferenceAssemblies,
                packageSources
            },
                cancellationToken).ConfigureAwait(false);

            var documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            if (!result.IsDefault)
            {
                return(result);
            }

            return(await GetFixesInCurrentProcessAsync(
                       document, span, diagnosticId, placeSystemNamespaceFirst,
                       symbolSearchService, searchReferenceAssemblies,
                       packageSources, cancellationToken).ConfigureAwait(false));
        }
Example #4
0
 public RemoteSymbolSearchService(
     ISymbolSearchService symbolSearchService,
     CancellationToken cancellationToken)
 {
     _symbolSearchService = symbolSearchService;
     _cancellationToken   = cancellationToken;
 }
 /// <summary>Values for these parameters can be provided (during testing) for mocking purposes.</summary>
 protected AbstractAddImportCodeFixProvider(
     IPackageInstallerService packageInstallerService = null,
     ISymbolSearchService symbolSearchService         = null)
 {
     _packageInstallerService = packageInstallerService;
     _symbolSearchService     = symbolSearchService;
 }
Example #6
0
            private async Task FindNugetTypeReferencesAsync(
                PackageSource source,
                ISymbolSearchService searchService,
                IPackageInstallerService installerService,
                List <Reference> allReferences,
                TSimpleNameSyntax nameNode,
                string name,
                int arity,
                bool isAttributeSearch,
                CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var results = searchService.FindPackagesWithType(source.Name, name, arity, cancellationToken);

                var project   = _document.Project;
                var projectId = project.Id;
                var workspace = project.Solution.Workspace;

                foreach (var result in results)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    await HandleNugetReferenceAsync(
                        source.Source, installerService, allReferences, nameNode,
                        project, isAttributeSearch, result, weight : allReferences.Count).ConfigureAwait(false);
                }
            }
Example #7
0
 /// <summary>
 /// Values for these parameters can be provided (during testing) for mocking purposes.
 /// </summary>
 protected AbstractAddPackageCodeFixProvider(
     IPackageInstallerService packageInstallerService,
     ISymbolSearchService symbolSearchService)
 {
     _packageInstallerService = packageInstallerService;
     _symbolSearchService     = symbolSearchService;
 }
Example #8
0
 /// <summary>
 /// Values for these parameters can be provided (during testing) for mocking purposes.
 /// </summary>
 protected AbstractAddMissingReferenceCodeFixProvider(
     IPackageInstallerService packageInstallerService = null,
     ISymbolSearchService symbolSearchService         = null)
 {
     _packageInstallerService = packageInstallerService;
     _symbolSearchService     = symbolSearchService;
 }
 /// <summary>
 /// Values for these parameters can be provided (during testing) for mocking purposes.
 /// </summary> 
 protected AbstractAddPackageCodeFixProvider(
     IPackageInstallerService packageInstallerService,
     ISymbolSearchService symbolSearchService)
 {
     _packageInstallerService = packageInstallerService;
     _symbolSearchService = symbolSearchService;
 }
Example #10
0
 public SymbolsController(
     ISymbolSearchService searchService,
     ILogger <SymbolsController> logger)
 {
     _searchService = searchService;
     _logger        = logger;
 }
Example #11
0
 public RemoteSymbolSearchService(
     ISymbolSearchService symbolSearchService,
     CancellationToken shutdownCancellationToken)
 {
     _symbolSearchService  = symbolSearchService;
     _shutdownCancellation = shutdownCancellationToken;
 }
        public async Task <ImmutableArray <(Diagnostic Diagnostic, ImmutableArray <AddImportFixData> Fixes)> > GetFixesForDiagnosticsAsync(
            Document document, TextSpan span, ImmutableArray <Diagnostic> diagnostics, int maxResultsPerDiagnostic,
            ISymbolSearchService symbolSearchService, bool searchReferenceAssemblies,
            ImmutableArray <PackageSource> packageSources, CancellationToken cancellationToken)
        {
            // We might have multiple different diagnostics covering the same span.  Have to
            // process them all as we might produce different fixes for each diagnostic.

            var documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            var placeSystemNamespaceFirst = documentOptions.GetOption(GenerationOptions.PlaceSystemNamespaceFirst);

            var fixesForDiagnosticBuilder = ArrayBuilder <(Diagnostic, ImmutableArray <AddImportFixData>)> .GetInstance();

            foreach (var diagnostic in diagnostics)
            {
                var fixes = await GetFixesAsync(
                    document, span, diagnostic.Id, maxResultsPerDiagnostic,
                    placeSystemNamespaceFirst, symbolSearchService, searchReferenceAssemblies,
                    packageSources, cancellationToken).ConfigureAwait(false);

                fixesForDiagnosticBuilder.Add((diagnostic, fixes));
            }

            return(fixesForDiagnosticBuilder.ToImmutableAndFree());
        }
Example #13
0
        public async Task <ImmutableArray <AddImportFixData> > GetFixesAsync(
            Document document, TextSpan span, string diagnosticId, int maxResults,
            bool placeSystemNamespaceFirst, bool allowInHiddenRegions,
            ISymbolSearchService symbolSearchService, bool searchReferenceAssemblies,
            ImmutableArray <PackageSource> packageSources, CancellationToken cancellationToken)
        {
            var client = await RemoteHostClient.TryGetClientAsync(document.Project, cancellationToken).ConfigureAwait(false);

            if (client != null)
            {
                var result = await client.TryInvokeAsync <IRemoteMissingImportDiscoveryService, ImmutableArray <AddImportFixData> >(
                    document.Project.Solution,
                    (service, solutionInfo, callbackId, cancellationToken) =>
                    service.GetFixesAsync(solutionInfo, callbackId, document.Id, span, diagnosticId, maxResults, placeSystemNamespaceFirst, allowInHiddenRegions, searchReferenceAssemblies, packageSources, cancellationToken),
                    callbackTarget : symbolSearchService,
                    cancellationToken).ConfigureAwait(false);

                return(result.HasValue ? result.Value : ImmutableArray <AddImportFixData> .Empty);
            }

            return(await GetFixesInCurrentProcessAsync(
                       document, span, diagnosticId, maxResults,
                       placeSystemNamespaceFirst, allowInHiddenRegions,
                       symbolSearchService, searchReferenceAssemblies,
                       packageSources, cancellationToken).ConfigureAwait(false));
        }
Example #14
0
            private async Task FindReferenceAssemblyTypeReferencesAsync(
                ISymbolSearchService searchService,
                List <Reference> allReferences,
                TSimpleNameSyntax nameNode,
                string name,
                int arity,
                bool isAttributeSearch,
                CancellationToken cancellationToken)
            {
                cancellationToken.ThrowIfCancellationRequested();
                var results = await searchService.FindReferenceAssembliesWithTypeAsync(
                    name, arity, cancellationToken).ConfigureAwait(false);

                if (results.IsDefault)
                {
                    return;
                }

                var project   = _document.Project;
                var projectId = project.Id;
                var workspace = project.Solution.Workspace;

                foreach (var result in results)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    await HandleReferenceAssemblyReferenceAsync(
                        allReferences, nameNode, project,
                        isAttributeSearch, result, weight : allReferences.Count, cancellationToken : cancellationToken).ConfigureAwait(false);
                }
            }
Example #15
0
        public async Task <ImmutableArray <AddImportFixData> > GetFixesAsync(
            Document document, TextSpan span, string diagnosticId, int maxResults, bool placeSystemNamespaceFirst,
            ISymbolSearchService symbolSearchService, bool searchReferenceAssemblies,
            ImmutableArray <PackageSource> packageSources, CancellationToken cancellationToken)
        {
            if (RemoteSupportedLanguages.IsSupported(document.Project.Language))
            {
                var callbackTarget = new RemoteSymbolSearchService(symbolSearchService, cancellationToken);
                var result         = await document.Project.Solution.TryRunCodeAnalysisRemoteAsync <IList <AddImportFixData> >(
                    callbackTarget,
                    nameof(IRemoteAddImportFeatureService.GetFixesAsync),
                    new object[]
                {
                    document.Id,
                    span,
                    diagnosticId,
                    maxResults,
                    placeSystemNamespaceFirst,
                    searchReferenceAssemblies,
                    packageSources
                },
                    cancellationToken).ConfigureAwait(false);

                if (result != null)
                {
                    return(result.ToImmutableArray());
                }
            }

            return(await GetFixesInCurrentProcessAsync(
                       document, span, diagnosticId, maxResults, placeSystemNamespaceFirst,
                       symbolSearchService, searchReferenceAssemblies,
                       packageSources, cancellationToken).ConfigureAwait(false));
        }
Example #16
0
        public async Task <ImmutableArray <AddImportFixData> > GetFixesAsync(
            Document document, TextSpan span, string diagnosticId, bool placeSystemNamespaceFirst,
            ISymbolSearchService symbolSearchService, bool searchReferenceAssemblies,
            ImmutableArray <PackageSource> packageSources, CancellationToken cancellationToken)
        {
            var callbackTarget = new RemoteSymbolSearchService(symbolSearchService, cancellationToken);
            var session        = await document.Project.Solution.TryCreateCodeAnalysisServiceSessionAsync(
                RemoteFeatureOptions.AddImportEnabled, callbackTarget, cancellationToken).ConfigureAwait(false);

            var documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

            using (session)
            {
                if (session == null)
                {
                    return(await GetFixesInCurrentProcessAsync(
                               document, span, diagnosticId, placeSystemNamespaceFirst,
                               symbolSearchService, searchReferenceAssemblies,
                               packageSources, cancellationToken).ConfigureAwait(false));
                }
                else
                {
                    return(await GetFixesInRemoteProcessAsync(
                               session, document, span, diagnosticId, placeSystemNamespaceFirst,
                               searchReferenceAssemblies, packageSources).ConfigureAwait(false));
                }
            }
        }
Example #17
0
 public TransactionService(IMediator mediator,
                           IDomainNotificationHandler <DomainNotification> notifications,
                           ISymbolSearchService symbolSearchService)
 {
     _mediator            = mediator;
     _notifications       = notifications;
     _symbolSearchService = symbolSearchService;
 }
Example #18
0
        private async Task <ImmutableArray <AddImportFixData> > GetFixesInCurrentProcessAsync(
            Document document, TextSpan span, string diagnosticId, ISymbolSearchService symbolSearchService,
            bool searchReferenceAssemblies, ImmutableArray <PackageSource> packageSources, CancellationToken cancellationToken)
        {
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var node = root.FindToken(span.Start, findInsideTrivia: true)
                       .GetAncestor(n => n.Span.Contains(span) && n != root);

            var result = ArrayBuilder <AddImportFixData> .GetInstance();

            if (node != null)
            {
                var documentOptions = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

                var placeSystemNamespaceFirst = documentOptions.GetOption(
                    GenerationOptions.PlaceSystemNamespaceFirst);

                using (Logger.LogBlock(FunctionId.Refactoring_AddImport, cancellationToken))
                {
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        if (this.CanAddImport(node, cancellationToken))
                        {
                            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                            var allSymbolReferences = await FindResultsAsync(
                                document, semanticModel, diagnosticId, node, symbolSearchService,
                                searchReferenceAssemblies, packageSources, cancellationToken).ConfigureAwait(false);

                            // Nothing found at all. No need to proceed.
                            foreach (var reference in allSymbolReferences)
                            {
                                cancellationToken.ThrowIfCancellationRequested();

                                var fixData = await reference.TryGetFixDataAsync(document, node, placeSystemNamespaceFirst, cancellationToken).ConfigureAwait(false);

                                result.AddIfNotNull(fixData);
                            }
                        }
                    }
                }
            }

            return(result.ToImmutableAndFree());
        }
Example #19
0
        private async Task <ImmutableArray <AddImportFixData> > GetFixesInCurrentProcessAsync(
            Document document, TextSpan span, string diagnosticId, int maxResults,
            bool allowInHiddenRegions,
            ISymbolSearchService symbolSearchService, AddImportOptions options,
            ImmutableArray <PackageSource> packageSources, CancellationToken cancellationToken)
        {
            var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

            var node = root.FindToken(span.Start, findInsideTrivia: true)
                       .GetAncestor(n => n.Span.Contains(span) && n != root);

            using var _ = ArrayBuilder <AddImportFixData> .GetInstance(out var result);

            if (node != null)
            {
                using (Logger.LogBlock(FunctionId.Refactoring_AddImport, cancellationToken))
                {
                    if (!cancellationToken.IsCancellationRequested)
                    {
                        if (CanAddImport(node, allowInHiddenRegions, cancellationToken))
                        {
                            var semanticModel = await document.GetRequiredSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                            var allSymbolReferences = await FindResultsAsync(
                                document, semanticModel, diagnosticId, node, maxResults, symbolSearchService,
                                options, packageSources, cancellationToken).ConfigureAwait(false);

                            // Nothing found at all. No need to proceed.
                            foreach (var reference in allSymbolReferences)
                            {
                                cancellationToken.ThrowIfCancellationRequested();

                                var fixData = await reference.TryGetFixDataAsync(document, node, allowInHiddenRegions, cancellationToken).ConfigureAwait(false);

                                result.AddIfNotNull(fixData);
                            }
                        }
                    }
                }
            }

            return(result.ToImmutable());
        }
        private async Task <ImmutableArray <PackageWithAssemblyResult> > FindMatchingPackagesAsync(
            PackageSource source,
            ISymbolSearchService searchService,
            ISet <string> assemblyNames,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var result = new HashSet <PackageWithAssemblyResult>();

            foreach (var assemblyName in assemblyNames)
            {
                var packagesWithAssembly = await searchService.FindPackagesWithAssemblyAsync(
                    source.Name, assemblyName, cancellationToken).ConfigureAwait(false);

                result.AddRange(packagesWithAssembly);
            }

            // Ensure the packages are sorted by rank.
            var sortedPackages = result.ToImmutableArray().Sort();

            return(sortedPackages);
        }
        private async Task<ImmutableArray<PackageWithAssemblyResult>> FindMatchingPackagesAsync(
            PackageSource source,
            ISymbolSearchService searchService,
            IPackageInstallerService installerService,
            ISet<string> assemblyNames,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var result = new HashSet<PackageWithAssemblyResult>();

            foreach (var assemblyName in assemblyNames)
            {
                var packagesWithAssembly = await searchService.FindPackagesWithAssemblyAsync(
                    source.Name, assemblyName, cancellationToken).ConfigureAwait(false);

                result.AddRange(packagesWithAssembly);
            }

            // Ensure the packages are sorted by rank.
            var sortedPackages = result.ToImmutableArray().Sort();

            return sortedPackages;
        }
Example #22
0
        private async Task <ImmutableArray <PackageWithAssemblyResult> > FindMatchingPackagesAsync(
            PackageSource source,
            ISymbolSearchService searchService,
            IPackageInstallerService installerService,
            ISet <AssemblyIdentity> uniqueIdentities,
            ArrayBuilder <CodeAction> builder,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var result = new HashSet <PackageWithAssemblyResult>();

            foreach (var identity in uniqueIdentities)
            {
                var packagesWithAssembly = await searchService.FindPackagesWithAssemblyAsync(
                    source.Name, identity.Name, cancellationToken).ConfigureAwait(false);

                result.AddRange(packagesWithAssembly);
            }

            // Ensure the packages are sorted by rank.
            var sortedPackages = result.ToImmutableArray().Sort();

            return(sortedPackages);
        }
Example #23
0
            public SymbolReferenceFinder(
                AbstractAddImportFeatureService <TSimpleNameSyntax> owner,
                Document document,
                SemanticModel semanticModel,
                string diagnosticId,
                SyntaxNode node,
                ISymbolSearchService symbolSearchService,
                AddImportOptions options,
                ImmutableArray <PackageSource> packageSources,
                CancellationToken cancellationToken)
            {
                _owner         = owner;
                _document      = document;
                _semanticModel = semanticModel;
                _diagnosticId  = diagnosticId;
                _node          = node;

                _symbolSearchService = symbolSearchService;
                _options             = options;
                _packageSources      = packageSources;
                _syntaxFacts         = document.GetLanguageService <ISyntaxFactsService>();

                _namespacesInScope = GetNamespacesInScope(cancellationToken);
            }
Example #24
0
 public RemoteSymbolSearchService(ISymbolSearchService symbolSearchService)
 {
     _symbolSearchService = symbolSearchService;
 }
 /// <summary>
 /// Values for these parameters can be provided (during testing) for mocking purposes.
 /// </summary> 
 protected AbstractAddSpecificPackageCodeFixProvider(
     IPackageInstallerService packageInstallerService = null,
     ISymbolSearchService symbolSearchService = null)
     : base(packageInstallerService, symbolSearchService)
 {
 }
 /// <summary>
 /// Values for these parameters can be provided (during testing) for mocking purposes.
 /// </summary>
 protected AbstractAddSpecificPackageCodeFixProvider(
     IPackageInstallerService packageInstallerService = null,
     ISymbolSearchService symbolSearchService         = null)
     : base(packageInstallerService, symbolSearchService)
 {
 }
 /// <summary>
 /// Values for these parameters can be provided (during testing) for mocking purposes.
 /// </summary> 
 protected AbstractAddMissingReferenceCodeFixProvider(
     IPackageInstallerService packageInstallerService = null,
     ISymbolSearchService symbolSearchService = null)
     : base(packageInstallerService, symbolSearchService)
 {
 }
        private async Task <ImmutableArray <Reference> > FindResultsAsync(
            Document document, SemanticModel semanticModel, string diagnosticId, SyntaxNode node, int maxResults, ISymbolSearchService symbolSearchService,
            bool searchReferenceAssemblies, ImmutableArray <PackageSource> packageSources, CancellationToken cancellationToken)
        {
            // Caches so we don't produce the same data multiple times while searching
            // all over the solution.
            var project                = document.Project;
            var projectToAssembly      = new ConcurrentDictionary <Project, AsyncLazy <IAssemblySymbol> >(concurrencyLevel: 2, capacity: project.Solution.ProjectIds.Count);
            var referenceToCompilation = new ConcurrentDictionary <PortableExecutableReference, Compilation>(concurrencyLevel: 2, capacity: project.Solution.Projects.Sum(p => p.MetadataReferences.Count));

            var finder = new SymbolReferenceFinder(
                this, document, semanticModel, diagnosticId, node, symbolSearchService,
                searchReferenceAssemblies, packageSources, cancellationToken);

            // Look for exact matches first:
            var exactReferences = await FindResultsAsync(projectToAssembly, referenceToCompilation, project, maxResults, finder, exact : true, cancellationToken : cancellationToken).ConfigureAwait(false);

            if (exactReferences.Length > 0)
            {
                return(exactReferences);
            }

            // No exact matches found.  Fall back to fuzzy searching.
            // Only bother doing this for host workspaces.  We don't want this for
            // things like the Interactive workspace as this will cause us to
            // create expensive bk-trees which we won't even be able to save for
            // future use.
            if (!IsHostOrTestOrRemoteWorkspace(project))
            {
                return(ImmutableArray <Reference> .Empty);
            }

            var fuzzyReferences = await FindResultsAsync(projectToAssembly, referenceToCompilation, project, maxResults, finder, exact : false, cancellationToken : cancellationToken).ConfigureAwait(false);

            return(fuzzyReferences);
        }
Example #29
0
 internal CSharpAddImportCodeFixProvider(
     IPackageInstallerService installerService,
     ISymbolSearchService symbolSearchService)
     : base(installerService, symbolSearchService)
 {
 }
 /// <summary>For testing purposes only (so that tests can pass in mock values)</summary> 
 internal CSharpAddMissingReferenceCodeFixProvider(
     IPackageInstallerService installerService,
     ISymbolSearchService symbolSearchService)
     : base(installerService, symbolSearchService)
 {
 }
Example #31
0
 /// <summary>
 /// Values for these parameters can be provided (during testing) for mocking purposes.
 /// </summary>
 protected AbstractAddMissingReferenceCodeFixProvider(
     IPackageInstallerService packageInstallerService = null,
     ISymbolSearchService symbolSearchService         = null)
     : base(packageInstallerService, symbolSearchService)
 {
 }
Example #32
0
 public RemoteAddImportServiceCallback(ISymbolSearchService symbolSearchService)
 => _symbolSearchService = symbolSearchService;
 /// <summary>For testing purposes only (so that tests can pass in mock values)</summary>
 internal CSharpAddMissingReferenceCodeFixProvider(
     IPackageInstallerService installerService,
     ISymbolSearchService symbolSearchService)
     : base(installerService, symbolSearchService)
 {
 }