discourse/plugins
Régis Hanol 47d1703b67
FIX: code "block" detection before showing autocomplete (#26023)
**TL;DR:** Refactor autocomplete to use async markdown parsing for code block detection.

Previously, the `inCodeBlock` function in `discourse/app/lib/utilities.js` used regular expressions to determine if a given position in the text was inside a code block. This approach had some limitations and could lead to incorrect behavior in certain edge cases.

This commit refactors `inCodeBlock` to use a more robust algorithm that leverages Discourse's markdown parsing library.

The new approach works as follows:

1. Check if the text contains any code block markers using a regular expression.
   If not, return `false` since the cursor can't be in a code block.
1. If potential code blocks exist, find a unique marker character that doesn't appear in the text.
1. Insert the unique marker character into the text at the cursor position.
1. Parse the modified text using Discourse's markdown parser, which converts the markdown into a tree of tokens.
1. Traverse the token tree to find the token that contains the unique marker character.
1. Check if the token's type is one of the types representing code blocks ("code_inline", "code_block", or "fence").
   If so, return `true`, indicating that the cursor is inside a code block.
   Otherwise, return `false`.

This algorithm provides a more accurate way to determine the cursor's position in relation to code blocks, accounting for the various ways code blocks can be represented in markdown.

To accommodate this change, the autocomplete `triggerRule` option is now an async function.

The autocomplete logic in `composer-editor.js`, `d-editor.js`, and `hashtag-autocomplete.js` has been updated to handle the async nature of `inCodeBlock`.

Additionally, many of the tests have been refactored to handle async behavior. The test helpers now simulate typing and autocomplete selection in a more realistic, step-by-step manner. This should make the tests more robust and reflective of real-world usage.

This is a significant refactor that touches multiple parts of the codebase, but it should lead to more accurate and reliable autocomplete behavior, especially when dealing with code blocks in the editor.

> Written by an 🤖 LLM. Edited by a 🧑‍💻 human.
2024-03-11 17:35:50 +01:00
..
chat FIX: code "block" detection before showing autocomplete (#26023) 2024-03-11 17:35:50 +01:00
checklist DEV: Use service: lookups instead of :main (#25402) 2024-01-24 15:59:44 +01:00
discourse-details Update translations (#25659) 2024-02-13 16:11:30 +01:00
discourse-lazy-videos DEV: Use service: lookups instead of :main (#25402) 2024-01-24 15:59:44 +01:00
discourse-local-dates DEV: Update moment and moment-timezone (#26089) 2024-03-11 03:04:51 +01:00
discourse-narrative-bot FIX: Update Discobot's UserProfile#bio_raw when default locale changes (#26045) 2024-03-06 11:42:20 +08:00
discourse-presence DEV: Use the "new" service import (#26059) 2024-03-06 18:05:11 +01:00
footnote DEV: Use service: lookups instead of :main (#25402) 2024-01-24 15:59:44 +01:00
poll DEV: Use the "new" service import (#26059) 2024-03-06 18:05:11 +01:00
spoiler-alert Update translations (#26030) 2024-03-05 16:47:46 +01:00
styleguide DEV: Use the "new" service import (#26059) 2024-03-06 18:05:11 +01:00