Ejemplo n.º 1
0
            private static ImmutableArray <CodeAction> CreateNestedActions(
                Document document,
                AddImportFixData fixData,
                IPackageInstallerService installerService)
            {
                // Determine what versions of this package are already installed in some project
                // in this solution.  We'll offer to add those specific versions to this project,
                // followed by an option to "Find and install latest version."
                var installedVersions = installerService.GetInstalledVersions(fixData.PackageName).NullToEmpty();
                var codeActions       = ArrayBuilder <CodeAction> .GetInstance();

                // First add the actions to install a specific version.
                codeActions.AddRange(installedVersions.Select(
                                         v => CreateCodeAction(
                                             document, fixData, installerService, versionOpt: v, isLocal: true)));

                // Now add the action to install the specific version.
                var preferredVersion = fixData.PackageVersionOpt;

                if (preferredVersion == null || !installedVersions.Contains(preferredVersion))
                {
                    codeActions.Add(CreateCodeAction(
                                        document, fixData, installerService, preferredVersion, isLocal: false));
                }

                // And finally the action to show the package manager dialog.
                codeActions.Add(new InstallWithPackageManagerCodeAction(installerService, fixData.PackageName));
                return(codeActions.ToImmutableAndFree());
            }
Ejemplo n.º 2
0
        private static ImmutableArray <CodeAction> CreateNestedActions(
            IPackageInstallerService installerService,
            string source, string packageName, bool includePrerelease,
            Document document)
        {
            // Determine what versions of this package are already installed in some project
            // in this solution.  We'll offer to add those specific versions to this project,
            // followed by an option to "Find and install latest version."
            var installedVersions = installerService.GetInstalledVersions(packageName);
            var codeActions       = ArrayBuilder <CodeAction> .GetInstance();

            // First add the actions to install a specific version.
            codeActions.AddRange(installedVersions.Select(v => CreateCodeAction(
                                                              installerService, source, packageName, document,
                                                              versionOpt: v, includePrerelease: includePrerelease, isLocal: true)));

            // Now add the action to install the specific version.
            codeActions.Add(CreateCodeAction(
                                installerService, source, packageName, document,
                                versionOpt: null, includePrerelease: includePrerelease, isLocal: false));

            // And finally the action to show the package manager dialog.
            codeActions.Add(new InstallWithPackageManagerCodeAction(installerService, packageName));
            return(codeActions.ToImmutableAndFree());
        }
        private static ImmutableArray<CodeAction> CreateNestedActions(
            IPackageInstallerService installerService,
            string source, string packageName, bool includePrerelease,
            Document document)
        {
            // Determine what versions of this package are already installed in some project
            // in this solution.  We'll offer to add those specific versions to this project,
            // followed by an option to "Find and install latest version."
            var installedVersions = installerService.GetInstalledVersions(packageName);
            var codeActions = ArrayBuilder<CodeAction>.GetInstance();

            // First add the actions to install a specific version.
            codeActions.AddRange(installedVersions.Select(v => CreateCodeAction(
                installerService, source, packageName, document, 
                versionOpt: v, includePrerelease: includePrerelease, isLocal: true)));

            // Now add the action to install the specific version.
            codeActions.Add(CreateCodeAction(
                installerService, source, packageName, document,
                versionOpt: null, includePrerelease: includePrerelease, isLocal: false));

            // And finally the action to show the package manager dialog.
            codeActions.Add(new InstallWithPackageManagerCodeAction(installerService, packageName));
            return codeActions.ToImmutableAndFree();
        }
        public async Task <ImmutableArray <PackageWithTypeResult> > FindPackagesWithTypeAsync(
            string source, string name, int arity, CancellationToken cancellationToken)
        {
            var engine = await GetEngine(cancellationToken).ConfigureAwait(false);

            var allPackagesWithType = await engine.FindPackagesWithTypeAsync(
                source, name, arity).ConfigureAwait(false);

            var typesFromPackagesUsedInOtherProjects    = new List <PackageWithTypeResult>();
            var typesFromPackagesNotUsedInOtherProjects = new List <PackageWithTypeResult>();

            foreach (var packageWithType in allPackagesWithType)
            {
                var resultList = _installerService.GetInstalledVersions(packageWithType.PackageName).Any()
                    ? typesFromPackagesUsedInOtherProjects
                    : typesFromPackagesNotUsedInOtherProjects;

                resultList.Add(packageWithType);
            }

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

            // We always returm types from packages that we've use elsewhere in the project.
            result.AddRange(typesFromPackagesUsedInOtherProjects);

            // For all other hits include as long as the popularity is high enough.
            // Popularity ranks are in powers of two.  So if two packages differ by
            // one rank, then one is at least twice as popular as the next.  Two
            // ranks would be four times as popular.  Three ranks = 8 times,  etc.
            // etc.  We keep packages that within 1 rank of the best package we find.
            int?bestRank = null;

            foreach (var packageWithType in typesFromPackagesNotUsedInOtherProjects)
            {
                var rank = packageWithType.Rank;
                bestRank = bestRank == null ? rank : Math.Max(bestRank.Value, rank);

                if (Math.Abs(bestRank.Value - rank) > 1)
                {
                    break;
                }

                result.Add(packageWithType);
            }

            return(result.ToImmutableAndFree());
        }
        private ImmutableArray <TPackageResult> FilterAndOrderPackages <TPackageResult>(
            ImmutableArray <TPackageResult> allPackages) where TPackageResult : PackageResult
        {
            var packagesUsedInOtherProjects    = new List <TPackageResult>();
            var packagesNotUsedInOtherProjects = new List <TPackageResult>();

            foreach (var package in allPackages)
            {
                var resultList = _installerService.GetInstalledVersions(package.PackageName).Any()
                    ? packagesUsedInOtherProjects
                    : packagesNotUsedInOtherProjects;

                resultList.Add(package);
            }

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

            // We always returm types from packages that we've use elsewhere in the project.
            result.AddRange(packagesUsedInOtherProjects);

            // For all other hits include as long as the popularity is high enough.
            // Popularity ranks are in powers of two.  So if two packages differ by
            // one rank, then one is at least twice as popular as the next.  Two
            // ranks would be four times as popular.  Three ranks = 8 times,  etc.
            // etc.  We keep packages that within 1 rank of the best package we find.
            int?bestRank = packagesUsedInOtherProjects.LastOrDefault()?.Rank;

            foreach (var packageWithType in packagesNotUsedInOtherProjects)
            {
                var rank = packageWithType.Rank;
                bestRank = bestRank == null ? rank : Math.Max(bestRank.Value, rank);

                if (Math.Abs(bestRank.Value - rank) > 1)
                {
                    break;
                }

                result.Add(packageWithType);
            }

            return(result.ToImmutableAndFree());
        }