public CompletionListUpdater(
                ITrackingSpan applicableToSpan,
                CompletionSessionData sessionData,
                AsyncCompletionSessionDataSnapshot snapshotData,
                RecentItemsManager recentItemsManager,
                IGlobalOptionService globalOptions)
            {
                _sessionData        = sessionData;
                _snapshotData       = snapshotData;
                _recentItemsManager = recentItemsManager;

                _applicableToSpan = applicableToSpan;
                _filterText       = applicableToSpan.GetText(_snapshotData.Snapshot);

                _hasSuggestedItemOptions = _sessionData.HasSuggestionItemOptions || _snapshotData.DisplaySuggestionItem;

                // We prefer using the original snapshot, which should always be available from items provided by Roslyn's CompletionSource.
                // Only use data.Snapshot in the theoretically possible but rare case when all items we are handling are from some non-Roslyn CompletionSource.
                var snapshotForDocument = TryGetInitialTriggerLocation(_snapshotData, out var intialTriggerLocation)
                    ? intialTriggerLocation.Snapshot
                    : _snapshotData.Snapshot;

                _document = snapshotForDocument?.TextBuffer.AsTextContainer().GetOpenDocumentInCurrentContext();
                if (_document != null)
                {
                    _completionService = _document.GetLanguageService <CompletionService>();
                    _completionRules   = _completionService?.GetRules(globalOptions.GetCompletionOptions(_document.Project.Language)) ?? CompletionRules.Default;

                    // Let us make the completion Helper used for non-Roslyn items case-sensitive.
                    // We can change this if get requests from partner teams.
                    _completionHelper = CompletionHelper.GetHelper(_document);
                    _filterMethod     = _completionService == null
                        ? ((itemsWithPatternMatches, text) => CompletionService.FilterItems(_completionHelper, itemsWithPatternMatches, text))
                        : ((itemsWithPatternMatches, text) => _completionService.FilterItems(_document, itemsWithPatternMatches, text));

                    // Nothing to highlight if user hasn't typed anything yet.
                    _highlightMatchingPortions = _filterText.Length > 0 &&
                                                 globalOptions.GetOption(CompletionViewOptions.HighlightMatchingPortionsOfCompletionListItems, _document.Project.Language);

                    _showCompletionItemFilters = globalOptions.GetOption(CompletionViewOptions.ShowCompletionItemFilters, _document.Project.Language);
                }
                else
                {
                    _completionService = null;
                    _completionRules   = CompletionRules.Default;

                    // Let us make the completion Helper used for non-Roslyn items case-sensitive.
                    // We can change this if get requests from partner teams.
                    _completionHelper = new CompletionHelper(isCaseSensitive: true);
                    _filterMethod     = (itemsWithPatternMatches, text) => CompletionService.FilterItems(_completionHelper, itemsWithPatternMatches, text);

                    _highlightMatchingPortions = false;
                    _showCompletionItemFilters = true;
                }
            }
Exemple #2
0
        public Task <ImmutableArray <VSCompletionItem> > SortCompletionListAsync(
            IAsyncCompletionSession session,
            AsyncCompletionSessionInitialDataSnapshot data,
            CancellationToken cancellationToken)
        {
            var stopwatch   = SharedStopwatch.StartNew();
            var sessionData = CompletionSessionData.GetOrCreateSessionData(session);

            // This method is called exactly once, so use the opportunity to set a baseline for telemetry.
            if (sessionData.TargetTypeFilterExperimentEnabled)
            {
                AsyncCompletionLogger.LogSessionHasTargetTypeFilterEnabled();
                if (data.InitialList.Any(i => i.Filters.Any(f => f.DisplayText == FeaturesResources.Target_type_matches)))
                {
                    AsyncCompletionLogger.LogSessionContainsTargetTypeFilter();
                }
            }

            // Sort by default comparer of Roslyn CompletionItem
            var sortedItems = data.InitialList.OrderBy(CompletionItemData.GetOrAddDummyRoslynItem).ToImmutableArray();

            AsyncCompletionLogger.LogItemManagerSortTicksDataPoint((int)stopwatch.Elapsed.TotalMilliseconds);
            return(Task.FromResult(sortedItems));
        }
        public async Task <FilteredCompletionModel?> UpdateCompletionListAsync(
            IAsyncCompletionSession session,
            AsyncCompletionSessionDataSnapshot data,
            CancellationToken cancellationToken)
        {
            var stopwatch = SharedStopwatch.StartNew();

            try
            {
                var sessionData = CompletionSessionData.GetOrCreateSessionData(session);

                // As explained in more details in the comments for `CompletionSource.GetCompletionContextAsync`, expanded items might
                // not be provided upon initial trigger of completion to reduce typing delays, even if they are supposed to be included by default.
                // While we do not expect to run in to this scenario very often, we'd still want to minimize the impact on user experience of this feature
                // as best as we could when it does occur. So the solution we came up with is this: if we decided to not include expanded items (because the
                // computation is running too long,) we will let it run in the background as long as the completion session is still active. Then whenever
                // any user input that would cause the completion list to refresh, we will check the state of this background task and add expanded items as part
                // of the update if they are available.
                // There is a `CompletionContext.IsIncomplete` flag, which is only supported in LSP mode at the moment. Therefore we opt to handle the checking
                // and combining the items in Roslyn until the `IsIncomplete` flag is fully supported in classic mode.

                if (sessionData.CombinedSortedList.HasValue)
                {
                    // Always use the previously saved combined list if available.
                    data = new AsyncCompletionSessionDataSnapshot(sessionData.CombinedSortedList.Value, data.Snapshot, data.Trigger, data.InitialTrigger, data.SelectedFilters,
                                                                  data.IsSoftSelected, data.DisplaySuggestionItem, data.Defaults);
                }
                else if (sessionData.ExpandedItemsTask != null)
                {
                    var task = sessionData.ExpandedItemsTask;
                    if (task.Status == TaskStatus.RanToCompletion)
                    {
                        // Make sure the task is removed when Adding expanded items,
                        // so duplicated items won't be added in subsequent list updates.
                        sessionData.ExpandedItemsTask = null;

                        var(expandedContext, _) = await task.ConfigureAwait(false);

                        if (expandedContext.Items.Length > 0)
                        {
                            // Here we rely on the implementation detail of `CompletionItem.CompareTo`, which always put expand items after regular ones.
                            var itemsBuilder = ImmutableArray.CreateBuilder <VSCompletionItem>(expandedContext.Items.Length + data.InitialSortedList.Length);
                            itemsBuilder.AddRange(data.InitialSortedList);
                            itemsBuilder.AddRange(expandedContext.Items);
                            var combinedList = itemsBuilder.MoveToImmutable();

                            // Add expanded items into a combined list, and save it to be used for future updates during the same session.
                            sessionData.CombinedSortedList = combinedList;
                            var combinedFilterStates = FilterSet.CombineFilterStates(expandedContext.Filters, data.SelectedFilters);

                            data = new AsyncCompletionSessionDataSnapshot(combinedList, data.Snapshot, data.Trigger, data.InitialTrigger, combinedFilterStates,
                                                                          data.IsSoftSelected, data.DisplaySuggestionItem, data.Defaults);
                        }

                        AsyncCompletionLogger.LogSessionWithDelayedImportCompletionIncludedInUpdate();
                    }
                }

                var updater = new CompletionListUpdater(session.ApplicableToSpan, sessionData, data, _recentItemsManager, _globalOptions);
                return(updater.UpdateCompletionList(cancellationToken));
            }
            finally
            {
                AsyncCompletionLogger.LogItemManagerUpdateDataPoint((int)stopwatch.Elapsed.TotalMilliseconds, isCanceled: cancellationToken.IsCancellationRequested);
            }
        }