feat: allow passing param depth to go_to_context() (#277)

* feat: allow passing param depth to go_to_context()

* doc: update README

* fix: remove usage of `vim.iter()` in `go_to_context()`
This commit is contained in:
Shihua Zeng 2023-12-23 18:16:06 +08:00 committed by GitHub
parent c9f2b429a1
commit 652ec514d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -242,7 +242,7 @@ hi TreesitterContextBottom gui=underline guisp=Grey
```lua
vim.keymap.set("n", "[c", function()
require("treesitter-context").go_to_context()
require("treesitter-context").go_to_context(vim.v.count1)
end, { silent = true })
```

View file

@ -212,15 +212,22 @@ function M.setup(options)
end
end
function M.go_to_context()
---@param depth integer? default 1
function M.go_to_context(depth)
depth = depth or 1
local line = 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[1] + 1 < line then
context = v
for idx = #contexts, 1, -1 do
local c = contexts[idx]
if depth == 0 then
break
end
if c[1] + 1 < line then
context = c
depth = depth - 1
end
end