Skip to content

Commit

Permalink
feat: add context jump function
Browse files Browse the repository at this point in the history
* feat: add context jump function

* style: naming and nvim_win_set_cursor

* fix: track contexts per-buffer
  • Loading branch information
alisnic authored May 11, 2023
1 parent ebb10f9 commit 1cce4b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ However, you can use this to create a border by applying an underline highlight,
hi TreesitterContextBottom gui=underline guisp=Grey
```

## Jumping to context (upwards)

```lua
vim.keymap.set("n", "[c", function()
require("treesitter-context").go_to_context()
end, { silent = true })
```

## Adding support for other languages

See [CONTRIBUTING.md](CONTRIBUTING.md)
Expand Down
24 changes: 24 additions & 0 deletions lua/treesitter-context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ local ns = api.nvim_create_namespace('nvim-treesitter-context')
--- @type TSNode[]?
local previous_nodes

--- @type table<integer, Context[]>
local all_contexts = {}

--- @return TSNode
local function get_root_node()
---@diagnostic disable-next-line
Expand Down Expand Up @@ -666,6 +669,8 @@ local function open(ctx_ranges)
table.insert(lno_highlights, hl)
end

all_contexts[bufnr] = contexts

set_lines(gbufnr, lno_text)
highlight_lno_str(gbufnr, lno_text, lno_highlights)

Expand Down Expand Up @@ -820,6 +825,25 @@ function M.setup(options)
end
end

function M.go_to_context()
local line = vim.api.nvim_win_get_cursor(0)[1]
local context = nil
local bufnr = api.nvim_get_current_buf()
local contexts = all_contexts[bufnr] or {}

for _, v in ipairs(contexts) do
if v.range[1] + 1 < line then
context = v
end
end

if context == nil then
return
end

vim.api.nvim_win_set_cursor(0, { context.range[1] + 1, context.range[2] })
end

command('TSContextEnable' , M.enable , {})
command('TSContextDisable', M.disable, {})
command('TSContextToggle' , M.toggle , {})
Expand Down

0 comments on commit 1cce4b7

Please sign in to comment.