Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.net 3.5 and .net core 3.1 targets #211

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Modified autocomplete compare way
Modified the way that match autocompletion items, in order to get all that contains selected text, instead of filter only those that start with. For example, if you have 2 words "checked" and "unchecked" and search for "ch", you will find "checked" at first, and then "unchecked" because "unchecked" contains "ch" too.
  • Loading branch information
NinjaFighter committed Jan 21, 2020
commit 8b5e3e952902a3bd9b7991ec9498155c13cfe985
2 changes: 1 addition & 1 deletion FastColoredTextBox/AutocompleteItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public virtual string GetTextForReplace()
/// </summary>
public virtual CompareResult Compare(string fragmentText)
{
if (Text.StartsWith(fragmentText, StringComparison.InvariantCultureIgnoreCase) &&
if(string.IsNullOrEmpty(fragmentText) || Text.ToLowerInvariant().Contains(fragmentText.ToLowerInvariant()) &&
Text != fragmentText)
return CompareResult.VisibleAndSelected;

Expand Down
22 changes: 17 additions & 5 deletions FastColoredTextBox/AutocompleteMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,23 +402,35 @@ internal void DoAutocomplete(bool forced)
&& (tb.Selection.Start > fragment.Start || text.Length == 0/*pops up only if caret is after first letter*/)))
{
Menu.Fragment = fragment;
bool foundSelected = false;
//bool foundSelected = false;
AutocompleteItem foundItem = null;
//build popup menu
foreach (var item in sourceItems)
{
item.Parent = Menu;
CompareResult res = item.Compare(text);
if(res != CompareResult.Hidden)
visibleItems.Add(item);
if (res == CompareResult.VisibleAndSelected && !foundSelected)
if (res == CompareResult.VisibleAndSelected && foundItem != null)
{
foundSelected = true;
FocussedItemIndex = visibleItems.Count - 1;
foundItem = item;
//FocussedItemIndex = visibleItems.Count - 1;
}
}

if (foundSelected)
visibleItems.Sort((AutocompleteItem a, AutocompleteItem b)=> {
var indexA = a.Text.IndexOf(text);
var indexB = b.Text.IndexOf(text);

return indexA - indexB;

});

if (foundItem != null)
{

FocussedItemIndex = visibleItems.IndexOf(foundItem);

AdjustScroll();
DoSelectedVisible();
}
Expand Down