Skip to content

feat: ZkBuffers #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ see what they can do, and learn as you go.
:ZkNotes [{options}]
```

```vim
" Opens a notes picker for active buffers (showing notebook files only).
" params
" (optional) additional options, see https://github.com/zk-org/zk/blob/main/docs/tips/editors-integration.md#zklist
:ZkBuffers [{options}]
```

```vim
" Opens a notes picker for the backlinks of the current buffer
" params
Expand Down
6 changes: 6 additions & 0 deletions lua/zk/commands/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ commands.add("ZkNotes", function(options)
zk.edit(options, { title = "Zk Notes" })
end)

commands.add("ZkBuffers", function(options)
local hrefs = util.get_buffer_paths()
options = vim.tbl_extend("force", { hrefs = hrefs }, options or {})
zk.edit(options, { title = "Zk Buffers" })
end)

commands.add("ZkBacklinks", function(options)
options = vim.tbl_extend("force", { linkTo = { vim.api.nvim_buf_get_name(0) } }, options or {})
zk.edit(options, { title = "Zk Backlinks" })
Expand Down
22 changes: 20 additions & 2 deletions lua/zk/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function M.get_lsp_location_from_selection()
local params = vim.lsp.util.make_given_range_params()
return {
uri = params.textDocument.uri,
range = params.range
range = params.range,
}
end

Expand Down Expand Up @@ -91,7 +91,7 @@ function M.get_lsp_location_from_caret()
})
end

---Gets the text in the last visual selection
---Gets the text in the last visual selection.
--
---@return string text in range
function M.get_selected_text()
Expand All @@ -107,4 +107,22 @@ function M.get_selected_text()
return table.concat(chunks, "\n")
end

---Gets the file paths of active buffers.
--
---@return table Paths of currently active buffers.
function M.get_buffer_paths()
local buffers = vim.api.nvim_list_bufs()
local paths = {}

for _, buf in ipairs(buffers) do
local path = vim.api.nvim_buf_get_name(buf)

if path ~= "" then
table.insert(paths, path)
end
end

return paths
end

return M