protected override IEnumerable <JsonCompletionEntry> GetEntries(JsonCompletionContext context)
        {
            MemberNode member = context.ContextNode.FindType <MemberNode>();

            if (member == null || member.UnquotedNameText != "files")
            {
                yield break;
            }

            var parent = member.Parent as ObjectNode;

            if (!JsonHelpers.TryGetInstallationState(parent, out ILibraryInstallationState state))
            {
                yield break;
            }

            if (string.IsNullOrEmpty(state.Name))
            {
                yield break;
            }

            var             dependencies = _dependenciesFactory.FromConfigFile(ConfigFilePath);
            IProvider       provider     = dependencies.GetProvider(state.ProviderId);
            ILibraryCatalog catalog      = provider?.GetCatalog();

            if (catalog == null)
            {
                yield break;
            }

            Task <ILibrary>      task      = catalog.GetLibraryAsync(state.Name, state.Version, CancellationToken.None);
            FrameworkElement     presenter = GetPresenter(context);
            IEnumerable <string> usedFiles = GetUsedFiles(context);

            if (task.IsCompleted)
            {
                if (!(task.Result is ILibrary library))
                {
                    yield break;
                }

                foreach (string file in library.Files.Keys)
                {
                    if (!usedFiles.Contains(file))
                    {
                        ImageSource glyph = WpfUtil.GetIconForFile(presenter, file, out bool isThemeIcon);
                        yield return(new SimpleCompletionEntry(file, glyph, context.Session));
                    }
                }
            }
            else
            {
                yield return(new SimpleCompletionEntry(Resources.Text.Loading, string.Empty, KnownMonikers.Loading, context.Session));

                _ = task.ContinueWith(async(t) =>
                {
                    await VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                    if (!(t.Result is ILibrary library))
                    {
                        return;
                    }

                    if (!context.Session.IsDismissed)
                    {
                        var results = new List <JsonCompletionEntry>();

                        foreach (string file in library.Files.Keys)
                        {
                            if (!usedFiles.Contains(file))
                            {
                                ImageSource glyph = WpfUtil.GetIconForFile(presenter, file, out bool isThemeIcon);
                                results.Add(new SimpleCompletionEntry(file, glyph, context.Session));
                            }
                        }

                        UpdateListEntriesSync(context, results);
                    }
                }, TaskScheduler.Default);
            }
        }
Beispiel #2
0
        protected override IEnumerable <JSONCompletionEntry> GetEntries(JSONCompletionContext context)
        {
            JSONMember member = context.ContextItem.FindType <JSONMember>();

            if (member == null || member.UnquotedNameText != "files")
            {
                yield break;
            }

            var parent = member.Parent as JSONObject;

            if (!JsonHelpers.TryGetInstallationState(parent, out ILibraryInstallationState state))
            {
                yield break;
            }

            if (string.IsNullOrEmpty(state.LibraryId))
            {
                yield break;
            }

            var             dependencies = Dependencies.FromConfigFile(ConfigFilePath);
            IProvider       provider     = dependencies.GetProvider(state.ProviderId);
            ILibraryCatalog catalog      = provider?.GetCatalog();

            if (catalog == null)
            {
                yield break;
            }

            Task <ILibrary>      task      = catalog.GetLibraryAsync(state.LibraryId, CancellationToken.None);
            FrameworkElement     presenter = GetPresenter(context);
            IEnumerable <string> usedFiles = GetUsedFiles(context);

            if (task.IsCompleted)
            {
                if (!(task.Result is ILibrary library))
                {
                    yield break;
                }

                foreach (string file in library.Files.Keys)
                {
                    if (!usedFiles.Contains(file))
                    {
                        ImageSource glyph = WpfUtil.GetIconForFile(presenter, file, out bool isThemeIcon);
                        yield return(new SimpleCompletionEntry(file, glyph, context.Session));
                    }
                }
            }
            else
            {
                yield return(new SimpleCompletionEntry(Resources.Text.Loading, KnownMonikers.Loading, context.Session));

                task.ContinueWith((a) =>
                {
                    if (!(task.Result is ILibrary library))
                    {
                        return;
                    }

                    if (!context.Session.IsDismissed)
                    {
                        var results = new List <JSONCompletionEntry>();

                        foreach (string file in library.Files.Keys)
                        {
                            if (!usedFiles.Contains(file))
                            {
                                ImageSource glyph = WpfUtil.GetIconForFile(presenter, file, out bool isThemeIcon);
                                results.Add(new SimpleCompletionEntry(file, glyph, context.Session));
                            }
                        }

                        UpdateListEntriesSync(context, results);
                    }
                });
            }
        }