1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-24 18:48:01 +02:00

Merge pull request #1277 from aharabada/FuzzyAutoComplete

Fuzzy string matiching for autocomplete
This commit is contained in:
Brian Fiete 2021-12-28 17:07:41 +01:00 committed by GitHub
commit 634dd7e509
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 656 additions and 97 deletions

View file

@ -78,6 +78,7 @@ namespace IDE.Compiler
public bool mCancelled;
public int32 mTextVersion = -1;
public bool mIsUserRequested;
public bool mDoFuzzyAutoComplete;
public Stopwatch mStopwatch ~ delete _;
public ProfileInstance mProfileInstance ~ _.Dispose();
}
@ -128,7 +129,7 @@ namespace IDE.Compiler
static extern char8* BfParser_GetDebugExpressionAt(void* bfParser, int32 cursorIdx);
[CallingConvention(.Stdcall), CLink]
static extern void* BfParser_CreateResolvePassData(void* bfSystem, int32 resolveType);
static extern void* BfParser_CreateResolvePassData(void* bfSystem, int32 resolveType, bool doFuzzyAutoComplete);
[CallingConvention(.Stdcall), CLink]
static extern bool BfParser_BuildDefs(void* bfParser, void* bfPassInstance, void* bfResolvePassData, bool fullRefresh);
@ -256,10 +257,10 @@ namespace IDE.Compiler
BfParser_GenerateAutoCompletionFrom(mNativeBfParser, srcPosition);
}
public BfResolvePassData CreateResolvePassData(ResolveType resolveType = ResolveType.Autocomplete)
public BfResolvePassData CreateResolvePassData(ResolveType resolveType = ResolveType.Autocomplete, bool doFuzzyAutoComplete = false)
{
var resolvePassData = new BfResolvePassData();
resolvePassData.mNativeResolvePassData = BfParser_CreateResolvePassData(mNativeBfParser, (int32)resolveType);
resolvePassData.mNativeResolvePassData = BfParser_CreateResolvePassData(mNativeBfParser, (int32)resolveType, doFuzzyAutoComplete);
return resolvePassData;
}

View file

@ -94,10 +94,10 @@ namespace IDE.Compiler
BfResolvePassData_SetDocumentationRequest(mNativeResolvePassData, entryName);
}
public static BfResolvePassData Create(ResolveType resolveType = ResolveType.Autocomplete)
public static BfResolvePassData Create(ResolveType resolveType = ResolveType.Autocomplete, bool doFuzzyAutoComplete = false)
{
var resolvePassData = new BfResolvePassData();
resolvePassData.mNativeResolvePassData = BfParser.[Friend]BfParser_CreateResolvePassData(null, (int32)resolveType);
resolvePassData.mNativeResolvePassData = BfParser.[Friend]BfParser_CreateResolvePassData(null, (int32)resolveType, doFuzzyAutoComplete);
return resolvePassData;
}
}

View file

@ -604,7 +604,7 @@ namespace IDE
No,
Yes,
BackupOnly
}
}
public List<String> mFonts = new .() ~ DeleteContainerAndItems!(_);
public float mFontSize = 12;
@ -613,6 +613,7 @@ namespace IDE
public bool mAutoCompleteRequireTab = false;
public bool mAutoCompleteOnEnter = true;
public bool mAutoCompleteShowDocumentation = true;
public bool mFuzzyAutoComplete = false;
public bool mShowLocatorAnim = true;
public bool mHiliteCursorReferences = true;
public bool mLockEditing;
@ -640,6 +641,7 @@ namespace IDE
sd.Add("AutoCompleteRequireTab", mAutoCompleteRequireTab);
sd.Add("AutoCompleteOnEnter", mAutoCompleteOnEnter);
sd.Add("AutoCompleteShowDocumentation", mAutoCompleteShowDocumentation);
sd.Add("FuzzyAutoComplete", mFuzzyAutoComplete);
sd.Add("ShowLocatorAnim", mShowLocatorAnim);
sd.Add("HiliteCursorReferences", mHiliteCursorReferences);
sd.Add("LockEditing", mLockEditing);
@ -670,6 +672,7 @@ namespace IDE
sd.Get("AutoCompleteRequireTab", ref mAutoCompleteRequireTab);
sd.Get("AutoCompleteOnEnter", ref mAutoCompleteOnEnter);
sd.Get("AutoCompleteShowDocumentation", ref mAutoCompleteShowDocumentation);
sd.Get("FuzzyAutoComplete", ref mFuzzyAutoComplete);
sd.Get("ShowLocatorAnim", ref mShowLocatorAnim);
sd.Get("HiliteCursorReferences", ref mHiliteCursorReferences);
sd.Get("LockEditing", ref mLockEditing);

View file

@ -382,6 +382,8 @@ namespace IDE.ui
public String mEntryInsert;
public String mDocumentation;
public Image mIcon;
public List<uint8> mMatchIndices;
public int32 mScore;
public float Y
{
@ -401,8 +403,41 @@ namespace IDE.ui
g.Draw(mIcon, 0, 0);
g.SetFont(IDEApp.sApp.mCodeFont);
g.DrawString(mEntryDisplay, GS!(20), 0);
}
float offset = GS!(20);
int index = 0;
for(char32 c in mEntryDisplay.DecodedChars)
loop:
{
if(mMatchIndices?.Contains((uint8)index) == true)
{
g.PushColor(DarkTheme.COLOR_MENU_FOCUSED);
defer:loop g.PopColor();
}
let str = StringView(mEntryDisplay, index, @c.NextIndex - index);
g.DrawString(str, offset, 0);
offset += IDEApp.sApp.mCodeFont.GetWidth(str);
index = @c.NextIndex;
}
}
public void SetMatches(Span<uint8> matchIndices)
{
mMatchIndices?.Clear();
if (!matchIndices.IsEmpty)
{
if(mMatchIndices == null)
mMatchIndices = new:(mAutoCompleteListWidget.mAlloc) List<uint8>(matchIndices.Length);
mMatchIndices.AddRange(matchIndices);
}
}
}
class Content : Widget
@ -602,8 +637,8 @@ namespace IDE.ui
mMaxWidth = Math.Max(mMaxWidth, entryWidth);
}*/
}
public void AddEntry(StringView entryType, StringView entryDisplay, Image icon, StringView entryInsert = default, StringView documentation = default)
public void AddEntry(StringView entryType, StringView entryDisplay, Image icon, StringView entryInsert = default, StringView documentation = default, List<uint8> matchIndices = null)
{
var entryWidget = new:mAlloc EntryWidget();
entryWidget.mAutoCompleteListWidget = this;
@ -615,10 +650,12 @@ namespace IDE.ui
entryWidget.mDocumentation = new:mAlloc String(documentation);
entryWidget.mIcon = icon;
entryWidget.SetMatches(matchIndices);
UpdateEntry(entryWidget, mEntryList.Count);
mEntryList.Add(entryWidget);
//mScrollContent.AddWidget(entryWidget);
}
}
public void EnsureSelectionVisible()
{
@ -1574,6 +1611,49 @@ namespace IDE.ui
mInvokeWidget.mIgnoreMove += ignoreMove ? 1 : -1;
}
// IDEHelper/third_party/FtsFuzzyMatch.h
[CallingConvention(.Stdcall), CLink]
static extern bool fts_fuzzy_match(char8* pattern, char8* str, ref int32 outScore, uint8* matches, int maxMatches);
/// Checks whether the given entry matches the filter and updates its score and match indices accordingly.
bool DoesFilterMatchFuzzy(AutoCompleteListWidget.EntryWidget entry, String filter)
{
if (filter.Length == 0)
return true;
if (filter.Length > entry.mEntryDisplay.Length)
return false;
int32 score = 0;
uint8[256] matches = ?;
if (!fts_fuzzy_match(filter.CStr(), entry.mEntryDisplay.CStr(), ref score, &matches, matches.Count))
{
entry.SetMatches(Span<uint8>(null, 0));
entry.mScore = score;
return false;
}
// Should be the amount of Unicode-codepoints in filter though it' probably faster to do it this way
int matchesLength = 0;
for (uint8 i = 0;; i++)
{
uint8 matchIndex = matches[i];
if ((matchIndex == 0 && i != 0) || i == uint8.MaxValue)
{
matchesLength = i;
break;
}
}
entry.SetMatches(Span<uint8>(&matches, matchesLength));
entry.mScore = score;
return true;
}
bool DoesFilterMatch(String entry, String filter)
{
if (filter.Length == 0)
@ -1640,6 +1720,9 @@ namespace IDE.ui
//return strnicmp(filter, initialStr, filterLen) == 0;
}
[LinkName("_stricmp")]
static extern int32 stricmp(char8* lhs, char8* rhs);
void UpdateData(String selectString, bool changedAfterInfo)
{
if ((mInsertEndIdx != -1) && (mInsertEndIdx < mInsertStartIdx))
@ -1692,14 +1775,21 @@ namespace IDE.ui
if (curString == ".")
curString.Clear();
bool doFuzzyAutoComplete = gApp.mSettings.mEditorSettings.mFuzzyAutoComplete;
for (int i < mAutoCompleteListWidget.mFullEntryList.Count)
{
var entry = mAutoCompleteListWidget.mFullEntryList[i];
//if (String.Compare(entry.mEntryDisplay, 0, curString, 0, curString.Length, true) == 0)
if (DoesFilterMatch(entry.mEntryDisplay, curString))
if (doFuzzyAutoComplete && DoesFilterMatchFuzzy(entry, curString))
{
mAutoCompleteListWidget.mEntryList.Add(entry);
mAutoCompleteListWidget.UpdateEntry(entry, visibleCount);
visibleCount++;
}
else if (!doFuzzyAutoComplete && DoesFilterMatch(entry.mEntryDisplay, curString))
{
mAutoCompleteListWidget.mEntryList.Add(entry);
mAutoCompleteListWidget.UpdateEntry(entry, visibleCount);
visibleCount++;
}
else
@ -1708,6 +1798,25 @@ namespace IDE.ui
}
}
if (doFuzzyAutoComplete)
{
// sort entries because the scores probably have changed
mAutoCompleteListWidget.mEntryList.Sort(scope (left, right) =>
{
if (left.mScore > right.mScore)
return -1;
else if (left.mScore < right.mScore)
return 1;
else
return ((stricmp(left.mEntryDisplay.CStr(), right.mEntryDisplay.CStr()) < 0) ? -1 : 1);
});
for (int i < mAutoCompleteListWidget.mEntryList.Count)
{
mAutoCompleteListWidget.UpdateEntry(mAutoCompleteListWidget.mEntryList[i], i);
}
}
if ((visibleCount == 0) && (mInvokeSrcPositions == null))
{
mPopulating = false;
@ -1853,6 +1962,7 @@ namespace IDE.ui
public void UpdateInfo(String info)
{
List<uint8> matchIndices = new:ScopedAlloc! .(256);
for (var entryView in info.Split('\n'))
{
StringView entryType = StringView(entryView);
@ -1863,13 +1973,35 @@ namespace IDE.ui
entryDisplay = StringView(entryView, tabPos + 1);
entryType = StringView(entryType, 0, tabPos);
}
StringView matches = default;
int matchesPos = entryDisplay.IndexOf('\x02');
matchIndices.Clear();
if (matchesPos != -1)
{
matches = StringView(entryDisplay, matchesPos + 1);
entryDisplay = StringView(entryDisplay, 0, matchesPos);
for(var sub in matches.Split(','))
{
if(sub.StartsWith('X'))
break;
var result = int64.Parse(sub, .HexNumber);
Debug.Assert((result case .Ok(let value)) && value <= uint8.MaxValue);
// TODO(FUZZY): we could save start and length instead of single chars
matchIndices.Add((uint8)result.Value);
}
}
StringView documentation = default;
int docPos = entryDisplay.IndexOf('\x03');
int docPos = matches.IndexOf('\x03');
if (docPos != -1)
{
documentation = StringView(entryDisplay, docPos + 1);
entryDisplay = StringView(entryDisplay, 0, docPos);
documentation = StringView(matches, docPos + 1);
matches = StringView(matches, 0, docPos);
}
StringView entryInsert = default;
@ -1892,15 +2024,27 @@ namespace IDE.ui
case "select":
default:
{
if ((!documentation.IsEmpty) && (mAutoCompleteListWidget != null))
if (((!documentation.IsEmpty) || (!matchIndices.IsEmpty)) && (mAutoCompleteListWidget != null))
{
while (entryIdx < mAutoCompleteListWidget.mEntryList.Count)
{
let entry = mAutoCompleteListWidget.mEntryList[entryIdx];
if ((entry.mEntryDisplay == entryDisplay) && (entry.mEntryType == entryType))
{
if (entry.mDocumentation == null)
if (!matchIndices.IsEmpty)
{
if (entry.mMatchIndices == null)
entry.mMatchIndices = new:(mAutoCompleteListWidget.[Friend]mAlloc) List<uint8>(matchIndices.GetEnumerator());
else
{
entry.mMatchIndices.Clear();
entry.mMatchIndices.AddRange(matchIndices);
}
}
if ((!documentation.IsEmpty) && entry.mDocumentation == null)
entry.mDocumentation = new:(mAutoCompleteListWidget.[Friend]mAlloc) String(documentation);
break;
}
entryIdx++;
@ -1982,9 +2126,9 @@ namespace IDE.ui
InvokeWidget oldInvokeWidget = null;
String selectString = null;
List<uint8> matchIndices = new:ScopedAlloc! .(256);
for (var entryView in info.Split('\n'))
{
Image entryIcon = null;
StringView entryType = StringView(entryView);
int tabPos = entryType.IndexOf('\t');
@ -1995,12 +2139,34 @@ namespace IDE.ui
entryType = StringView(entryType, 0, tabPos);
}
StringView matches = default;
int matchesPos = entryDisplay.IndexOf('\x02');
matchIndices.Clear();
if (matchesPos != -1)
{
matches = StringView(entryDisplay, matchesPos + 1);
entryDisplay = StringView(entryDisplay, 0, matchesPos);
for(var sub in matches.Split(','))
{
if(sub.StartsWith('X'))
break;
var result = int64.Parse(sub, .HexNumber);
Debug.Assert((result case .Ok(let value)) && value <= uint8.MaxValue);
// TODO(FUZZY): we could save start and length instead of single chars
matchIndices.Add((uint8)result.Value);
}
}
StringView documentation = default;
int docPos = entryDisplay.IndexOf('\x03');
int docPos = matches.IndexOf('\x03');
if (docPos != -1)
{
documentation = StringView(entryDisplay, docPos + 1);
entryDisplay = StringView(entryDisplay, 0, docPos);
documentation = StringView(matches, docPos + 1);
matches = StringView(matches, 0, docPos);
}
StringView entryInsert = default;
@ -2129,7 +2295,7 @@ namespace IDE.ui
if (!mInvokeOnly)
{
mIsFixit |= entryType == "fixit";
mAutoCompleteListWidget.AddEntry(entryType, entryDisplay, entryIcon, entryInsert, documentation);
mAutoCompleteListWidget.AddEntry(entryType, entryDisplay, entryIcon, entryInsert, documentation, matchIndices);
}
}
}

View file

@ -98,6 +98,7 @@ namespace IDE.ui
AddPropertiesItem(category, "Autocomplete Require Tab", "mAutoCompleteRequireTab");
AddPropertiesItem(category, "Autocomplete on Enter", "mAutoCompleteOnEnter");
AddPropertiesItem(category, "Autocomplete Show Documentation", "mAutoCompleteShowDocumentation");
AddPropertiesItem(category, "Fuzzy Autocomplete", "mFuzzyAutoComplete");
AddPropertiesItem(category, "Show Locator Animation", "mShowLocatorAnim");
AddPropertiesItem(category, "Hilite Symbol at Cursor", "mHiliteCursorReferences");

View file

@ -548,6 +548,11 @@ namespace IDE.ui
public ~this()
{
if (mProjectSource?.mEditData?.HasTextChanged() == true)
{
mProjectSource.ClearEditData();
}
if (mInPostRemoveUpdatePanels)
{
//Debug.WriteLine("Removing sourceViewPanel from mPostRemoveUpdatePanel {0} in ~this ", this);
@ -623,6 +628,7 @@ namespace IDE.ui
if (gApp.mDbgPerfAutocomplete)
resolveParams.mProfileInstance = Profiler.StartSampling("Autocomplete").GetValueOrDefault();
resolveParams.mIsUserRequested = options.HasFlag(.UserRequested);
resolveParams.mDoFuzzyAutoComplete = gApp.mSettings.mEditorSettings.mFuzzyAutoComplete;
Classify(.Autocomplete, resolveParams);
if (!resolveParams.mInDeferredList)
delete resolveParams;
@ -1854,7 +1860,9 @@ namespace IDE.ui
/*else (!isFullClassify) -- do we ever need to do this?
parser.SetCursorIdx(mEditWidget.mEditWidgetContent.CursorTextPos);*/
var resolvePassData = parser.CreateResolvePassData(resolveType);
bool doFuzzyAutoComplete = resolveParams?.mDoFuzzyAutoComplete ?? false;
var resolvePassData = parser.CreateResolvePassData(resolveType, doFuzzyAutoComplete);
if (resolveParams != null)
{
if (resolveParams.mLocalId != -1)