-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathzk.lua
139 lines (124 loc) · 4.13 KB
/
zk.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
local lsp = require("zk.lsp")
local config = require("zk.config")
local ui = require("zk.ui")
local api = require("zk.api")
local util = require("zk.util")
local M = {}
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
---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
---The entry point of the plugin
--
---@param options? table user configuration options
function M.setup(options)
config.options = vim.tbl_deep_extend("force", config.defaults, options or {})
if config.options.lsp.auto_attach.enabled then
setup_lsp_auto_attach()
end
require("zk.commands.builtin")
end
---Cd into the notebook root
--
---@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
end
---Creates and edits a new note
--
---@param options? table additional options
---@see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zknew
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.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
end)
end
---Indexes the notebook
--
---@param options? table additional options
---@param cb? function for processing stats
---@see https://github.com/mickael-menu/zk/blob/main/docs/editors-integration.md#zkindex
function M.index(options, cb)
options = options or {}
cb = cb or function(stats)
vim.notify(vim.inspect(stats))
end
api.index(options.notebook_path, options, function(err, stats)
assert(not err, tostring(err))
cb(stats)
end)
end
---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)
options = vim.tbl_extend("force", { select = ui.get_pick_notes_list_api_selection(picker_options) }, options or {})
api.list(options.notebook_path, options, function(err, notes)
assert(not err, tostring(err))
ui.pick_notes(notes, picker_options, cb)
end)
end
---Opens a tags 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#zktaglist
---@see zk.ui.pick_tags
function M.pick_tags(options, picker_options, cb)
options = options or {}
api.tag.list(options.notebook_path, options, function(err, tags)
assert(not err, tostring(err))
ui.pick_tags(tags, picker_options, cb)
end)
end
---Opens a notes picker, and edits the selected notes
--
---@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 and picker_options.multi_select == false then
notes = { notes }
end
for _, note in ipairs(notes) do
vim.cmd("e " .. note.absPath)
end
end)
end
return M