Exemple #1
0
 public PackageInfoControl(string packageId, string version, string tfm, IPackageSearchManager searcher)
 {
     InitializeComponent();
     PackageId.Content  = packageId;
     Glyph.Source       = WpfUtil.MonikerToBitmap(KnownMonikers.NuGet, 32);
     Glyph.ImageFailed += OnImageFailed;
     _job          = searcher.SearchPackageInfo(packageId, version, tfm);
     _job.Updated += JobUpdated;
     _firstRun     = true;
     JobUpdated(null, EventArgs.Empty);
 }
Exemple #2
0
        private void IssueIconQuery(string name, string ver, string tfm, PackageGlyphTag t)
        {
            IPackageFeedSearchJob <IPackageInfo> job = _searchManager.SearchPackageInfo(name, ver, tfm);
            EventHandler handler     = null;
            string       currentIcon = null;

            handler = (o, e) =>
            {
                if (job.IsCancelled)
                {
                    job.Updated -= handler;
                    job          = _searchManager.SearchPackageInfo(name, ver, tfm);
                    job.Updated += handler;
                    handler(o, e);
                    return;
                }

                IPackageInfo package = job.Results.OrderByDescending(x => SemanticVersion.Parse(x.Version)).FirstOrDefault();

                if (package != null)
                {
                    if (string.IsNullOrEmpty(package.IconUrl) || package.IconUrl == currentIcon)
                    {
                        return;
                    }

                    if (Uri.TryCreate(package.IconUrl, UriKind.Absolute, out Uri iconUri))
                    {
                        string proposedIcon = package.IconUrl;
                        ThreadHelper.JoinableTaskFactory.Run(async() =>
                        {
                            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                            if (currentIcon == proposedIcon)
                            {
                                return;
                            }

                            currentIcon          = proposedIcon;
                            BitmapImage img      = new BitmapImage(iconUri);
                            t.PackageIcon.Source = img;
                        });
                    }
                }
            };

            job.Updated += handler;
            handler(null, EventArgs.Empty);
        }
                public NuGetSearchJob(NuGetSearchUpdater parent, AsyncCompletionSessionDataSnapshot data)
                {
                    this.parent = parent;
                    this.data   = data;

                    var filterText = parent.session.ApplicableToSpan.GetText(data.Snapshot);

                    search = parent.parent.provider.PackageSearchManager.SearchPackageNames(filterText, parent.tfm);
                    cts.Token.Register(search.Cancel);

                    search.Updated += SearchUpdated;

                    // it may have cached results, and SearchUpdated may never fire
                    if (search.Results.Count > 0)
                    {
                        SearchUpdated(search, EventArgs.Empty);
                    }
                }
Exemple #4
0
        void UpdateSearch(string searchString)
        {
            if (oldSearchString != null && !ShouldUpdate(searchString, oldSearchString))
            {
                return;
            }
            oldSearchString = searchString;

            var newSearch = CreateSearch(searchString);

            if (search != null)
            {
                search.Cancel();
                search.Updated -= HandleSearchUpdated;
            }
            search          = newSearch;
            search.Updated += HandleSearchUpdated;

            Changing?.Invoke(this, EventArgs.Empty);
            UpdateList();
        }
Exemple #5
0
        public static Task <IReadOnlyList <T> > ToTask <T> (this IPackageFeedSearchJob <T> searchJob, CancellationToken cancelToken = default)
        {
            if (searchJob.RemainingFeeds.Count == 0)
            {
                return(Task.FromResult(searchJob.Results));
            }

            var tcs = new TaskCompletionSource <IReadOnlyList <T> > ();

            //making sure we actually unregister the eventhandler is kinda tricky
            //it could be already completed, or it could complete after we check but before we register
            EventHandler handleSearchUpdated = null;

            cancelToken.Register(() => {
                searchJob.Cancel();
                if (tcs.TrySetCanceled())
                {
                    searchJob.Updated -= handleSearchUpdated;
                }
            });

            handleSearchUpdated = (s, a) => {
                if (!cancelToken.IsCancellationRequested && searchJob.RemainingFeeds.Count == 0)
                {
                    if (tcs.TrySetResult(searchJob.Results))
                    {
                        searchJob.Updated -= handleSearchUpdated;
                    }
                }
            };
            searchJob.Updated += handleSearchUpdated;

            if (searchJob.RemainingFeeds.Count == 0)
            {
                handleSearchUpdated(searchJob, EventArgs.Empty);
            }

            return(tcs.Task);
        }
Exemple #6
0
        public void AugmentCompletionSession(ICompletionSession session, IList <CompletionSet> completionSets)
        {
            _currentSession = session;
            ITextSnapshot  snapshot = _textBuffer.CurrentSnapshot;
            ITrackingPoint point    = session.GetTriggerPoint(_textBuffer);
            int            pos      = point.GetPosition(snapshot);

            if (_classifier.GetClassificationSpans(new SnapshotSpan(snapshot, new Span(pos, 1))).Any(x => (x.ClassificationType.Classification?.IndexOf("comment", StringComparison.OrdinalIgnoreCase) ?? -1) > -1))
            {
                return;
            }

            _pos = pos;
            if (!IsInRangeForPackageCompletion(snapshot, pos, out Span span, out string name, out string version, out string completionType))
            {
                _nameSearchJob?.Cancel();
                _versionSearchJob?.Cancel();
                _nameSearchJob    = null;
                _versionSearchJob = null;
                return;
            }

            if (_isSelfTrigger)
            {
                _isSelfTrigger = false;
                if (_currentCompletionSet != null)
                {
                    completionSets.Add(_currentCompletionSet);
                }

                return;
            }

            string text = snapshot.GetText();
            int    targetFrameworkElementStartIndex  = text.IndexOf("<TargetFramework>", StringComparison.OrdinalIgnoreCase);
            int    targetFrameworksElementStartIndex = text.IndexOf("<TargetFrameworks>", StringComparison.OrdinalIgnoreCase);
            string tfm = "netcoreapp1.0";

            if (targetFrameworksElementStartIndex > -1)
            {
                int    closeTfms = text.IndexOf("</TargetFrameworks>", targetFrameworksElementStartIndex);
                int    realStart = targetFrameworksElementStartIndex + "<TargetFrameworks>".Length;
                string allTfms   = text.Substring(realStart, closeTfms - realStart);
                tfm = allTfms.Split(';')[0];
            }
            else if (targetFrameworkElementStartIndex > -1)
            {
                int closeTfm  = text.IndexOf("</TargetFramework>", targetFrameworkElementStartIndex);
                int realStart = targetFrameworkElementStartIndex + "<TargetFramework>".Length;
                tfm = text.Substring(realStart, closeTfm - realStart);
            }

            bool showLoading = false;

            switch (completionType)
            {
            case "Name":
                if (_nameSearchJob != null)
                {
                    _nameSearchJob.Cancel();
                    _nameSearchJob.Updated -= UpdateCompletions;
                }
                _versionSearchJob?.Cancel();
                _versionSearchJob       = null;
                _nameSearchJob          = _searchManager.SearchPackageNames(name, tfm);
                _nameSearchJob.Updated += UpdateCompletions;
                showLoading             = _nameSearchJob.RemainingFeeds.Count > 0;
                break;

            case "Version":
                if (_versionSearchJob != null)
                {
                    _versionSearchJob.Cancel();
                    _versionSearchJob.Updated -= UpdateCompletions;
                }
                _nameSearchJob?.Cancel();
                _nameSearchJob             = null;
                _versionSearchJob          = _searchManager.SearchPackageVersions(name, tfm);
                _versionSearchJob.Updated += UpdateCompletions;
                showLoading = _versionSearchJob.RemainingFeeds.Count > 0;
                break;
            }

            _currentCompletionSet = _currentSession.CompletionSets.FirstOrDefault(x => x is PackageCompletionSet) as PackageCompletionSet;

            bool newCompletionSet = _currentCompletionSet == null;

            if (newCompletionSet)
            {
                _currentCompletionSet = new PackageCompletionSet("PackageCompletion", "Package Completion", _textBuffer.CurrentSnapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeInclusive));
            }

            if (_nameSearchJob != null)
            {
                ProduceNameCompletionSet();
            }
            else if (_versionSearchJob != null)
            {
                ProduceVersionCompletionSet();
            }

            //If we're not part of an existing session & the results have already been
            //  finalized and those results assert that no packages match, show that
            //  there is no such package/version
            if (!session.CompletionSets.Any(x => x is PackageCompletionSet))
            {
                if (((_nameSearchJob != null && _nameSearchJob.RemainingFeeds.Count == 0) ||
                     (_versionSearchJob != null && _versionSearchJob.RemainingFeeds.Count == 0)) &&
                    _currentCompletionSet.Completions.Count == 0)
                {
                    _currentCompletionSet.AccessibleCompletions.Add(new Microsoft.VisualStudio.Language.Intellisense.Completion("(No Results)"));
                }

                completionSets.Add(_currentCompletionSet);
            }
        }