2022-01-01 23:13:36 +00:00
|
|
|
local lsp = require("zk.lsp")
|
2021-12-20 22:06:14 +00:00
|
|
|
local config = require("zk.config")
|
2022-01-01 23:13:36 +00:00
|
|
|
local ui = require("zk.ui")
|
|
|
|
local api = require("zk.api")
|
|
|
|
local util = require("zk.util")
|
2021-12-20 22:06:14 +00:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2022-01-01 23:13:36 +00:00
|
|
|
local function setup_lsp_auto_attach()
|
|
|
|
--- NOTE: modified version of code in nvim-lspconfig
|
|
|
|
local trigger
|
|
|
|
local filetypes = config.options.lsp.auto_attach.filetypes
|
|
|
|
if filetypes then
|
|
|
|
trigger = "FileType " .. table.concat(filetypes, ",")
|
|
|
|
else
|
|
|
|
trigger = "BufReadPost *"
|
|
|
|
end
|
|
|
|
vim.api.nvim_command(string.format("autocmd %s lua require'zk'._lsp_buf_auto_add(0)", trigger))
|
|
|
|
end
|
2021-12-20 22:06:14 +00:00
|
|
|
|
2022-01-01 23:13:36 +00:00
|
|
|
---Automatically called via an |autocmd| if lsp.auto_attach is enabled.
|
|
|
|
--
|
|
|
|
---@param bufnr number
|
|
|
|
function M._lsp_buf_auto_add(bufnr)
|
|
|
|
if vim.api.nvim_buf_get_option(bufnr, "buftype") == "nofile" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if not util.notebook_root(vim.api.nvim_buf_get_name(bufnr)) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
lsp.buf_add(bufnr)
|
|
|
|
end
|
2021-12-20 22:06:14 +00:00
|
|
|
|
2021-12-22 19:04:44 +00:00
|
|
|
---The entry point of the plugin
|
2022-01-01 23:13:36 +00:00
|
|
|
--
|
2021-12-22 19:04:44 +00:00
|
|
|
---@param options? table user configuration options
|
2021-12-20 22:06:14 +00:00
|
|
|
function M.setup(options)
|
|
|
|
config.options = vim.tbl_deep_extend("force", config.defaults, options or {})
|
2021-12-20 22:19:51 +00:00
|
|
|
|
2021-12-20 22:06:14 +00:00
|
|
|
if config.options.lsp.auto_attach.enabled then
|
2022-01-01 23:13:36 +00:00
|
|
|
setup_lsp_auto_attach()
|
2021-12-20 22:06:14 +00:00
|
|
|
end
|
2021-12-20 22:19:51 +00:00
|
|
|
|
2022-01-01 23:13:36 +00:00
|
|
|
require("zk.commands.builtin")
|
2021-12-20 22:06:14 +00:00
|
|
|
end
|
|
|
|
|
2022-01-01 23:13:36 +00:00
|
|
|
---Cd into the notebook root
|
2021-12-22 19:04:44 +00:00
|
|
|
--
|
2022-01-01 23:13:36 +00:00
|
|
|
---@param options? table
|
|
|
|
function M.cd(options)
|
|
|
|
options = options or {}
|
|
|
|
local notebook_path = options.notebook_path or util.resolve_notebook_path(0)
|
|
|
|
local root = util.notebook_root(notebook_path)
|
|
|
|
if root then
|
|
|
|
vim.cmd("cd " .. root)
|
|
|
|
end
|
2021-12-20 22:06:14 +00:00
|
|
|
end
|
|
|
|
|
2022-01-01 23:13:36 +00:00
|
|
|
---Creates and edits a new note
|
2021-12-22 19:04:44 +00:00
|
|
|
--
|
2022-01-01 23:13:36 +00:00
|
|
|
---@param options? table additional options
|
2021-12-22 19:04:44 +00:00
|
|
|
---@see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zknew
|
2022-01-01 23:13:36 +00:00
|
|
|
function M.new(options)
|
|
|
|
options = options or {}
|
2022-01-29 22:20:23 +00:00
|
|
|
api.new(options.notebook_path, options, function(err, res)
|
|
|
|
assert(not err, tostring(err))
|
2021-12-23 19:22:44 +00:00
|
|
|
if options and options.edit == false then
|
|
|
|
return
|
|
|
|
end
|
2022-01-01 23:13:36 +00:00
|
|
|
-- neovim does not yet support window/showDocument, therefore we handle options.edit locally
|
2021-12-20 22:06:14 +00:00
|
|
|
vim.cmd("edit " .. res.path)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-01-01 23:13:36 +00:00
|
|
|
---Indexes the notebook
|
2021-12-22 19:04:44 +00:00
|
|
|
--
|
2022-01-01 23:13:36 +00:00
|
|
|
---@param options? table additional options
|
|
|
|
---@see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zkindex
|
|
|
|
function M.index(options)
|
|
|
|
options = options or {}
|
2022-01-29 22:20:23 +00:00
|
|
|
api.index(options.notebook_path, options, function(err, stats)
|
|
|
|
assert(not err, tostring(err))
|
2022-01-01 23:13:36 +00:00
|
|
|
vim.notify(vim.inspect(stats))
|
|
|
|
end)
|
2021-12-22 19:04:44 +00:00
|
|
|
end
|
|
|
|
|
2022-01-01 23:13:36 +00:00
|
|
|
---Opens a notes picker, and calls the callback with the selection
|
|
|
|
--
|
|
|
|
---@param options? table additional options
|
|
|
|
---@param picker_options? table options for the picker
|
|
|
|
---@param cb function
|
|
|
|
---@see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zklist
|
|
|
|
---@see zk.ui.pick_notes
|
|
|
|
function M.pick_notes(options, picker_options, cb)
|
2022-01-29 18:35:51 +00:00
|
|
|
options = vim.tbl_extend("force", { select = ui.get_pick_notes_list_api_selection(picker_options) }, options or {})
|
2022-01-29 22:20:23 +00:00
|
|
|
api.list(options.notebook_path, options, function(err, notes)
|
|
|
|
assert(not err, tostring(err))
|
2022-01-01 23:13:36 +00:00
|
|
|
ui.pick_notes(notes, picker_options, cb)
|
|
|
|
end)
|
|
|
|
end
|
2021-12-22 19:04:44 +00:00
|
|
|
|
2022-01-01 23:13:36 +00:00
|
|
|
---Opens a tags picker, and calls the callback with the selection
|
2021-12-22 19:04:44 +00:00
|
|
|
--
|
2022-01-01 23:13:36 +00:00
|
|
|
---@param options? table additional options
|
|
|
|
---@param picker_options? table options for the picker
|
|
|
|
---@param cb function
|
2021-12-22 19:04:44 +00:00
|
|
|
---@see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zktaglist
|
2022-01-01 23:13:36 +00:00
|
|
|
---@see zk.ui.pick_tags
|
|
|
|
function M.pick_tags(options, picker_options, cb)
|
2022-01-29 18:35:51 +00:00
|
|
|
options = options or {}
|
2022-01-29 22:20:23 +00:00
|
|
|
api.tag.list(options.notebook_path, options, function(err, tags)
|
|
|
|
assert(not err, tostring(err))
|
2022-01-01 23:13:36 +00:00
|
|
|
ui.pick_tags(tags, picker_options, cb)
|
|
|
|
end)
|
2021-12-22 19:04:44 +00:00
|
|
|
end
|
|
|
|
|
2022-01-01 23:13:36 +00:00
|
|
|
---Opens a notes picker, and edits the selected notes
|
2021-12-23 19:22:44 +00:00
|
|
|
--
|
2022-01-01 23:13:36 +00:00
|
|
|
---@param options? table additional options
|
|
|
|
---@param picker_options? table options for the picker
|
|
|
|
---@see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zklist
|
|
|
|
---@see zk.ui.pick_notes
|
|
|
|
function M.edit(options, picker_options)
|
|
|
|
M.pick_notes(options, picker_options, function(notes)
|
|
|
|
if picker_options.multi_select == false then
|
|
|
|
notes = { notes }
|
|
|
|
end
|
|
|
|
for _, note in ipairs(notes) do
|
|
|
|
vim.cmd("e " .. note.absPath)
|
|
|
|
end
|
|
|
|
end)
|
2021-12-23 19:22:44 +00:00
|
|
|
end
|
|
|
|
|
2021-12-20 22:06:14 +00:00
|
|
|
return M
|