0

can a neovim guru help create a treesitter query which matches '#%%' pattern in python files?

I use #%% inside my python files to quickly jump between code blocks. It also helps if i convert the .py file to .ipynb, in that it uses the ``#%%``` string to define cell boundaries in jupyter.

I will be grateful if someone can help create a tree-sitter text query which recognises this pattern and a keymapping to help me jump from one occurence of ``#%%``` to next (and backwards)

Thanks.

Maelstorm
  • 580
  • 2
  • 10
  • 29
  • Do you require Treesitter? Or, would you like a solution that makes use of the built-in forward and backward search? Using Treesitter's syntax trees seems excessive for this problem – Kyle F Hartzenberg Aug 24 '23 at 22:35

1 Answers1

0

This does the needful:

((comment) @cell.boundary
  (#eq? @cell.boundary "#%%"))

To get next/prev key-mappings you can install the nvim-treesitter-textobjects plugin and configure its move capability to map your desired keystrokes to this @cell.boundary capture.

That would look something like:

textobjects = {
  move = {
    enable = true,
    goto_next_start = {
      ["]c"] = "@cell.boundary",
    },
    goto_previous_start = {
      ["[c"] = "@cell.boundary",
    },
  },

in the nvim-treesitter configuration.

scanny
  • 26,423
  • 5
  • 54
  • 80