Skip to content

Commit

Permalink
Allow to inline new notes in the current buffer (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu authored Dec 19, 2022
1 parent 29c278c commit 2fd9f9f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,29 @@ Try out different [commands](#built-in-commands) such as `:ZkNotes` or `:ZkNew`,

```vim
" Creates and edits a new note
"
" Use the `inline = true` option to insert the content of the created note at the caret position, instead of writing the note on the file system.
"
" params
" (optional) additional options, see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zknew
:ZkNew [{options}]
```

```vim
" Creates a new note and uses the last visual selection as the title while replacing the selection with a link to the new note
"
" Use the `inline = true` option to replace the selection with the content of the created note, instead of writing the note on the file system.
"
" params
" (optional) additional options, see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zknew
:'<,'>ZkNewFromTitleSelection [{options}]
```

```vim
" Creates a new note and uses the last visual selection as the content while replacing the selection with a link to the new note
"
" Use the `inline = true` option to replace the selection with the content of the created note, instead of writing the note on the file system.
"
" params
" (optional) additional options, see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zknew
:'<,'>ZkNewFromContentSelection [{options}]
Expand Down
7 changes: 3 additions & 4 deletions lua/zk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ function M.new(options)
options = options or {}
api.new(options.notebook_path, options, function(err, res)
assert(not err, tostring(err))
if options and options.edit == false then
return
if options and options.dryRun ~= true and options.edit ~= false then
-- neovim does not yet support window/showDocument, therefore we handle options.edit locally
vim.cmd("edit " .. res.path)
end
-- neovim does not yet support window/showDocument, therefore we handle options.edit locally
vim.cmd("edit " .. res.path)
end)
end

Expand Down
40 changes: 37 additions & 3 deletions lua/zk/commands/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,54 @@ local commands = require("zk.commands")

commands.add("ZkIndex", zk.index)

commands.add("ZkNew", zk.new)
commands.add("ZkNew", function(options)
options = options or {}

if options.inline == true then
options.inline = nil
options.dryRun = true
options.insertContentAtLocation = util.get_lsp_location_from_caret()
end

zk.new(options)
end)

commands.add("ZkNewFromTitleSelection", function(options)
local location = util.get_lsp_location_from_selection()
local selected_text = util.get_text_in_range(location.range)
assert(selected_text ~= nil, "No selected text")
zk.new(vim.tbl_extend("force", { insertLinkAtLocation = location, title = selected_text }, options or {}))

options = options or {}
options.title = selected_text

if options.inline == true then
options.inline = nil
options.dryRun = true
options.insertContentAtLocation = location
else
options.insertLinkAtLocation = location
end

zk.new(options)
end, { needs_selection = true })

commands.add("ZkNewFromContentSelection", function(options)
local location = util.get_lsp_location_from_selection()
local selected_text = util.get_text_in_range(location.range)
assert(selected_text ~= nil, "No selected text")
zk.new(vim.tbl_extend("force", { insertLinkAtLocation = location, content = selected_text }, options or {}))

options = options or {}
options.content = selected_text

if options.inline == true then
options.inline = nil
options.dryRun = true
options.insertContentAtLocation = location
else
options.insertLinkAtLocation = location
end

zk.new(options)
end, { needs_selection = true })

commands.add("ZkCd", zk.cd)
Expand Down
26 changes: 22 additions & 4 deletions lua/zk/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,28 @@ end
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#location
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
return {
uri = params.textDocument.uri,
range = M.get_selected_range() -- workaround for neovim 0.6.1 bug (https://github.com/mickael-menu/zk-nvim/issues/19)
}
end

---Makes an LSP location object from the caret position in the current buffer.
--
---@return table LSP location object
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#location
function M.get_lsp_location_from_caret()
local params = vim.lsp.util.make_given_range_params()

local row,col = unpack(vim.api.nvim_win_get_cursor(0))
local position = { line = row, character = col }
return {
uri = params.textDocument.uri,
range = {
start = position,
["end"] = position
}
}
end

---Gets the text in the given range of the current buffer.
Expand Down

0 comments on commit 2fd9f9f

Please sign in to comment.