Ejemplo n.º 1
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 <NpmPackageInfo> packages = await _packageSearch.GetPackageNamesAsync(libraryNameStart, CancellationToken.None);

                    foreach (NpmPackageInfo packageInfo in packages)
                    {
                        var completionItem = new CompletionItem
                        {
                            DisplayText   = packageInfo.Name,
                            InsertionText = _libraryNamingScheme.GetLibraryId(packageInfo.Name, packageInfo.LatestVersion)
                        };

                        completions.Add(completionItem);
                    }

                    completionSet.CompletionType = CompletionSortOrder.AsSpecified;
                }

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

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

                    IList <SemanticVersion> versions = npmPackageInfo.Versions;

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

                        completions.Add(completionItem);
                    }

                    // support @latest version
                    completions.Add(new CompletionItem
                    {
                        DisplayText   = LatestVersionTag,
                        InsertionText = _libraryNamingScheme.GetLibraryId(name, LatestVersionTag),
                    });

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

            return(completionSet);
        }
Ejemplo n.º 2
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);

            try
            {
                // library name completion
                if (caretPosition < name.Length + 1)
                {
                    if (IsGitHub(libraryNameStart))
                    {
                        return(completionSet);
                    }

                    IEnumerable <NpmPackageInfo> packages = await _packageSearch.GetPackageNamesAsync(libraryNameStart, CancellationToken.None);

                    foreach (NpmPackageInfo package in packages)
                    {
                        var completionItem = new CompletionItem
                        {
                            DisplayText   = package.Name,
                            InsertionText = _libraryNamingScheme.GetLibraryId(package.Name, package.LatestVersion)
                        };

                        completions.Add(completionItem);
                    }
                }

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

                    IEnumerable <string> versions;

                    if (IsGitHub(name))
                    {
                        versions = await GetGithubLibraryVersionsAsync(name);
                    }
                    else
                    {
                        var libGroup = new JsDelivrLibraryGroup(_packageInfoFactory, name);
                        versions = await libGroup.GetLibraryVersions(CancellationToken.None);
                    }

                    foreach (string v in versions)
                    {
                        var completionItem = new CompletionItem
                        {
                            DisplayText   = v,
                            InsertionText = _libraryNamingScheme.GetLibraryId(name, v),
                        };

                        completions.Add(completionItem);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Log(ex.ToString(), LogLevel.Error);
            }

            return(completionSet);
        }