예제 #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);

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

                    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);
                    }
                }

                // 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(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);
        }
예제 #2
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) = LibraryIdToNameAndVersionConverter.Instance.GetLibraryNameAndVersion(libraryNameStart, _provider.Id);

            try
            {
                // library name completion
                if (caretPosition < name.Length + 1 && name[name.Length - 1] != '@')
                {
                    if (IsGitHub(libraryNameStart))
                    {
                        return(completionSet);
                    }

                    IEnumerable <string> packageNames = await Microsoft.Web.LibraryManager.Providers.Unpkg.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
                {
                    name = name[name.Length - 1] == '@' ? name.Remove(name.Length - 1) : name;

                    completionSet.Start  = name.Length + 1;
                    completionSet.Length = version.Length;

                    IEnumerable <string> versions;

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

                    foreach (string v in versions)
                    {
                        CompletionItem completionItem = new CompletionItem
                        {
                            DisplayText   = v,
                            InsertionText = LibraryIdToNameAndVersionConverter.Instance.GetLibraryId(name, v, _provider.Id)
                        };

                        completions.Add(completionItem);
                    }
                }

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

            return(completionSet);
        }