예제 #1
0
        /// <summary>
        /// Given a list of completion items that match the current code typed by the user,
        /// returns the item that is considered the best match, and whether or not that
        /// item should be selected or not.
        ///
        /// itemToFilterText provides the values that each individual completion item should
        /// be filtered against.
        /// </summary>
        public virtual ImmutableArray <CompletionItem> FilterItems(
            Document document,
            ImmutableArray <CompletionItem> items,
            string filterText)
        {
            var helper = CompletionHelper.GetHelper(document);
            var itemsWithPatternMatch = new SegmentedList <(CompletionItem, PatternMatch?)>(items.Select(
                                                                                                item => (item, helper.GetMatch(item.FilterText, filterText, includeMatchSpans: false, CultureInfo.CurrentCulture))));

            var builder = ImmutableArray.CreateBuilder <CompletionItem>();

            FilterItems(helper, itemsWithPatternMatch, filterText, builder);
            return(builder.ToImmutable());
        }
예제 #2
0
        /// <summary>
        /// Given a list of completion items that match the current code typed by the user,
        /// returns the item that is considered the best match, and whether or not that
        /// item should be selected or not.
        ///
        /// itemToFilterText provides the values that each individual completion item should
        /// be filtered against.
        /// </summary>
        public virtual ImmutableArray <CompletionItem> FilterItems(
            Document document,
            ImmutableArray <CompletionItem> items,
            string filterText)
        {
            var helper = CompletionHelper.GetHelper(document);

            var bestItems = ArrayBuilder <CompletionItem> .GetInstance();

            foreach (var item in items)
            {
                if (bestItems.Count == 0)
                {
                    // We've found no good items yet.  So this is the best item currently.
                    bestItems.Add(item);
                }
                else
                {
                    var comparison = helper.CompareItems(item, bestItems.First(), filterText, CultureInfo.CurrentCulture);
                    if (comparison < 0)
                    {
                        // This item is strictly better than the best items we've found so far.
                        bestItems.Clear();
                        bestItems.Add(item);
                    }
                    else if (comparison == 0)
                    {
                        // This item is as good as the items we've been collecting.  We'll return
                        // it and let the controller decide what to do.  (For example, it will
                        // pick the one that has the best MRU index).
                        bestItems.Add(item);
                    }
                    // otherwise, this item is strictly worse than the ones we've been collecting.
                    // We can just ignore it.
                }
            }

            return(bestItems.ToImmutableAndFree());
        }