gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Fix autocomplete to prefer longest matching trigger for overlapping trigger

Open BugReportOnWeb opened this issue 1 year ago • 3 comments

What?

Closes #61919

This PR updates the logic used to match autocomplete triggers when multiple completers have overlapping prefixes. The fix ensures the longest matching trigger that ends closest to the cursor is selected, resulting in more accurate and expected behaviour for overlapping triggers.

Why?

This change is necessary to restore expected behaviour for overlapping autocomplete triggers such as @ and @@. Specially, the trigger closest to the cursor was selected, regardless of prefix length. This caused the shorter @ completer to be selected even when @@ was typed.

How?

The trigger matching logic now:

  • Iterates through all available completers.
  • Selects the one whose trigger ends furthest into the text.
  • If two triggers end at the same place, chooses the one with the longer prefix.

Testing Instructions

  1. Add the following snippet to add additional completer:
wp.hooks.addFilter(
  'editor.Autocomplete.completers',
  'some-plugin/some-name-here',
  (completers) => {
    completers = completers.filter((c) => c.triggerPrefix !== '@');

    completers.push({
      name: 'some-plugin/double-at',
      triggerPrefix: '@@',
      options: ['hello from double at completer'],
      getOptionLabel: (option) => option,
      getOptionCompletion: (option) => option,
    });

    completers.push({
      name: 'some-plugin/at',
      triggerPrefix: '@',
      options: ['hello from at completer'],
      getOptionLabel: (option) => option,
      getOptionCompletion: (option) => option,
    });

    return completers;
  },
);
  1. Type @hello and @@hello in a paragraph block.
  2. Confirm the correct suggestions appear:
    • @hello -> @ completer
    • @@hello -> @@ completer

Screenshots or screencast

Before

https://github.com/user-attachments/assets/123348a3-bf54-44ba-9d23-06381db21809

After

https://github.com/user-attachments/assets/87c0b17f-b411-48f0-857c-86e77720fccd

BugReportOnWeb avatar Jun 12 '25 14:06 BugReportOnWeb

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: BugReportOnWeb <[email protected]>
Co-authored-by: SirLouen <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: WunderBart <[email protected]>
Co-authored-by: jakub300 <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

github-actions[bot] avatar Jun 12 '25 14:06 github-actions[bot]

:wave: Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @BugReportOnWeb! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

github-actions[bot] avatar Jun 12 '25 14:06 github-actions[bot]

Test Report

Description

✅ This report validates that the indicated patch works as expected.

Patch tested: https://patch-diff.githubusercontent.com/raw/WordPress/gutenberg/pull/70400.diff

Environment

  • WordPress: 6.9-alpha-60093-src
  • PHP: 8.2.29
  • Server: Apache/2.4.62 (Debian)
  • Database: mysqli (Server: 11.8.2-MariaDB-ubu2404 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 138.0.0.0
  • OS: Windows 10/11
  • Theme: Twenty Twenty-Five 1.2
  • MU Plugins: None activated
  • Plugins:
    • Gutenberg 21.2.0-rc.1
    • JS Testing 1.0.0
    • Test Reports 1.2.0

Actual Results

  1. ✅ Issue resolved with patch.

Additional Notes

  • This is only a functional test. Code has not been reviewed on my side.

SirLouen avatar Jul 16 '25 14:07 SirLouen

Fixed via #77018. All contributors are attributed in the commit.

Mamaduka avatar Apr 06 '26 07:04 Mamaduka