예제 #1
0
        public virtual CompletionSelectionStatus FindMatchedEntry(ICompletionDataList completionDataList, MruCache cache, string partialWord, List <int> filteredItems)
        {
            // default - word with highest match rating in the list.
            int idx = -1;

            if (DefaultCompletionString != null && DefaultCompletionString.StartsWith(partialWord, StringComparison.OrdinalIgnoreCase))
            {
                partialWord = DefaultCompletionString;
            }
            StringMatcher matcher = null;

            if (!string.IsNullOrEmpty(partialWord))
            {
                matcher = CompletionMatcher.CreateCompletionMatcher(partialWord);
                string bestWord          = null;
                int    bestRank          = int.MinValue;
                int    bestIndex         = 0;
                int    bestIndexPriority = int.MinValue;
                for (int i = 0; i < filteredItems.Count; i++)
                {
                    int index = filteredItems [i];
                    var data  = completionDataList [index];
                    if (bestIndexPriority > data.PriorityGroup)
                    {
                        continue;
                    }
                    string text = data.DisplayText;
                    int    rank;
                    if (!matcher.CalcMatchRank(text, out rank))
                    {
                        continue;
                    }
                    if (rank > bestRank || data.PriorityGroup > bestIndexPriority)
                    {
                        bestWord          = text;
                        bestRank          = rank;
                        bestIndex         = i;
                        bestIndexPriority = data.PriorityGroup;
                    }
                }

                if (bestWord != null)
                {
                    idx = bestIndex;
                    // exact match found.
                    if (string.Compare(bestWord, partialWord ?? "", true) == 0)
                    {
                        return(new CompletionSelectionStatus(idx));
                    }
                }
            }

            CompletionData currentData;
            int            bestMruIndex;

            if (idx >= 0)
            {
                currentData  = completionDataList [filteredItems [idx]];
                bestMruIndex = cache.GetIndex(currentData);
            }
            else
            {
                bestMruIndex = int.MaxValue;
                currentData  = null;
            }
            for (int i = 0; i < filteredItems.Count; i++)
            {
                var mruData     = completionDataList [filteredItems [i]];
                int curMruIndex = cache.GetIndex(mruData);
                if (curMruIndex == 1)
                {
                    continue;
                }
                if (curMruIndex < bestMruIndex)
                {
                    int r1 = 0, r2 = 0;
                    if (currentData == null || matcher != null && matcher.CalcMatchRank(mruData.DisplayText, out r1) && matcher.CalcMatchRank(currentData.DisplayText, out r2))
                    {
                        if (r1 >= r2 || partialWord.Length == 0 || partialWord.Length == 1 && mruData.DisplayText [0] == partialWord [0])
                        {
                            bestMruIndex = curMruIndex;
                            idx          = i;
                            currentData  = mruData;
                        }
                    }
                }
            }
            return(new CompletionSelectionStatus(idx));
        }