Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ui for user defined reports and filters #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"diagnostics": {
"disable": ["redefined-local"]
}
}
3 changes: 2 additions & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ fmt:
stylua lua/ --config-path=stylua.toml

lint:
luacheck lua/ --globals vim
# 4** is shadowing warnings on luacheck
luacheck lua/ --globals vim --ignore 4*

test:
nvim --headless --noplugin -u scripts/minimal_init.vim -c "PlenaryBustedDirectory lua/tests/ { minimal_init = './scripts/minimal_init.vim' }"
9 changes: 7 additions & 2 deletions lua/ledger/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ local M = {}
local LedgerCommands = {}
LedgerCommands.__index = LedgerCommands

--- @class LedgerCommands
local instance

function LedgerCommands:create_augroups()
self.augroup = vim.api.nvim_create_augroup("Ledger", {})
self.output_augroup = vim.api.nvim_create_augroup("LedgerOutput", {})
Expand Down Expand Up @@ -109,8 +112,10 @@ function LedgerCommands:setup_autocommands()
end

function M.setup()
local self = setmetatable({}, LedgerCommands)
return self
if not instance then
instance = setmetatable({}, LedgerCommands)
end
return instance
end

return M
55 changes: 54 additions & 1 deletion lua/ledger/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
local M = {}

--- @class ledger.Tui
--- @field enabled boolean
--- @field sections table<string, ledger.TuiSection>

--- @class ledger.TuiFilter
--- @field flag string
--- @field input boolean

--- @class ledger.TuiSection
--- @field command string
--- @field filters table<string, ledger.TuiFilter[]>

--- @class ledger.SnippetKeymaps
--- @field new_account string[]
--- @field new_posting string[]
Expand All @@ -11,9 +23,14 @@ local M = {}
--- @field key string
--- @field command string

--- @class ledger.TuiKeymaps
--- @field initialize string[]
--- @field shutdown string[]

--- @class ledger.Keymaps
--- @field snippets ledger.SnippetKeymaps
--- @field reports ledger.ReportKeymap[]
--- @field tui ledger.TuiKeymaps

--- @class ledger.PartialKeymaps
--- @field snippets? ledger.SnippetKeymaps
Expand Down Expand Up @@ -45,12 +62,14 @@ local M = {}
--- @field strict boolean

--- @class ledger.Config
--- @field plugin_name string
--- @field extensions string[]
--- @field default_ignored_paths string[]
--- @field completion ledger.Completion
--- @field snippets ledger.Snippet
--- @field keymaps ledger.Keymaps
--- @field diagnostics ledger.Diagnostics
--- @field tui ledger.Tui
local LedgerConfig = {}
LedgerConfig.__index = LedgerConfig

Expand Down Expand Up @@ -116,6 +135,7 @@ end
local function get_default_config()
--- @type ledger.Config
local default_config = {
plugin_name = "ledger.nvim",
extensions = {
"ledger",
"hledger",
Expand Down Expand Up @@ -144,6 +164,33 @@ local function get_default_config()
new_commodity = { "cm" },
},
reports = {},
tui = {
initialize = { "<leader>tui" },
shutdown = { "<leader>tud" },
},
},
tui = {
enabled = true,
sections = {
["Show Balance"] = {
command = "ledger --strict -f main.ledger bal",
filters = {
["Period"] = {
flag = "-p",
input = true,
},
},
},
["Show Budget"] = {
command = "ledger --strict -f main.ledger budget",
filters = {
["Another"] = {
flag = "-p",
input = true,
},
},
},
},
},
}

Expand All @@ -158,6 +205,13 @@ function LedgerConfig:set_keymaps()
commands:run_report(keymap.command)
end)
end

if not self.tui.enabled then
return
end

local tui = require("ledger.tui").get()
tui:set_keymaps()
end

--- Config is a singleton, allowing us to call `get` as many times as we
Expand All @@ -171,7 +225,6 @@ function M.setup(overrides)
local default = get_default_config()
local with_overrides = vim.tbl_deep_extend("force", default, overrides or {})
instance = setmetatable(with_overrides, LedgerConfig)
instance:set_keymaps()
end
return instance
end
Expand Down
6 changes: 6 additions & 0 deletions lua/ledger/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ function M.setup(overrides)
require("ledger.diagnostics").get_diagnostics()
end

if config.tui.enabled then
require("ledger.tui").setup()
end

config:set_keymaps()

--- @type ledger.Main
local default = {
context = context,
Expand Down
8 changes: 5 additions & 3 deletions lua/ledger/parser.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
local queries = require("ledger.queries")
local ts_utils = require("nvim-treesitter.ts_utils")

--- @alias TSQueryIter fun(end_line: integer|nil):integer, TSNode, vim.treesitter.query.TSMetadata, TSQueryMatch

--- @class ledger.parser
--- @field get_parser fun(source: string): TSSource
--- @field find_current_scope fun(): Scope
--- @field query_iter fun(query: vim.treesitter.Query, node: TSNode, source: string):
--- @field query_iter fun(query: vim.treesitter.Query, node: TSNode, source: string): TSQueryIter
--- @field get_account_names_from_source fun(node: TSNode, source: string, filename: string, ctx: ledger.Context)
--- @field get_commodities_from_source fun(node: TSNode, source: string, filename: string, ctx: ledger.Context)
local M = {}
Expand Down Expand Up @@ -142,13 +144,13 @@ function M.get_postings_from_source(node, source, filename, ctx)

if child_count > 0 then
--- @type TSNode
local account, amount = unpack(match:named_children(0))
local account, amount = unpack(match:named_children())
local account_text = vim.treesitter.get_node_text(account, source)
posting.account = { text = account_text, range = M.get_node_range(account) }

if amount then
--- @type TSNode, TSNode
local commodity, quantity = unpack(amount:named_children(0))
local commodity, quantity = unpack(amount:named_children())
local commodity_text = vim.treesitter.get_node_text(commodity, source)
posting.commodity = { text = commodity_text, range = M.get_node_range(commodity) }

Expand Down
69 changes: 69 additions & 0 deletions lua/ledger/tui/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
local layout = require("ledger.tui.layout")
local logger = require("ledger.logger").get()
local reports = require("ledger.tui.reports")

local M = {}

--- @class ledger.Tui
--- @field layout ledger.TuiLayout
--- @field reports ledger.TuiReports
--- @field running boolean
local LedgerTui = {}
LedgerTui.__index = LedgerTui

--- @class ledger.Tui
local instance

function LedgerTui:setup()
if self.running then
print("ledger.nvim already has the tui open")
return
end

logger:info("initializing tui view")
self.running = true
self.layout:setup_windows()
self.layout:setup_aucmds()
self.reports:populate_reports()
self.reports:populate_filters()
end

function LedgerTui:shutdown()
self.layout:restore_window_options()
print("shutting down")
end

function LedgerTui:set_keymaps()
local config = require("ledger.config").get()
local tui_keymaps = config.keymaps.tui

--- @param commands string[]
--- @param callback function
local set_keymap = function(commands, callback)
for _, trigger in pairs(commands) do
vim.keymap.set("n", trigger, function()
callback(self)
end)
end
end

set_keymap(tui_keymaps.initialize, self.setup)
set_keymap(tui_keymaps.shutdown, self.shutdown)
end

function M.setup()
if not instance then
local layout_instance = layout.setup()
instance = setmetatable({
layout = layout_instance,
reports = reports.setup(layout_instance),
}, LedgerTui)
end
return instance
end

function M.get()
return instance
end

return M
Loading
Loading