fix(code blocks): prevent list indent and list utils from firing inside code blocks#193
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes issue #192 where list manipulation handlers (indent, outdent, enter, etc.) were incorrectly triggering inside fenced code blocks, causing unwanted modifications to code that resembles markdown list syntax (e.g., Rust method chaining with leading dots/dashes).
Key Changes:
- Added
is_in_code_block()detection utility with treesitter support and regex fallback - Wrapped all interactive list handlers with
skip_in_codeblock()to prevent execution inside code blocks - Added comprehensive test coverage for both the detection logic and handler wrapping
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
lua/markdown-plus/utils.lua |
Added is_in_code_block() utility function with treesitter-first approach and regex fallback for detecting fenced code blocks |
lua/markdown-plus/format/treesitter.lua |
Added is_in_fenced_code_block() function using treesitter node traversal to detect fenced_code_block nodes |
lua/markdown-plus/list/handlers.lua |
Added skip_in_codeblock() wrapper function that checks for code blocks and falls back to default key behavior when inside them |
lua/markdown-plus/list/init.lua |
Wrapped all interactive list handlers (Enter, Tab, Backspace, o/O) with skip_in_codeblock() protection |
spec/markdown-plus/utils_spec.lua |
Added 9 comprehensive tests covering various code block scenarios (backtick/tilde, indented, multiple, unclosed, etc.) |
spec/markdown-plus/list_spec.lua |
Added 4 tests verifying that the wrapper correctly skips handlers inside code blocks and executes them outside |
doc/markdown-plus.txt |
Updated "Context-aware keymaps" section to document that list operations are now code-block aware |
|
Hi 😁 Let me know if you'd like to see any changes, or for this PR to be less invasive P.S: I also noticed that there is some mixed logic, where some uses treesitter, and some good-old string.match (such as footnote parser which checks for codeblocks). Would you appreciate a followup that makes those use treesitter (when available) as well? |
The PR looks good and fixes the bug really nice, thank you for working on this 😄 The treesitter approach was recently introduced in the But since you mentioned it, I would really love it if you can open an issue for tracking first, and if you want you can give it a shot sometime with a PR. |
There was a problem hiding this comment.
🪨 solid.
@ten3roberts Please have the honor of merging this PR.
|
@all-contributors please add @ten3roberts for code |
|
I've updated the pull request to add @ten3roberts! 🎉 |
|
@all-contributors please add @ten3roberts for code and bug |
|
I've put up a pull request to add @ten3roberts! 🎉 |
|
Thanks Had to fix merge conflicts from the smart-paste PR The It is another issue, but fixable in the same way by using feedkeys to let nvim handle tab as normal |
|
Sorry for the merge conflicts, I had to fix the git commit history but thanks for taking care of it. |
|
@ten3roberts Because of the change I made to the commit history, the squash and merge for this PR shows an extended description with co-authors, if you don't mind removing those when you merge the PR. Thanks for your contribution. ❤️ |
b17a44e to
2068a81
Compare
|
@YousefHadder the commit history should now be clean. Don't have the merge button on my side, so feel free to merge yourself once PR succeeds |
Description
Fixes a bug where e.g; list indent handlers were invoked inside codeblocks, which could mess up Rust codeblocks using function chaining (one method wrapper per line), as well as other languages
Type of Change
Related Issues
Fixes #192
Testing
Screencast.From.2026-01-02.16-51-20.mp4
Checklist