Example #1
0
        internal static CompletionSelectionStatus SelectBestMatch(this CompletionSet set, CompletionMatchType matchType, bool caseSensitive)
        {
            CompletionMatchResult matchedCompletions        = set.MatchCompletionList(set.Completions, matchType, caseSensitive);
            CompletionMatchResult matchedCompletionBuilders = set.MatchCompletionList(set.CompletionBuilders, matchType, caseSensitive);
            int completionBuilderCount = 0;

            if (matchedCompletionBuilders != null)
            {
                completionBuilderCount = (matchedCompletionBuilders.CharsMatchedCount + (matchedCompletionBuilders.SelectionStatus.IsSelected ? 1 : 0)) + (matchedCompletionBuilders.SelectionStatus.IsUnique ? 1 : 0);
            }
            int completionCount = 0;

            if (matchedCompletions != null)
            {
                completionCount = (matchedCompletions.CharsMatchedCount + (matchedCompletions.SelectionStatus.IsSelected ? 1 : 0)) + (matchedCompletions.SelectionStatus.IsUnique ? 1 : 0);
            }
            if ((completionBuilderCount > completionCount) && (matchedCompletionBuilders != null))
            {
                set.SelectionStatus = matchedCompletionBuilders.SelectionStatus;
            }
            else if (matchedCompletions != null)
            {
                set.SelectionStatus = matchedCompletions.SelectionStatus;
            }
            else if (set.Completions.Count > 0)
            {
                if (!set.Completions.Contains(set.SelectionStatus.Completion))
                {
                    set.SelectionStatus = new CompletionSelectionStatus(set.Completions[0], false, false);
                }
            }
            else if (set.CompletionBuilders.Count > 0)
            {
                if (!set.CompletionBuilders.Contains(set.SelectionStatus.Completion))
                {
                    set.SelectionStatus = new CompletionSelectionStatus(set.CompletionBuilders[0], false, false);
                }
            }
            else
            {
                set.SelectionStatus = new CompletionSelectionStatus(null, false, false);
            }

            return(set.SelectionStatus);
        }
        private static CompletionMatchResult MatchCompletionList(this CompletionSet set, IList<Completion> completionList, CompletionMatchType matchType, bool caseSensitive)
        {
            if (set.ApplicableTo == null)
            {
                throw new InvalidOperationException("Cannot match completion set with no applicability span.");
            }

            ITextSnapshot currentSnapshot = set.ApplicableTo.TextBuffer.CurrentSnapshot;
            string text = set.ApplicableTo.GetText(currentSnapshot);
            if (text.Length != 0)
            {
                Completion bestMatch = null;
                int maxMatchPosition = -1;
                bool isUnique = false;
                bool isSelected = false;
                foreach (Completion currentCompletion in completionList)
                {
                    string displayText = string.Empty;
                    if (matchType == CompletionMatchType.MatchDisplayText)
                    {
                        displayText = currentCompletion.DisplayText;
                    }
                    else if (matchType == CompletionMatchType.MatchInsertionText)
                    {
                        displayText = currentCompletion.InsertionText;
                    }
                    int matchPositionCount = 0;
                    for (int i = 0; i < text.Length; i++)
                    {
                        if (i >= displayText.Length)
                        {
                            break;
                        }
                        char textChar = text[i];
                        char displayTextChar = displayText[i];
                        if (!caseSensitive)
                        {
                            textChar = char.ToLowerInvariant(textChar);
                            displayTextChar = char.ToLowerInvariant(displayTextChar);
                        }
                        if (textChar != displayTextChar)
                        {
                            break;
                        }
                        matchPositionCount++;
                    }
                    if (matchPositionCount > maxMatchPosition)
                    {
                        maxMatchPosition = matchPositionCount;
                        bestMatch = currentCompletion;
                        isUnique = true;
                        if ((matchPositionCount == text.Length) && (maxMatchPosition > 0))
                        {
                            isSelected = true;
                        }
                    }
                    else if (matchPositionCount == maxMatchPosition)
                    {
                        isUnique = false;
                        if (isSelected)
                        {
                            break;
                        }
                    }
                }
                if (bestMatch != null)
                {
                    CompletionMatchResult result = new CompletionMatchResult();
                    result.SelectionStatus = new CompletionSelectionStatus(bestMatch, isSelected, isUnique);
                    result.CharsMatchedCount = (maxMatchPosition >= 0) ? maxMatchPosition : 0;
                    return result;
                }
            }
            return null;
        }
 private static int GetMatchWeight(CompletionMatchResult builderResult)
 {
     return (builderResult.CharsMatchedCount) +
            (builderResult.SelectionStatus.IsSelected ? 1 : 0) +
            (builderResult.SelectionStatus.IsUnique ? 1 : 0);
 }
Example #4
0
        private static CompletionMatchResult MatchCompletionList(this CompletionSet set, IList <Completion> completionList, CompletionMatchType matchType, bool caseSensitive)
        {
            if (set.ApplicableTo == null)
            {
                throw new InvalidOperationException("Cannot match completion set with no applicability span.");
            }

            ITextSnapshot currentSnapshot = set.ApplicableTo.TextBuffer.CurrentSnapshot;
            string        text            = set.ApplicableTo.GetText(currentSnapshot);

            if (text.Length != 0)
            {
                Completion bestMatch        = null;
                int        maxMatchPosition = -1;
                bool       isUnique         = false;
                bool       isSelected       = false;
                foreach (Completion currentCompletion in completionList)
                {
                    string displayText = string.Empty;
                    if (matchType == CompletionMatchType.MatchDisplayText)
                    {
                        displayText = currentCompletion.DisplayText;
                    }
                    else if (matchType == CompletionMatchType.MatchInsertionText)
                    {
                        displayText = currentCompletion.InsertionText;
                    }
                    int matchPositionCount = 0;
                    for (int i = 0; i < text.Length; i++)
                    {
                        if (i >= displayText.Length)
                        {
                            break;
                        }
                        char textChar        = text[i];
                        char displayTextChar = displayText[i];
                        if (!caseSensitive)
                        {
                            textChar        = char.ToLowerInvariant(textChar);
                            displayTextChar = char.ToLowerInvariant(displayTextChar);
                        }
                        if (textChar != displayTextChar)
                        {
                            break;
                        }
                        matchPositionCount++;
                    }
                    if (matchPositionCount > maxMatchPosition)
                    {
                        maxMatchPosition = matchPositionCount;
                        bestMatch        = currentCompletion;
                        isUnique         = true;
                        if ((matchPositionCount == text.Length) && (maxMatchPosition > 0))
                        {
                            isSelected = true;
                        }
                    }
                    else if (matchPositionCount == maxMatchPosition)
                    {
                        isUnique = false;
                        if (isSelected)
                        {
                            break;
                        }
                    }
                }
                if (bestMatch != null)
                {
                    CompletionMatchResult result = new CompletionMatchResult();
                    result.SelectionStatus   = new CompletionSelectionStatus(bestMatch, isSelected, isUnique);
                    result.CharsMatchedCount = (maxMatchPosition >= 0) ? maxMatchPosition : 0;
                    return(result);
                }
            }
            return(null);
        }
Example #5
0
 private static int GetMatchWeight(CompletionMatchResult builderResult)
 {
     return((builderResult.CharsMatchedCount) +
            (builderResult.SelectionStatus.IsSelected ? 1 : 0) +
            (builderResult.SelectionStatus.IsUnique ? 1 : 0));
 }
Example #6
0
        /// <summary>
        /// Redetermines the best matching completion in the completion set.
        /// </summary>
        /// <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>
        protected void SelectBestMatch(CompletionMatchType matchType, bool caseSensitive)
        {
            CompletionMatchResult completionMatch = this.MatchCompletionList
                                                    (
                this.Completions,
                matchType,
                caseSensitive
                                                    );
            CompletionMatchResult builderMatch = this.MatchCompletionList
                                                 (
                this.CompletionBuilders,
                matchType,
                caseSensitive
                                                 );

            // Only select a builder if it's far-and-away better.  Otherwise, select a completion
            int builderScore = 0;

            if (builderMatch != null)
            {
                builderScore =
                    builderMatch.CharsMatchedCount +
                    (builderMatch.SelectionStatus.IsSelected ? 1 : 0) +
                    (builderMatch.SelectionStatus.IsUnique ? 1 : 0);
            }
            int completionScore = 0;

            if (completionMatch != null)
            {
                completionScore =
                    completionMatch.CharsMatchedCount +
                    (completionMatch.SelectionStatus.IsSelected ? 1 : 0) +
                    (completionMatch.SelectionStatus.IsUnique ? 1 : 0);
            }

            if ((builderScore > completionScore) && (builderMatch != null))
            {
                this.SelectionStatus = builderMatch.SelectionStatus;
            }
            else if (completionMatch != null)
            {
                this.SelectionStatus = completionMatch.SelectionStatus;
            }
            else if (this.Completions.Count > 0)
            {
                // If we've already got a pretty good selection, just leave it selected, but not fully-selected.
                if (this.Completions.Contains(this.SelectionStatus.Completion))
                {
                    this.SelectionStatus = new CompletionSelectionStatus(this.SelectionStatus.Completion, false, false);
                }
                else
                {
                    this.SelectionStatus = new CompletionSelectionStatus(this.Completions[0], false, false);
                }
            }
            else if (this.CompletionBuilders.Count > 0)
            {
                // If we've already got a pretty good selection, just leave it selected, but not fully-selected.
                if (this.CompletionBuilders.Contains(this.SelectionStatus.Completion))
                {
                    this.SelectionStatus = new CompletionSelectionStatus(this.SelectionStatus.Completion, false, false);
                }
                else
                {
                    this.SelectionStatus = new CompletionSelectionStatus(this.CompletionBuilders[0], false, false);
                }
            }
            else
            {
                this.SelectionStatus = new CompletionSelectionStatus(null, false, false);
            }
        }