init.lua/lua/scratch/ledger.lua(view raw)
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 |
local t = require("blink.cmp.types").CompletionItemKind
---@module 'blink.cmp'
---@class blink.cmp.Source
local source = {}
function source.new(opts)
local self = setmetatable({}, { __index = source })
self.opts = opts
self.items = {}
return self
end
function source:enabled()
return vim.bo.filetype == "ledger"
end
function source:get_trigger_characters()
return { " ", ":", "as", "eq", "li", "in", "ex" }
end
function source:get_completions(_, callback)
local rs = vim
.system({ "hledger", "accounts", "--flat" }, { text = true })
:wait()
assert(rs.code == 0, "Failed to run hledger accounts: " .. rs.stderr)
---@type lsp.CompletionItem[]
local items = vim
.iter(vim.split(rs.stdout, "\n"))
:map(function(acc)
---@type lsp.CompletionItem
return {
label = acc,
kind = t.Property,
insertTextFormat = vim.lsp.protocol.InsertTextFormat.PlainText,
}
end)
:totable()
callback {
items = items,
is_incomplete_backward = false,
is_incomplete_forward = false,
}
return function() end
end
return source
|