Exemple #1
0
        internal static async Task <NpmPackageInfo> GetPackageInfoAsync(string packageName, CancellationToken cancellationToken)
        {
            NpmPackageInfo packageInfo = null;

            if (!_cachedPackages.TryGetValue(packageName, out packageInfo))
            {
                packageInfo = await NpmPackageSearch.GetPackageInfoAsync(packageName, cancellationToken).ConfigureAwait(false);

                if (packageInfo != null)
                {
                    _cachedPackages[packageName] = packageInfo;
                }
            }

            return(packageInfo);
        }
        public async Task <IReadOnlyList <ILibraryGroup> > SearchAsync(string term, int maxHits, CancellationToken cancellationToken)
        {
            List <ILibraryGroup> libraryGroups = new List <ILibraryGroup>();

            try
            {
                IEnumerable <string> packageNames = await NpmPackageSearch.GetPackageNamesAsync(term, CancellationToken.None);

                libraryGroups = packageNames.Select(packageName => new UnpkgLibraryGroup(packageName)).ToList <ILibraryGroup>();
            }
            catch (Exception ex)
            {
                _provider.HostInteraction.Logger.Log(ex.ToString(), LogLevel.Error);
            }

            return(libraryGroups);
        }
        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);
        }
        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);
        }
        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);
        }