Пример #1
0
        /// <summary>
        /// Gets the best match in <see cref="CompletionSet.Completions"/>
        /// </summary>
        /// <returns></returns>
        protected virtual CompletionSelectionStatus GetBestMatch()
        {
            Debug.Assert(ApplicableTo != null, "You must initialize " + nameof(ApplicableTo) + " before calling this method");
            var inputText        = SearchText;
            var completionFilter = CreateCompletionFilter(inputText);
            int matches          = 0;
            var selector         = new BestMatchSelector(inputText);
            var mruSelectionCase = default(MruSelection);
            var mruSelection     = default(MruSelection);

            if (inputText.Length > 0)
            {
                foreach (var completion in Completions)
                {
                    if (!completionFilter.IsMatch(completion))
                    {
                        continue;
                    }
                    matches++;
                    selector.Select(completion);

                    if (completion.DisplayText.StartsWith(searchText, StringComparison.Ordinal))
                    {
                        int currentMruIndex = GetMruIndex(completion);
                        if (mruSelectionCase.Completion == null || currentMruIndex < mruSelectionCase.Index)
                        {
                            mruSelectionCase = new MruSelection(completion, currentMruIndex);
                        }
                    }
                    else if (completion.DisplayText.StartsWith(searchText, StringComparison.OrdinalIgnoreCase))
                    {
                        int currentMruIndex = GetMruIndex(completion);
                        if (mruSelection.Completion == null || currentMruIndex < mruSelection.Index)
                        {
                            mruSelection = new MruSelection(completion, currentMruIndex);
                        }
                    }
                }
            }

            // If it was an exact match, don't use the MRU-selected completion. Eg.
            // local 'i' exists, and we previously typed 'int', and we've just typed 'i',
            // then select 'i' and not 'int'
            var selectedCompletion = mruSelectionCase.Completion ?? mruSelection.Completion ?? selector.Result;

            if (selector.Result != null && inputText.Equals(selector.Result.TryGetFilterText(), StringComparison.OrdinalIgnoreCase))
            {
                selectedCompletion = selector.Result;
            }

            bool isSelected = selectedCompletion != null;
            bool isUnique   = matches == 1;

            return(new CompletionSelectionStatus(selectedCompletion, isSelected, isUnique));
        }
Пример #2
0
        /// <summary>
        /// Gets the best match in <see cref="FilteredCollection"/>
        /// </summary>
        /// <returns></returns>
        protected virtual CurrentCompletion GetBestMatch()
        {
            Debug.Assert(ApplicableTo != null, "You must initialize " + nameof(ApplicableTo) + " before calling this method");
            var inputText        = ApplicableTo.GetText(ApplicableTo.TextBuffer.CurrentSnapshot);
            var completionFilter = CreateCompletionFilter(inputText);
            int matches          = 0;
            var selector         = new BestMatchSelector(inputText);

            if (inputText.Length > 0)
            {
                foreach (var completion in filteredCompletions)
                {
                    if (!completionFilter.IsMatch(completion))
                    {
                        continue;
                    }
                    matches++;
                    selector.Select(completion);
                }
            }
            bool isSelected = selector.Result != null;

            return(new CurrentCompletion(selector.Result, isSelected: isSelected, isUnique: matches == 1));
        }
Пример #3
0
		/// <summary>
		/// Gets the best match in <see cref="CompletionSet.Completions"/>
		/// </summary>
		/// <returns></returns>
		protected virtual CompletionSelectionStatus GetBestMatch() {
			Debug.Assert(ApplicableTo != null, "You must initialize " + nameof(ApplicableTo) + " before calling this method");
			var inputText = SearchText;
			var completionFilter = CreateCompletionFilter(inputText);
			int matches = 0;
			var selector = new BestMatchSelector(inputText);
			var mruSelectionCase = default(MruSelection);
			var mruSelection = default(MruSelection);
			if (inputText.Length > 0) {
				foreach (var completion in Completions) {
					if (!completionFilter.IsMatch(completion))
						continue;
					matches++;
					selector.Select(completion);

					if (completion.DisplayText.StartsWith(searchText, StringComparison.Ordinal)) {
						int currentMruIndex = GetMruIndex(completion);
						if (mruSelectionCase.Completion == null || currentMruIndex < mruSelectionCase.Index)
							mruSelectionCase = new MruSelection(completion, currentMruIndex);
					}
					else if (completion.DisplayText.StartsWith(searchText, StringComparison.OrdinalIgnoreCase)) {
						int currentMruIndex = GetMruIndex(completion);
						if (mruSelection.Completion == null || currentMruIndex < mruSelection.Index)
							mruSelection = new MruSelection(completion, currentMruIndex);
					}
				}
			}

			// If it was an exact match, don't use the MRU-selected completion. Eg.
			// local 'i' exists, and we previously typed 'int', and we've just typed 'i',
			// then select 'i' and not 'int'
			var selectedCompletion = mruSelectionCase.Completion ?? mruSelection.Completion ?? selector.Result;
			if (selector.Result != null && inputText.Equals(selector.Result.TryGetFilterText(), StringComparison.OrdinalIgnoreCase))
				selectedCompletion = selector.Result;

			bool isSelected = selectedCompletion != null;
			bool isUnique = matches == 1;
			return new CompletionSelectionStatus(selectedCompletion, isSelected, isUnique);
		}