pull/21/head
Karim Abou Zeid 2 years ago committed by GitHub
parent 17d2880421
commit 9ddedad880
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -37,7 +37,7 @@ commands.add("ZkLinks", function(options)
end)
commands.add("ZkMatch", function(options)
local selected_text = util.get_selected_text()
local selected_text = util.get_text_in_range(util.get_selected_range())
assert(selected_text ~= nil, "No selected text")
options = vim.tbl_extend("force", { match = selected_text }, options or {})
zk.edit(options, { title = "Zk Notes matching " .. vim.inspect(selected_text) })

@ -47,13 +47,14 @@ function M.get_lsp_location_from_selection()
local params = vim.lsp.util.make_given_range_params()
params.uri = params.textDocument.uri
params.textDocument = nil
params.range = M.get_selected_range() -- workaround for neovim 0.6.1 bug (https://github.com/mickael-menu/zk-nvim/issues/19)
return params
end
---Gets the text in the given range of the current buffer.
---Needed until https://github.com/neovim/neovim/pull/13896 is merged.
--
---@param range table contains {start} and {end} tables with {line} and {character} values
---@param range table contains {start} and {end} tables with {line} (0-indexed, end inclusive) and {character} (0-indexed, end exclusive) values
---@return string? text in range
function M.get_text_in_range(range)
local A = range["start"]
@ -68,27 +69,28 @@ function M.get_text_in_range(range)
return table.concat(lines, "\n")
end
---Gets the most recently selected text of the current buffer.
---Gets the most recently selected range of the current buffer.
---That is the text between the '<,'> marks.
---Note that these marks are only updated *after* leaving the visual mode.
--
---@return string? selected text
function M.get_selected_text()
---@return table selected range, contains {start} and {end} tables with {line} (0-indexed, end inclusive) and {character} (0-indexed, end exclusive) values
function M.get_selected_range()
-- code adjusted from `vim.lsp.util.make_given_range_params`
-- we don't want to use character encoding offsets here
local A = vim.api.nvim_buf_get_mark(0, "<")
local B = vim.api.nvim_buf_get_mark(0, ">")
-- convert to 0-index
A[1] = A[1] - 1
B[1] = B[1] - 1
if vim.o.selection ~= "exclusive" then
B[2] = B[2] + 1
end
return M.get_text_in_range({
return {
start = { line = A[1], character = A[2] },
["end"] = { line = B[1], character = B[2] },
})
}
end
return M

Loading…
Cancel
Save