Skip to content

Fuzzy text search, part 1: algorithm & searching shortcuts#160

Open
juli27 wants to merge 9 commits into
musescore:mainfrom
juli27:addFuzzySearchPart1
Open

Fuzzy text search, part 1: algorithm & searching shortcuts#160
juli27 wants to merge 9 commits into
musescore:mainfrom
juli27:addFuzzySearchPart1

Conversation

@juli27

@juli27 juli27 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Part of: musescore/MuseScore#15983
Rebased after framework split (previous PR: musescore/MuseScore#32634)
Requires: #14

This PR implements a fuzzy filter and score sorter and adds fuzzy search support to the shortcuts list in preferences.

Overview of the Algorithm

  1. Split the user input into words (words are assumed to be separated by whitespace)
  2. Try to find a fuzzy match of each individual word within every candidate item. Matches have an error budget of 1 when the searched word is 4 characters or longer. An additional error is added to the budget every 8 characters.
  3. The resulting items are then sorted by match similarity (amount of errors between the user input and the item). If the amount of errors is equal they are sorted further by whether the match spans a whole word or starts at a word in the item.
    An error can be a missing character, an excess character, different characters, and swapped characters.

The implemented fuzzy search algorithm is an extended version of Seller's variant for string search of the Wagner-Fischer algorithm (this is the algorithm that is used in our implementation of muse::string::levenshteinDistance). It is extended to also allow transposition errors (Damerau-Levenshtein distance).

  • I signed the CLA as username:
  • The title of the PR describes the problem it addresses.
  • Each commit's message describes its purpose and effects, and references the issue it resolves. If changes are extensive, there is a sequence of easily reviewable commits.
  • The code in the PR follows the coding rules.
  • I understand all aspects of the code I'm contributing and I'm able to explain it if requested.
  • The code compiles and runs on my machine, preferably after each commit individually. I have manually tested and verified that my changes fulfil their intended purpose.
  • No prior attempts to resolve this problem exist, or if they do, I listed them in my PR description and described how I avoided repeating past mistakes.
  • There are no unnecessary changes.
  • I created a unit test or vtest to verify the changes I made (if applicable).

juli27 added 5 commits July 17, 2026 19:21
- Remove unused private method `reset()`
- Remove unused invokable method `refresh()`
- Remove unused signal `filtersChanged`
  - it is never emitted
- expose roleIdFromName (formerly roleKey)
  - this is needed by later commits
This prevented multiple filters from using the same role.
Also, filter subclasses won't necessarily operate on a single role.
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds a UTF-32 fuzzy matcher with edit-distance matching, transposition support, iterators, and tests. String splitting now uses templated string-view-based overloads. QML filtering and sorting are reorganized around shared Filter and Sorter bases, with new fuzzy filtering and score sorting components. SortFilterProxyModel delegates row evaluation and ordering to these components. Both shortcut implementations now build structured search keys and use case-insensitive fuzzy filtering with score-based ordering.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is missing the required 'Resolves: #NNNNN' line from the template, so it doesn't fully satisfy the repository format. Add the required issue reference line, e.g. 'Resolves: #NNNNN', and keep the rest of the template sections intact.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the fuzzy search algorithm and shortcut integration, matching the main change in the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@framework/global/stringsearch.cpp`:
- Around line 92-96: Guard FuzzyMatcher::findMatch against an uninitialized or
cleared matcher before accessing m_suffixTable, returning no match when the
suffix table is empty. Ensure empty() and begin()-based iteration remain safe
both before the first match() and after clear(), and add regression coverage for
those states.

In `@framework/uicomponents/qml/Muse/UiComponents/fuzzyfilter.cpp`:
- Around line 88-97: Update FuzzyFilter::setCaseSensitivity to assign
m_caseSensitivity before calling compilePattern(), while preserving the existing
no-op check and dataChanged emission. Add a regression test that sets a
mixed-case pattern, switches to Qt::CaseInsensitive, and verifies matching uses
the new sensitivity.

In `@framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.cpp`:
- Around line 166-176: Update the source-model replacement flow in
SortFilterProxyModel so invalidateFilters() runs before
QSortFilterProxyModel::setSourceModel(sourceModel), then explicitly invalidate
the proxy after clearing the FuzzyFilter caches. Preserve the existing signal
disconnection and reconnection behavior while ensuring old fuzzy scores cannot
carry across source replacement.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d397ba58-6e07-43ff-8dc7-95afd7f93343

📥 Commits

Reviewing files that changed from the base of the PR and between d4bd027 and fee0076.

📒 Files selected for processing (30)
  • framework/global/CMakeLists.txt
  • framework/global/stringsearch.cpp
  • framework/global/stringsearch.h
  • framework/global/stringutils.cpp
  • framework/global/stringutils.h
  • framework/global/tests/CMakeLists.txt
  • framework/global/tests/stringsearch_tests.cpp
  • framework/global/tests/stringutils_tests.cpp
  • framework/shortcuts/qml/Muse/Shortcuts/internal/ShortcutsList.qml
  • framework/shortcuts/qml/Muse/Shortcuts/shortcutsmodel.cpp
  • framework/shortcuts_v2/qml/Muse/Shortcuts/internal/ShortcutsList.qml
  • framework/shortcuts_v2/qml/Muse/Shortcuts/shortcutsmodel.cpp
  • framework/uicomponents/qml/Muse/UiComponents/CMakeLists.txt
  • framework/uicomponents/qml/Muse/UiComponents/ValueList.qml
  • framework/uicomponents/qml/Muse/UiComponents/filter.cpp
  • framework/uicomponents/qml/Muse/UiComponents/filter.h
  • framework/uicomponents/qml/Muse/UiComponents/filtervalue.cpp
  • framework/uicomponents/qml/Muse/UiComponents/filtervalue.h
  • framework/uicomponents/qml/Muse/UiComponents/fuzzyfilter.cpp
  • framework/uicomponents/qml/Muse/UiComponents/fuzzyfilter.h
  • framework/uicomponents/qml/Muse/UiComponents/fuzzyscoresorter.cpp
  • framework/uicomponents/qml/Muse/UiComponents/fuzzyscoresorter.h
  • framework/uicomponents/qml/Muse/UiComponents/sorter.cpp
  • framework/uicomponents/qml/Muse/UiComponents/sorter.h
  • framework/uicomponents/qml/Muse/UiComponents/sortervalue.cpp
  • framework/uicomponents/qml/Muse/UiComponents/sortervalue.h
  • framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.cpp
  • framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.h
  • framework/uicomponents/qml/Muse/UiComponents/tests/CMakeLists.txt
  • framework/uicomponents/qml/Muse/UiComponents/tests/sortfilterproxymodel_tests.cpp

Comment thread framework/global/stringsearch.cpp
Comment thread framework/uicomponents/qml/Muse/UiComponents/fuzzyfilter.cpp
juli27 added 4 commits July 19, 2026 15:48
The current implementation is "Seller's variant for string search" of the
Wagner-Fischer edit distance algorithm[^1], extended to also allow
transposition errors (Damerau-Levenshtein distance (optimal string
alignment)

[^1]: https://en.wikipedia.org/w/index.php?title=Wagner%E2%80%93Fischer_algorithm&oldid=1344122699#Seller's_variant_for_string_search
The function can now be used with any std string type.
The output can now be written into any output iterator.
@juli27
juli27 force-pushed the addFuzzySearchPart1 branch from fee0076 to 72c0036 Compare July 19, 2026 13:48

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.cpp (1)

53-59: 🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win

Coalesce async invalidations to prevent redundant re-evaluations.

Using QTimer::singleShot for asynchronous invalidation will queue a separate invalidateRows execution for every dataChanged emission. If multiple filters are modified synchronously, or if a filter rapidly emits dataChanged, invalidateRows() will execute multiple times in the next event loop iteration, causing redundant O(N) re-filtering overheads.

To prevent this, coalesce the updates by using a dedicated single-shot QTimer member variable.

🛠️ Proposed solution

Add a timer member to SortFilterProxyModel.h:

QTimer m_asyncUpdateTimer;

In the constructor, configure it and use start() to coalesce overlapping calls:

m_asyncUpdateTimer.setSingleShot(true);
m_asyncUpdateTimer.setInterval(0);
connect(&m_asyncUpdateTimer, &QTimer::timeout, this, invalidateRows);

auto onFilterChanged = [this, invalidateRows](Filter* changedFilter) {
    if (changedFilter->async()) {
        m_asyncUpdateTimer.start(); // Resets and coalesces timer
    } else {
        invalidateRows();
    }
};
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.cpp` around
lines 53 - 59, Replace the per-emission QTimer::singleShot scheduling in the
onFilterChanged callback with a dedicated single-shot QTimer member on
SortFilterProxyModel. Configure the timer with a zero interval, connect its
timeout to invalidateRows, and call start() for asynchronous filters so repeated
changes coalesce into one invalidation; keep synchronous filters calling
invalidateRows() immediately.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.cpp`:
- Around line 173-178: Remove the sourceModel dataChanged connection to
SortFilterProxyModel::invalidateFilters so normal QSortFilterProxyModel
row-level optimization remains intact. Update FuzzyFilter to evict only affected
cached QPersistentModelIndex entries when the source model emits dataChanged,
using either a targeted cache-invalidation contract on Filter or FuzzyFilter’s
sourceModel connection; retain full invalidation only for model reset handling.

---

Outside diff comments:
In `@framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.cpp`:
- Around line 53-59: Replace the per-emission QTimer::singleShot scheduling in
the onFilterChanged callback with a dedicated single-shot QTimer member on
SortFilterProxyModel. Configure the timer with a zero interval, connect its
timeout to invalidateRows, and call start() for asynchronous filters so repeated
changes coalesce into one invalidation; keep synchronous filters calling
invalidateRows() immediately.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4c945ce8-6ec0-41e4-a46d-1bfd99a19c23

📥 Commits

Reviewing files that changed from the base of the PR and between fee0076 and 72c0036.

📒 Files selected for processing (21)
  • framework/global/CMakeLists.txt
  • framework/global/stringsearch.cpp
  • framework/global/stringsearch.h
  • framework/global/stringutils.cpp
  • framework/global/stringutils.h
  • framework/global/tests/CMakeLists.txt
  • framework/global/tests/stringsearch_tests.cpp
  • framework/global/tests/stringutils_tests.cpp
  • framework/shortcuts/qml/Muse/Shortcuts/internal/ShortcutsList.qml
  • framework/shortcuts/qml/Muse/Shortcuts/shortcutsmodel.cpp
  • framework/shortcuts_v2/qml/Muse/Shortcuts/internal/ShortcutsList.qml
  • framework/shortcuts_v2/qml/Muse/Shortcuts/shortcutsmodel.cpp
  • framework/uicomponents/qml/Muse/UiComponents/CMakeLists.txt
  • framework/uicomponents/qml/Muse/UiComponents/filter.cpp
  • framework/uicomponents/qml/Muse/UiComponents/filter.h
  • framework/uicomponents/qml/Muse/UiComponents/fuzzyfilter.cpp
  • framework/uicomponents/qml/Muse/UiComponents/fuzzyfilter.h
  • framework/uicomponents/qml/Muse/UiComponents/fuzzyscoresorter.cpp
  • framework/uicomponents/qml/Muse/UiComponents/fuzzyscoresorter.h
  • framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.cpp
  • framework/uicomponents/qml/Muse/UiComponents/sortfilterproxymodel.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant