Components: Extract the autocomplete matcher into a separate function#76957
Components: Extract the autocomplete matcher into a separate function#76957
Conversation
562422f to
f7e3182
Compare
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @SavvyShah, @gwwar. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: -54 B (0%) Total Size: 7.73 MB
ℹ️ View Unchanged
|
|
Flaky tests detected in 3fbfca7. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23867423472
|
| const textAfterSelection = textContent | ||
| ? getTextContent( | ||
| slice( record, undefined, getTextContent( record ).length ) | ||
| ) | ||
| : ''; |
There was a problem hiding this comment.
This refactor now computes textAfterSelection before we know whether there is any viable match. On trunk, that slice()/getTextContent() work only happened after the cheaper trigger and mismatch guards had already passed.
Not sure how much of a hotspot this is without perf measurements, but could be potential point to make lazier.
There was a problem hiding this comment.
Changed arg to getTextAfterSelection. Since this is an internal function, I don't think there's a need to support function|string argument time at the moment.
| if ( tooDistantFromTrigger ) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
Small behavior change here: on trunk, the textWithoutTrigger.length > 50 case bails out without calling reset(), while the version in this PR will call the reset(). That seems like a reasonable improvement, but it is a visible change in behavior. It may be worth either noting that in the PR description or adding a test so the intent is documented.
There was a problem hiding this comment.
Right, reset seems a better approach. @ockham also asked a similar question when this condition was introduced - #30649 (comment).
Added note to the PR description.
|
@mirka, let me know if this is good to merge. I have a couple of follow-ups that I would like to land after this refactoring. |
|
Yes, looks good to go 👍 |
…#76957) Unlinked contributors: SavvyShah, gwwar. Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: mirka <0mirka00@git.wordpress.org> Co-authored-by: gziolo <gziolo@git.wordpress.org> Co-authored-by: USERSATOSHI <tusharbharti@git.wordpress.org>
What?
Closes #30969.
Alternative to #71417. Pure matcher function; tests without benchmarking; perf tests already track this. Return types.
PR extracts autocomplete matching logic into a standalone pure function.
Why?
The matching algorithm was buried inside a
useEffect, making it untestable in isolation. Also simplifies by removing dead code (no-op unicode regex, unnecessaryescapeRegExp+ regex construction).This should make it easier to fix and test cases like #61919.
How?
getAutocompleteMatch()takes text content, completers, and a few flags.slice()+removeAccents(). Improves upon Autocomplete: reduce work before finding trigger #48327.Testing Instructions
The
test/e2e/specs/editor/various/autocomplete-and-mentions.spec.jscovers most of the cases.Testing Instructions for Keyboard
Same.
Use of AI Tools
Claude did most of the grunt work. I reviewed and validated results.