conform.nvim icon indicating copy to clipboard operation
conform.nvim copied to clipboard

feature request: ignore patterns to send to formatter

Open kanghengliu opened this issue 11 months ago • 1 comments

Did you check existing requests?

  • [x] I have searched the existing issues

Describe the feature

For injected formatter, add a lua match pattern variable to match and filter out some input_lines, as they break formatter.

Quick and dirty proof of concept: https://github.com/kanghengliu/conform.nvim/commit/30568d6e160463752f6b96234396238bd2ca4204

~~This commit hard codes the matching patterns. Also it does not paste back the matched lines after formatting.~~

update: added a filter for python only and allow matching for multiple lines. https://github.com/kanghengliu/conform.nvim/commit/ca99f295a0e1316dd747d5344782f1af224284ab

Provide background

When editing a markdown file, ipython magic commands and shell commands will prevent python formatters from functioning. Example1:

%%time
print("hello")

Example2:

!pip install randomstuff

Black will break, since this is not a valid python chunk, however, modern linters will highlight the chunk just fine. Both chunks are valid in jupyter notebook with IPython.

What is the significance of this feature?

strongly desired

Additional details

I've attempted to instruct the formatters to ignore certain patterns, however it is reasonably out of scope for code formatters to handle "broken" formats. Many formatters that i've found will not support this, and it makes sense for them to do so.

For additional use cases, see my issue at molten.nvim where I attempt to lint and format a chunk with cell magic.

kanghengliu avatar Mar 03 '25 15:03 kanghengliu

I would review a PR for this. I think I would prefer a more general approach, as expressing all of the possible changes someone might want to make as some key-value pattern-match DSL will be a challenge. We're already doing some modification of the text chunks; we could also pass them to a user-provided function for additional modification. This would likely happen around remove_surrounding and restore_surrounding

stevearc avatar Mar 05 '25 06:03 stevearc