Пример #1
0
        public override void SelectBestMatch()
        {
            var text = ApplicableTo.GetText(ApplicableTo.TextBuffer.CurrentSnapshot);

            if (string.IsNullOrWhiteSpace(text))
            {
                SelectionStatus = new CompletionSelectionStatus(null, false, false);
                return;
            }

            Microsoft.VisualStudio.Language.Intellisense.Completion bestMatch = null;
            var bestValue = 0;
            var isUnique  = true;

            foreach (var completion in Completions)
            {
                int value = CompareCompletionText(completion.DisplayText, text);
                if (bestMatch == null || value > bestValue)
                {
                    bestMatch = completion;
                    bestValue = value;
                    isUnique  = true;
                }
                else if (value == bestValue)
                {
                    isUnique = false;
                }
            }

            SelectionStatus = new CompletionSelectionStatus(bestMatch, bestValue > 0, isUnique);
        }
Пример #2
0
        private bool DoesCompletionMatchApplicabilityText(Microsoft.VisualStudio.Language.Intellisense.Completion completion)
        {
            if (IsUpper(this._filterBufferText))
            {
                return(GetUpperString(completion.DisplayText).StartsWith(this._filterBufferText));
            }

            return(completion.DisplayText.ToLowerInvariant().Contains(this._filterBufferText.ToLowerInvariant()));
        }
Пример #3
0
        PresentationItem ICompletionSet.GetPresentationItem(VSCompletion completion)
        {
            // Linear search is ok since this is only called by the user manually selecting
            // an item.  Creating a reverse mapping uses too much memory and affects GCs.
            foreach (var kvp in _presentationItemMap)
            {
                if (kvp.Value == completion)
                {
                    return(kvp.Key);
                }
            }

            return(null);
        }
 PresentationItem ICompletionSet.GetPresentationItem(VSCompletion completion)
 {
     return(_roslynCompletionSet.GetPresentationItem(completion));
 }
 CompletionItem ICompletionSet.GetCompletionItem(VSCompletion completion)
 {
     return _roslynCompletionSet.GetCompletionItem(completion);
 }
        internal CompletionItem GetCompletionItem(VSCompletion completion)
        {
            // Linear search is ok since this is only called by the user manually selecting 
            // an item.  Creating a reverse mapping uses too much memory and affects GCs.
            foreach (var kvp in _completionItemMap)
            {
                if (kvp.Value == completion)
                {
                    return kvp.Key;
                }
            }

            return null;
        }
Пример #7
0
        void ICompletionSet.SetCompletionItems(
            IList <PresentationItem> completionItems,
            PresentationItem selectedItem,
            PresentationItem presetBuilder,
            bool suggestionMode,
            bool isSoftSelected,
            ImmutableArray <CompletionItemFilter> completionItemFilters,
            IReadOnlyDictionary <CompletionItem, string> completionItemToFilterText)
        {
            this._foregroundObject.AssertIsForeground();

            VSCompletion selectedCompletionItem = null;

            // Initialize the completion map to a reasonable default initial size (+1 for the builder)
            _presentationItemMap        = _presentationItemMap ?? new Dictionary <PresentationItem, VSCompletion>(completionItems.Count + 1);
            _completionItemToFilterText = completionItemToFilterText;

            try
            {
                this.WritableCompletionBuilders.BeginBulkOperation();
                this.WritableCompletionBuilders.Clear();

#if DEV15
                // If more than one filter was provided, then present it to the user.
                if (_filters == null && completionItemFilters.Length > 1)
                {
                    _filters = completionItemFilters.Select(f => new IntellisenseFilter2(this, f, GetLanguage()))
                               .ToArray();
                }
#endif

                var applicableToText = this.ApplicableTo.GetText(this.ApplicableTo.TextBuffer.CurrentSnapshot);

                var filteredSuggestionModeItem = new SimplePresentationItem(
                    CompletionItem.Create(
                        displayText: applicableToText,
                        span: this.ApplicableTo.GetSpan(this.ApplicableTo.TextBuffer.CurrentSnapshot).Span.ToTextSpan()),
                    selectedItem.CompletionService,
                    isSuggestionModeItem: true);

                var showBuilder            = suggestionMode || presetBuilder != null;
                var bestSuggestionModeItem = applicableToText.Length > 0 ? filteredSuggestionModeItem : presetBuilder ?? filteredSuggestionModeItem;

                if (showBuilder && bestSuggestionModeItem != null)
                {
                    var suggestionModeCompletion = GetVSCompletion(bestSuggestionModeItem);
                    this.WritableCompletionBuilders.Add(suggestionModeCompletion);

                    if (selectedItem != null && selectedItem.IsSuggestionModeItem)
                    {
                        selectedCompletionItem = suggestionModeCompletion;
                    }
                }
            }
            finally
            {
                this.WritableCompletionBuilders.EndBulkOperation();
            }

            try
            {
                this.WritableCompletions.BeginBulkOperation();
                this.WritableCompletions.Clear();

                foreach (var item in completionItems)
                {
                    var completionItem = GetVSCompletion(item);
                    this.WritableCompletions.Add(completionItem);

                    if (item == selectedItem)
                    {
                        selectedCompletionItem = completionItem;
                    }
                }
            }
            finally
            {
                this.WritableCompletions.EndBulkOperation();
            }

            this.SelectionStatus = new CompletionSelectionStatus(
                selectedCompletionItem, isSelected: !isSoftSelected, isUnique: selectedCompletionItem != null);
        }
Пример #8
0
        public void SetCompletionItems(
            IList <PresentationItem> completionItems,
            PresentationItem selectedItem,
            PresentationItem presetBuilder,
            bool suggestionMode,
            bool isSoftSelected,
            ImmutableArray <CompletionItemFilter> completionItemFilters,
            IReadOnlyDictionary <CompletionItem, string> completionItemToFilterText)
        {
            this.AssertIsForeground();

            VSCompletion selectedCompletionItem = null;

            // Initialize the completion map to a reasonable default initial size (+1 for the builder)
            PresentationItemMap        = PresentationItemMap ?? new Dictionary <PresentationItem, VSCompletion>(completionItems.Count + 1);
            CompletionItemToFilterText = completionItemToFilterText;

            try
            {
                VsCompletionSet.WritableCompletionBuilders.BeginBulkOperation();
                VsCompletionSet.WritableCompletionBuilders.Clear();

                this.SetupFilters(completionItemFilters);

                var applicableToText = VsCompletionSet.ApplicableTo.GetText(
                    VsCompletionSet.ApplicableTo.TextBuffer.CurrentSnapshot);

                SimplePresentationItem filteredSuggestionModeItem = null;
                if (selectedItem != null)
                {
                    filteredSuggestionModeItem = new SimplePresentationItem(
                        CompletionItem.Create(
                            displayText: applicableToText,
                            span: VsCompletionSet.ApplicableTo.GetSpan(
                                VsCompletionSet.ApplicableTo.TextBuffer.CurrentSnapshot).Span.ToTextSpan()),
                        selectedItem.CompletionService,
                        isSuggestionModeItem: true);
                }

                var showBuilder            = suggestionMode || presetBuilder != null;
                var bestSuggestionModeItem = applicableToText.Length > 0 ? filteredSuggestionModeItem : presetBuilder ?? filteredSuggestionModeItem;

                if (showBuilder && bestSuggestionModeItem != null)
                {
                    var suggestionModeCompletion = GetVSCompletion(bestSuggestionModeItem);
                    VsCompletionSet.WritableCompletionBuilders.Add(suggestionModeCompletion);

                    if (selectedItem != null && selectedItem.IsSuggestionModeItem)
                    {
                        selectedCompletionItem = suggestionModeCompletion;
                    }
                }
            }
            finally
            {
                VsCompletionSet.WritableCompletionBuilders.EndBulkOperation();
            }

            try
            {
                VsCompletionSet.WritableCompletions.BeginBulkOperation();
                VsCompletionSet.WritableCompletions.Clear();

                foreach (var item in completionItems)
                {
                    var completionItem = GetVSCompletion(item);
                    VsCompletionSet.WritableCompletions.Add(completionItem);

                    if (item == selectedItem)
                    {
                        selectedCompletionItem = completionItem;
                    }
                }
            }
            finally
            {
                VsCompletionSet.WritableCompletions.EndBulkOperation();
            }

            VsCompletionSet.SelectionStatus = new CompletionSelectionStatus(
                selectedCompletionItem, isSelected: !isSoftSelected, isUnique: selectedCompletionItem != null);
        }
Пример #9
0
        internal void SetCompletionItems(
            IList <CompletionItem> completionItems,
            CompletionItem selectedItem,
            CompletionItem presetBuilder,
            bool suggestionMode,
            bool isSoftSelected)
        {
            VSCompletion selectedCompletionItem = null;

            // Initialize the completion map to a reasonable default initial size (+1 for the builder)
            _completionItemMap = _completionItemMap ?? new Dictionary <CompletionItem, VSCompletion>(completionItems.Count + 1);

            try
            {
                this.WritableCompletionBuilders.BeginBulkOperation();
                this.WritableCompletionBuilders.Clear();

                var applicableToText = this.ApplicableTo.GetText(this.ApplicableTo.TextBuffer.CurrentSnapshot);
                var filteredBuilder  = new CompletionItem(null, applicableToText,
                                                          this.ApplicableTo.GetSpan(this.ApplicableTo.TextBuffer.CurrentSnapshot).Span.ToTextSpan(), isBuilder: true);

                var showBuilder = suggestionMode || presetBuilder != null;
                var bestBuilder = applicableToText.Length > 0 ? filteredBuilder : presetBuilder ?? filteredBuilder;

                if (showBuilder && bestBuilder != null)
                {
                    var builderCompletion = ConvertCompletionItem(bestBuilder);
                    this.WritableCompletionBuilders.Add(builderCompletion);

                    if (selectedItem.IsBuilder)
                    {
                        selectedCompletionItem = builderCompletion;
                    }
                }
            }
            finally
            {
                this.WritableCompletionBuilders.EndBulkOperation();
            }

            try
            {
                this.WritableCompletions.BeginBulkOperation();
                this.WritableCompletions.Clear();

                foreach (var item in completionItems)
                {
                    var completionItem = ConvertCompletionItem(item);
                    this.WritableCompletions.Add(completionItem);

                    if (item == selectedItem)
                    {
                        selectedCompletionItem = completionItem;
                    }
                }
            }
            finally
            {
                this.WritableCompletions.EndBulkOperation();
            }

            Contract.ThrowIfNull(selectedCompletionItem);
            this.SelectionStatus = new CompletionSelectionStatus(
                selectedCompletionItem, isSelected: !isSoftSelected, isUnique: true);
        }
Пример #10
0
        /// <summary>
        /// Matches the completion list.
        /// </summary>
        /// <param name="completionList">The list of completions.</param>
        /// <param name="matchType">The <see cref="CompletionMatchType"/>.</param>
        /// <param name="caseSensitive"><c>true</c> if the match is case-sensitive, otherwise <c>false</c>.</param>
        /// <returns>A <see cref="CompletionMatchResult"/>.</returns>
        /// <exception cref="InvalidOperationException">The span to which this completion applies is null.</exception>
        protected CompletionMatchResult MatchCompletionList
        (
            IList <Completion> completionList,
            CompletionMatchType matchType,
            bool caseSensitive
        )
        {
            if (_applicableTo == null)
            {
                throw new InvalidOperationException("Cannot match completion set with no applicability span.");
            }

            // Get the actual text from the buffer
            ITextSnapshot snapshot   = _applicableTo.TextBuffer.CurrentSnapshot;
            string        bufferText = _applicableTo.GetText(snapshot);

            // Short-circuit if we detect that there's no text to match.
            if (bufferText.Length == 0)
            {
                return(null);
            }

            Completion bestMatch = null;
            int        bestMatchNumCharsMatched = -1;
            bool       uniqueMatch = false;
            bool       fullMatch   = false;

            foreach (Completion completion in completionList)
            {
                // Determine what text should be matched.
                string matchText = string.Empty;
                if (matchType == CompletionMatchType.MatchDisplayText)
                {
                    matchText = completion.DisplayText;
                }
                else if (matchType == CompletionMatchType.MatchInsertionText)
                {
                    matchText = completion.InsertionText;
                }

                // Count the number of characters that match.
                int numCharsMatched = 0;
                for (int i = 0; i < bufferText.Length; i++)
                {
                    if (i >= matchText.Length)
                    {
                        break;
                    }
                    else
                    {
                        char b = bufferText[i];
                        char m = matchText[i];
                        if (!caseSensitive)
                        {
                            b = char.ToLowerInvariant(b);
                            m = char.ToLowerInvariant(m);
                        }

                        if (b == m)
                        {
                            numCharsMatched++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                // See if this completion is the best match.
                if (numCharsMatched > bestMatchNumCharsMatched)
                {
                    bestMatchNumCharsMatched = numCharsMatched;
                    bestMatch = completion;

                    // Since we've found a completion with more characters matching than any before, this is now a unique
                    // match
                    uniqueMatch = true;

                    // Let's see if this is also a full match.  In order to be a full match, all of the characters in the buffer
                    // must have matched.  In addition, we must have at least one character matched.
                    if ((numCharsMatched == bufferText.Length) && (bestMatchNumCharsMatched > 0))
                    {
                        fullMatch = true;
                    }
                }
                else if (numCharsMatched == bestMatchNumCharsMatched)
                {
                    // We've come across a completion that matches exactly the same number of buffer characters as a previous
                    // completion.  We're going to take that to mean that we no longer have a unique match.
                    uniqueMatch = false;

                    // Now that we know we no longer have a unique match, we may be able to break out of the loop without looking at
                    // any more completions.  We can only do this if we've matched all of the characters in the applicable-to text,
                    // however.  If we've matched all of the text so far, there can't possibly be another completion that will match
                    // more.
                    if (fullMatch)
                    {
                        break;
                    }
                }
            }

            // If there was a match, return it.
            if (bestMatch != null)
            {
                return(new CompletionMatchResult()
                {
                    SelectionStatus = new CompletionSelectionStatus(bestMatch, fullMatch, uniqueMatch),
                    CharsMatchedCount = bestMatchNumCharsMatched >= 0 ? bestMatchNumCharsMatched : 0
                });
            }

            // We didn't find anything.  Return an "empty" match status.
            return(null);
        }
Пример #11
0
        internal void SetCompletionItems(
            IList <CompletionItem> completionItems,
            CompletionItem selectedItem,
            CompletionItem presetBuilder,
            bool suggestionMode,
            bool isSoftSelected,
            ImmutableArray <CompletionItemFilter> completionItemFilters,
            IReadOnlyDictionary <CompletionItem, string> completionItemToFilterText)
        {
            this._foregroundObject.AssertIsForeground();

            VSCompletion selectedCompletionItem = null;

            // Initialize the completion map to a reasonable default initial size (+1 for the builder)
            _completionItemToVSCompletion = _completionItemToVSCompletion ?? new Dictionary <CompletionItem, VSCompletion>(completionItems.Count + 1);
            _completionItemToFilterText   = completionItemToFilterText;

            try
            {
                this.WritableCompletionBuilders.BeginBulkOperation();
                this.WritableCompletionBuilders.Clear();

                // If more than one filter was provided, then present it to the user.
                if (_filters == null && completionItemFilters.Length > 1)
                {
                    _filters = completionItemFilters.Select(f => new IntellisenseFilter2(this, f))
                               .ToArray();
                }

                var applicableToText = this.ApplicableTo.GetText(this.ApplicableTo.TextBuffer.CurrentSnapshot);
                var filteredBuilder  = new CompletionItem(null, applicableToText,
                                                          this.ApplicableTo.GetSpan(this.ApplicableTo.TextBuffer.CurrentSnapshot).Span.ToTextSpan(), isBuilder: true);

                var showBuilder = suggestionMode || presetBuilder != null;
                var bestBuilder = applicableToText.Length > 0 ? filteredBuilder : presetBuilder ?? filteredBuilder;

                if (showBuilder && bestBuilder != null)
                {
                    var builderCompletion = ConvertCompletionItem(bestBuilder);
                    this.WritableCompletionBuilders.Add(builderCompletion);

                    if (selectedItem != null && selectedItem.IsBuilder)
                    {
                        selectedCompletionItem = builderCompletion;
                    }
                }
            }
            finally
            {
                this.WritableCompletionBuilders.EndBulkOperation();
            }

            try
            {
                this.WritableCompletions.BeginBulkOperation();
                this.WritableCompletions.Clear();

                foreach (var item in completionItems)
                {
                    var completionItem = ConvertCompletionItem(item);
                    this.WritableCompletions.Add(completionItem);

                    if (item == selectedItem)
                    {
                        selectedCompletionItem = completionItem;
                    }
                }
            }
            finally
            {
                this.WritableCompletions.EndBulkOperation();
            }

            this.SelectionStatus = new CompletionSelectionStatus(
                selectedCompletionItem, isSelected: !isSoftSelected, isUnique: selectedCompletionItem != null);
        }