# How to navigate to end/start of current programming block with nvim-treesitter

**URL:** https://discourse.julialang.org/t/how-to-navigate-to-end-start-of-current-programming-block-with-nvim-treesitter/99740
**Category:** Tooling
**Tags:** neovim
**Created:** [June 1, 2023, 6:43pm UTC](https://discourse.julialang.org/t/how-to-navigate-to-end-start-of-current-programming-block-with-nvim-treesitter/99740 "2023-06-01T18:43:56Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)
#### Post date: [June 1, 2023, 6:43pm UTC](https://discourse.julialang.org/t/how-to-navigate-to-end-start-of-current-programming-block-with-nvim-treesitter/99740/1 "2023-06-01T18:43:56Z")

</div>

Have you found a way to go to the end/start of the current (where cursor is) most direct programming block (for-loop, if-else, let-block, struct, etc.) ?

This is something that in julia-vim is implemented and supported out-of-the-box by using `%`.

I believer it is possible with the current [julia tree-sitter implementation](https://github.com/tree-sitter/tree-sitter-julia), but it’s not out-of-the-box and it needs some magic.

Using [nvim-treesitter-textobjects](https://github.com/nvim-treesitter/nvim-treesitter-textobjects), one can navigate to the start/end of an if-statement (`@conditional.inner/outer`), for-loop `@loop.inner/outer`, funtion (`@function.inner/outer`), etc.  
However each time you need to specify the exact nature.

I believe there could be a way to define to go `inner/outer` of the most direct block that currently the cursor is located. However, I am a rookie here and I don’t know how to do it (probably tree-sitter queries are needed ?).

Do you know how this could be integrated in the nvim-treesitter configuration ?

It might also help solving this issue

> <https://github.com/julia-vscode/julia-vscode/issues/2987>
>
> discussed in https://julialang.zulipchat.com/#narrow/stream/225542-helpdesk/topi…c/vim.20.25.20in.20vscode
> 
> \[julia-vim\](https://github.com/JuliaEditorSupport/julia-vim) supports \`%\` to toggle the cursor between the start and end of a code block (e.g. \`function - end\`, \`if - else - end\`, \`for - end\`, etc.)
> 
> Could we get a command to do this in vscode?

_P.S. now that I think about it, this must be language-agnostic 😅_

---

<div class="post-metadata">

### Author: ![sbuercklin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbuercklin/32/15728_2.png) [@sbuercklin](https://discourse.julialang.org/u/sbuercklin)
#### Post date: [June 1, 2023, 11:11pm UTC](https://discourse.julialang.org/t/how-to-navigate-to-end-start-of-current-programming-block-with-nvim-treesitter/99740/2 "2023-06-01T23:11:17Z")

</div>

I’m new to treesitter as well, but I was playing around with a similar problem a couple days ago. A simple implementation in bare Lua without any extra plugins is pretty easy to do, although I imagine there are more elegant solutions if you have a deeper knowledge of treesitter.

You can get the current `node` using `vim.treesitter.get_node()` and recurse through its parents until you find a node type that you want to stop at. You can check the `node:type()` against a set of types you want to stop at by pre-generating this list for Julia, and once you find the outermost node you want to stop at there are utilities for getting the start/end/range of a node.

Actually using queries to do this would be much more robust, cleaner, and probably language-agnostic. But if you just want to get something working for Julia, the Lua to make it “just work” is not too bad.

Most of the functionality/docs you would need is in here: [Treesitter - Neovim docs](https://neovim.io/doc/user/treesitter.html#treesitter-node)

My (similar) work is here, although I was trying to select nodes with specific scoping rules: [dotfiles/julia-repl.lua at d2353e56fe39ef95d002e3a69e51912fb70db4bc · SBuercklin/dotfiles · GitHub](https://github.com/SBuercklin/dotfiles/blob/d2353e56fe39ef95d002e3a69e51912fb70db4bc/nvim/lua/sbuercklin/julia-repl.lua#L122-L147)

I expect to dump some more time into this over the weekend. If I get a better solution that’s using more proper treesitter functionality, I’ll bump the thread
