private static async Task <NpmPackageInfo> GetPackageInfoForUnscopedPackageAsync(string packageName, CancellationToken cancellationToken)
        {
            NpmPackageInfo packageInfo = null;

            try
            {
                string  packageInfoUrl  = string.Format(NpmLatestPackageInfoUrl, packageName);
                JObject packageInfoJSON = await WebRequestHandler.Instance.GetJsonObjectViaGetAsync(packageInfoUrl, cancellationToken).ConfigureAwait(false);

                if (packageInfoJSON != null)
                {
                    packageInfo = NpmPackageInfo.Parse(packageInfoJSON);
                }
            }
            catch (Exception)
            {
                packageInfo = new NpmPackageInfo(
                    packageName,
                    Resources.Text.LibraryDetail_Unavailable,
                    Resources.Text.LibraryDetail_Unavailable);
            }

            return(await CreatePackageInfoAsync(packageName, packageInfo, cancellationToken));
        }
        private static async Task <NpmPackageInfo> CreatePackageInfoAsync(string packageName, NpmPackageInfo packageInfo, CancellationToken cancellationToken)
        {
            try
            {
                string  packageInfoUrl  = string.Format(NpmPackageInfoUrl, packageName);
                JObject packageInfoJSON = await WebRequestHandler.Instance.GetJsonObjectViaGetAsync(packageInfoUrl, cancellationToken).ConfigureAwait(false);

                if (packageInfoJSON != null && packageInfo == null)
                {
                    packageInfo = NpmPackageInfo.Parse(packageInfoJSON);
                }

                JObject versionsList = packageInfoJSON["versions"] as JObject;
                if (versionsList != null)
                {
                    List <SemanticVersion> semanticVersions = versionsList.Properties().Select(p => SemanticVersion.Parse(p.Name)).ToList <SemanticVersion>();
                    string latestVersion = semanticVersions.Max().OriginalText;
                    IList <SemanticVersion> filteredSemanticVersions = FilterOldPrereleaseVersions(semanticVersions);

                    packageInfo = new NpmPackageInfo(packageInfo.Name, packageInfo.Description, latestVersion, filteredSemanticVersions);
                }
            }
            catch (Exception)
            {
                packageInfo = new NpmPackageInfo(
                    packageName,
                    Resources.Text.LibraryDetail_Unavailable,
                    Resources.Text.LibraryDetail_Unavailable);
            }

            return(packageInfo);
        }
        public async Task <CompletionSet> GetLibraryCompletionSetAsync(string libraryNameStart, int caretPosition)
        {
            CompletionSet completionSet = new CompletionSet
            {
                Start  = 0,
                Length = libraryNameStart.Length
            };

            List <CompletionItem> completions = new List <CompletionItem>();

            (string name, string version) = LibraryIdToNameAndVersionConverter.Instance.GetLibraryNameAndVersion(libraryNameStart, _provider.Id);

            // Typing '@' after the library name should have version completion.
            int at = name.LastIndexOf('@');

            name = at > -1 ? name.Substring(0, at) : name;

            try
            {
                // library name completion
                if (caretPosition < name.Length + 1)
                {
                    IEnumerable <string> packageNames = await NpmPackageSearch.GetPackageNamesAsync(libraryNameStart, CancellationToken.None);

                    foreach (string packageName in packageNames)
                    {
                        CompletionItem completionItem = new CompletionItem
                        {
                            DisplayText   = packageName,
                            InsertionText = packageName,
                        };

                        completions.Add(completionItem);
                    }

                    completionSet.CompletionType = CompletionSortOrder.AsSpecified;
                }

                // library version completion
                else
                {
                    completionSet.Start  = name.Length + 1;
                    completionSet.Length = version.Length;

                    NpmPackageInfo npmPackageInfo = await NpmPackageInfoCache.GetPackageInfoAsync(name, CancellationToken.None);

                    foreach (SemanticVersion semVersion in npmPackageInfo.Versions)
                    {
                        string         versionText    = semVersion.ToString();
                        CompletionItem completionItem = new CompletionItem
                        {
                            DisplayText   = versionText,
                            InsertionText = name + "@" + versionText
                        };

                        completions.Add(completionItem);
                    }

                    completionSet.CompletionType = CompletionSortOrder.Version;
                }

                completionSet.Completions = completions;
            }
            catch (Exception ex)
            {
                _provider.HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
            }

            return(completionSet);
        }
示例#4
0
        public async Task <CompletionSet> GetLibraryCompletionSetAsync(string libraryNameStart, int caretPosition)
        {
            var completions = new List <CompletionItem>();

            var completionSet = new CompletionSet
            {
                Start       = 0,
                Length      = 0,
                Completions = completions
            };

            if (string.IsNullOrEmpty(libraryNameStart))
            {
                // no point in doing the rest of the work, we know it's going to be an empty completion set anyway
                return(completionSet);
            }

            completionSet.Length = libraryNameStart.Length;

            (string name, string version) = _libraryNamingScheme.GetLibraryNameAndVersion(libraryNameStart);

            // Typing '@' after the library name should have version completion.

            try
            {
                // library name completion
                if (caretPosition < name.Length + 1)
                {
                    IEnumerable <string> packageNames = await NpmPackageSearch.GetPackageNamesAsync(libraryNameStart, CancellationToken.None);

                    foreach (string packageName in packageNames)
                    {
                        var completionItem = new CompletionItem
                        {
                            DisplayText   = packageName,
                            InsertionText = packageName,
                        };

                        completions.Add(completionItem);
                    }

                    completionSet.CompletionType = CompletionSortOrder.AsSpecified;
                }

                // library version completion
                else
                {
                    completionSet.Start  = name.Length + 1;
                    completionSet.Length = version.Length;

                    NpmPackageInfo npmPackageInfo = await NpmPackageInfoCache.GetPackageInfoAsync(name, CancellationToken.None);

                    IList <SemanticVersion> versions = npmPackageInfo.Versions;

                    if (versions != null)
                    {
                        foreach (SemanticVersion semVersion in versions)
                        {
                            string versionText    = semVersion.ToString();
                            var    completionItem = new CompletionItem
                            {
                                DisplayText   = versionText,
                                InsertionText = name + "@" + versionText
                            };

                            completions.Add(completionItem);
                        }
                    }

                    completionSet.CompletionType = CompletionSortOrder.Version;
                }
            }
            catch (Exception ex)
            {
                _logger.Log(ex.ToString(), LogLevel.Error);
            }

            return(completionSet);
        }
示例#5
0
        private async Task <IEnumerable <NpmPackageInfo> > GetPackageNamesFromSimpleQueryAsync(string searchTerm, CancellationToken cancellationToken)
        {
            string packageListUrl = string.Format(CultureInfo.InvariantCulture, NpmPackageSearchUrl, searchTerm);
            var    packages       = new List <NpmPackageInfo>();

            try
            {
                JObject topLevelObject = await _requestHandler.GetJsonObjectViaGetAsync(packageListUrl, cancellationToken).ConfigureAwait(false);

                if (topLevelObject != null)
                {
                    // We get back something like this:
                    //
                    //{
                    //  "objects": [
                    //    {
                    //      "package": {
                    //        "name": "yargs",
                    //        "version": "6.6.0",
                    //        "description": "yargs the modern, pirate-themed, successor to optimist.",
                    //        "keywords": [
                    //          "argument",
                    //          "args",
                    //          "option",
                    //          "parser",
                    //          "parsing",
                    //          "cli",
                    //          "command"
                    //        ],
                    //        "date": "2016-12-30T16:53:16.023Z",
                    //        "links": {
                    //          "npm": "https://www.npmjs.com/package/yargs",
                    //          "homepage": "http://yargs.js.org/",
                    //          "repository": "https://github.com/yargs/yargs",
                    //          "bugs": "https://github.com/yargs/yargs/issues"
                    //        },
                    //        "publisher": {
                    //          "username": "******",
                    //          "email": "*****@*****.**"
                    //        },
                    //        "maintainers": [
                    //          {
                    //            "username": "******",
                    //            "email": "*****@*****.**"
                    //          },
                    //          {
                    //            "username": "******",
                    //            "email": "*****@*****.**"
                    //          },
                    //          {
                    //            "username": "******",
                    //            "email": "*****@*****.**"
                    //          },
                    //          {
                    //            "username": "******",
                    //            "email": "*****@*****.**"
                    //          }
                    //        ]
                    //      },
                    //      "score": {
                    //        "final": 0.9237841281241451,
                    //        "detail": {
                    //          "quality": 0.9270640902288084,
                    //          "popularity": 0.8484861649808381,
                    //          "maintenance": 0.9962706951777409
                    //        }
                    //      },
                    //      "searchScore": 100000.914
                    //    }
                    //  ],
                    //  "total": 1,
                    //  "time": "Wed Jan 25 2017 19:23:35 GMT+0000 (UTC)"
                    //}

                    if (topLevelObject["objects"] is JArray searchResultList)
                    {
                        foreach (JObject searchResultObject in searchResultList.Children())
                        {
                            if (searchResultObject["package"] is JObject packageEntry)
                            {
                                var packageInfo = NpmPackageInfo.Parse(packageEntry);
                                packages.Add(packageInfo);
                            }
                        }
                    }
                }
            }
            catch
            {
                // TODO: telemetry.
                // should we report response failures separate from parse failures?
            }

            return(packages);
        }
示例#6
0
        public async Task <CompletionSet> GetLibraryCompletionSetAsync(string libraryNameStart, int caretPosition)
        {
            CompletionSet completionSet = new CompletionSet
            {
                Start  = 0,
                Length = libraryNameStart.Length
            };

            List <CompletionItem> completions = new List <CompletionItem>();

            (string name, string version) = LibraryNamingScheme.Instance.GetLibraryNameAndVersion(libraryNameStart);

            try
            {
                // library name completion
                if (caretPosition < name.Length + 1)
                {
                    IEnumerable <string> packageNames = await NpmPackageSearch.GetPackageNamesAsync(libraryNameStart, CancellationToken.None);

                    foreach (string packageName in packageNames)
                    {
                        CompletionItem completionItem = new CompletionItem
                        {
                            DisplayText   = packageName,
                            InsertionText = packageName
                        };

                        completions.Add(completionItem);
                    }
                }

                // library version completion
                else
                {
                    completionSet.Start  = name.Length + 1;
                    completionSet.Length = version.Length;

                    NpmPackageInfo npmPackageInfo = await NpmPackageInfoCache.GetPackageInfoAsync(name, CancellationToken.None);

                    foreach (SemanticVersion semVersion in npmPackageInfo.Versions)
                    {
                        string         itemText       = name + "@" + semVersion.ToString();
                        CompletionItem completionItem = new CompletionItem
                        {
                            DisplayText   = itemText,
                            InsertionText = itemText
                        };

                        completions.Add(completionItem);
                    }
                }

                completionSet.Completions = completions;
            }
            catch (Exception ex)
            {
                _provider.HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
            }

            return(completionSet);
        }