summary refs log tree commit diff
path: root/.vim/lua/zk.lua
blob: 242badcdacfe9fccf32de7e6af42a45a6d5e2dfc (plain) (blame)
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
local function zk_new(path, title_dir)
  path = path or vim.loop.cwd()
  if not (path and #path > 0) then return end

  if not title_dir then
    vim.ui.input('New Zettel: ', function(result)
      title_dir = result
      end)
  end
  if title_dir == "" then return end

  local o = {}
  local parts = vim.split(title_dir, '/')
  if not parts[2] then
    o.title = vim.trim(parts[1])
  else
    o.title = vim.trim(parts[2])
    o.dir = vim.trim(parts[1])
  end

  vim.lsp.buf_request(0, 'workspace/executeCommand',
    {
      command = 'zk.new',
      arguments = { path, o },
    },
    function(_, _, result)
      vim.lsp.util.jump_to_location{
        uri = vim.uri_from_fname(result.path),
        range = {start={line=2, character=0}},
      }
      vim.cmd('startinsert')
    end
  )
end

local function zk_index(path)
  path = path or vim.loop.cwd()
  vim.lsp.buf.execute_command {
    command = 'zk.index', arguments = {path, nil},
  }
end

return {
  new = zk_new,
  index = zk_index,
}