repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

refactor(lua): adopt direct-return module pattern, update AGENTS.md

f2fd4b1600bed9ca4cbc6b18a6894f6c2bd7548e

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Convert 12 modules from M-style (local M = {} / function M.xxx() / return M) to direct-return pattern:
- Define functions as local, collect fields and return table at bottom
- Avoid intermediate M variable as container
- AGENTS.md updated to document module return conventions
- All callsites unchanged (require returns the same value)

Changed files (12)

neovim/AGENTS.md

Modified
188 188 ### Lua Conventions
190 190 - **No comments** unless explicitly requested by user
191 -- **Module pattern**: `return setmetatable({...}, {__index = ...})` or
192 - `return {...}`
191 +- **Module return**: Return the value directly — no `local M = {}` intermediate
192 + variable. Collect fields at the bottom and return the table.
193 +- **Module patterns**:
194 + - Table: `return { foo = foo, bar = bar }`
195 + - setmetatable callable: `return setmetatable({...}, {__call = func})`
196 + - Function: `return function(...) end`
197 + - Side-effect (plugin setup): no return needed
198 +- **Avoid**: `local M = {}` / `function M.xxx()` / `return M` — use
199 + `local function xxx()` and return the table at the end
193 200 - **Global table shadowing**: Modules do `local table = require 'std.table'`
194 201 (not `table = require...`)
195 202 - **Input validation**: Use `vim.validate` at function entry points

neovim/lua/bugabinga/cmdline/highlight.lua

Modified
1 -local M = {}
3 1 local function resolve_lang(lang)
4 2 if lang ~= 'bash' then
5 3 return lang
11 9 return 'bash'
14 -function M.apply(buf, ns, text, lang, row, col_offset)
12 +local function apply(buf, ns, text, lang, row, col_offset)
15 13 lang = resolve_lang(lang)
16 14 if not lang or lang == '' then
17 15 return
51 -return M
49 +return {
50 + apply = apply,

neovim/lua/bugabinga/cmdline/init.lua

Modified
39 39 return false
42 -local M = {}
44 -function M.setup()
42 +local function setup()
45 43 if attached then
46 44 return
51 49 attached = true
54 -function M.disable()
52 +local function disable()
55 53 if not attached then
56 54 return
61 59 popupmenu.on_hide()
64 -M.setup()
62 +setup()
66 -return M
64 +return {
65 + setup = setup,
66 + disable = disable,

neovim/lua/bugabinga/cmdline/popupmenu.lua

Modified
112 112 return state.window
115 -local M = {}
117 115 local function apply_scrollbar(buf, height)
118 116 local total = #state.items
119 117 if total <= height then
180 -function M.on_show(items, selected, row, col, grid, prefix)
178 +local function on_show(items, selected, row, col, grid, prefix)
181 179 if not items or #items == 0 then
182 - M.on_hide()
180 + on_hide()
183 181 return
185 183 state.items = items
227 -function M.on_select(selected)
225 +local function on_select(selected)
228 226 if not vim.api.nvim_win_is_valid(state.window) then
229 227 return
275 -function M.on_hide()
273 +local function on_hide()
276 274 if vim.api.nvim_win_is_valid(state.window) then
277 275 vim.api.nvim_win_hide(state.window)
289 287 state.top_idx = 0
292 -return M
290 +return {
291 + on_show = on_show,
292 + on_select = on_select,
293 + on_hide = on_hide,

neovim/lua/bugabinga/cmdline/position.lua

Modified
1 -local M = {
2 - _anchor = nil,
3 -}
1 +local _anchor = nil
5 -function M.at_cursor(cmdline_width)
3 +local function at_cursor(cmdline_width)
6 4 local row = math.max(0, vim.fn.screenrow() - 2)
7 5 local col = 0
8 6 local screen_width = vim.o.columns
13 11 return { row = row, col = col }
16 -function M.get_cmdline_anchor()
17 - return M._anchor
14 +local function get_cmdline_anchor()
15 + return _anchor
20 -function M.set_cmdline_anchor(anchor)
21 - M._anchor = anchor
18 +local function set_cmdline_anchor(anchor)
19 + _anchor = anchor
24 -return M
22 +return {
23 + at_cursor = at_cursor,
24 + get_cmdline_anchor = get_cmdline_anchor,
25 + set_cmdline_anchor = set_cmdline_anchor,

neovim/lua/bugabinga/cmdline/render.lua

Modified
1 -local dbg_file = io.open('/tmp/cmdline_debug.log', 'a')
2 -local function dbg(...)
3 - local args = {...}
4 - local parts = {}
5 - for i, v in ipairs(args) do
6 - parts[i] = vim.inspect(v)
7 - end
8 - local line = os.date '%H:%M:%S' .. ' ' .. table.concat(parts, ' ')
9 - dbg_file:write(line .. '\n')
10 - dbg_file:flush()
11 -end
13 1 local format = require 'bugabinga.cmdline.format'
14 2 local position = require 'bugabinga.cmdline.position'
15 3 local cursor = require 'bugabinga.cmdline.cursor'
20 8 local prompt_ns = vim.api.nvim_create_namespace 'bugabinga.cmdline.prompt'
22 10 local state = {
23 - buffer = -1,
24 - window = -1,
25 - current_fmt = nil,
26 - text = '',
27 - firstc = '',
28 - prompt = '',
11 + buffer = -1,
12 + window = -1,
13 + current_fmt = nil,
14 + text = '',
15 + firstc = '',
16 + prompt = '',
31 19 local function get_buf()
32 - if vim.api.nvim_buf_is_valid(state.buffer) then
33 - return state.buffer
34 - end
35 - state.buffer = vim.api.nvim_create_buf(false, true)
36 - vim.bo[state.buffer].buflisted = false
37 - vim.bo[state.buffer].filetype = 'bugabinga_cmdline'
38 - return state.buffer
20 + if vim.api.nvim_buf_is_valid( state.buffer ) then
21 + return state.buffer
22 + end
23 + state.buffer = vim.api.nvim_create_buf( false, true )
24 + vim.bo[state.buffer].buflisted = false
25 + vim.bo[state.buffer].filetype = 'bugabinga_cmdline'
26 + return state.buffer
41 -local function get_win(buf, width, fmt)
42 - if vim.api.nvim_win_is_valid(state.window) then
43 - return state.window
44 - end
45 - local screen_pos = position.at_cursor(width or vim.o.columns)
46 - state.window = vim.api.nvim_open_win(buf, false, {
47 - relative = 'editor',
48 - width = width or vim.o.columns,
49 - height = 1,
50 - row = screen_pos.row,
51 - col = 0,
52 - style = 'minimal',
53 - border = vim.g.border_style,
54 - focusable = false,
55 - zindex = 50,
56 - })
57 - vim.wo[state.window].cursorline = false
58 - vim.wo[state.window].winbar = ''
59 - vim.wo[state.window].foldenable = false
60 - vim.wo[state.window].winhighlight = format.winhighlight(fmt.type, 'NormalFloat')
61 - return state.window
29 +local function get_win( buf, width, fmt )
30 + if vim.api.nvim_win_is_valid( state.window ) then
31 + return state.window
32 + end
33 + local screen_pos = position.at_cursor( width or vim.o.columns )
34 + state.window = vim.api.nvim_open_win( buf, false, {
35 + relative = 'editor',
36 + width = width or vim.o.columns,
37 + height = 1,
38 + row = screen_pos.row,
39 + col = 0,
40 + style = 'minimal',
41 + border = vim.g.border_style,
42 + focusable = false,
43 + zindex = 50,
44 + } )
45 + vim.wo[state.window].cursorline = false
46 + vim.wo[state.window].winbar = ''
47 + vim.wo[state.window].foldenable = false
48 + vim.wo[state.window].winhighlight = format.winhighlight( fmt.type, 'NormalFloat' )
49 + return state.window
64 -local M = {}
66 -local function apply_icon(buf, fmt)
67 - vim.api.nvim_buf_clear_namespace(buf, ns, 0, -1)
68 - local icon_hl = format.icon_highlight(fmt.type)
69 - vim.api.nvim_buf_set_extmark(buf, ns, 0, 0, {
70 - virt_text = { { fmt.icon, icon_hl } },
71 - virt_text_pos = 'overlay',
72 - })
52 +local function apply_icon( buf, fmt )
53 + vim.api.nvim_buf_clear_namespace( buf, ns, 0, -1 )
54 + local icon_hl = format.icon_highlight( fmt.type )
55 + vim.api.nvim_buf_set_extmark( buf, ns, 0, 0, {
56 + virt_text = { { fmt.icon, icon_hl } },
57 + virt_text_pos = 'overlay',
58 + } )
75 -function M.show(cmdline_state)
76 - local text = cmdline_state:text()
77 - local firstc = cmdline_state.firstc
78 - local prompt = cmdline_state.prompt
79 - state.text = text
80 - state.firstc = firstc
81 - state.prompt = prompt
82 - local fmt = format.detect(firstc, text, prompt)
83 - local fmt_changed = not state.current_fmt or fmt.type ~= state.current_fmt.type
84 - state.current_fmt = fmt
85 - local display_text = ' ' .. text .. ' '
86 - local width = vim.o.columns
87 - local buf = get_buf()
88 - local win = get_win(buf, width, fmt)
89 - local is_new = not position.get_cmdline_anchor()
90 - if is_new then
91 - local screen_pos = position.at_cursor(width)
92 - vim.api.nvim_win_set_config(win, {
93 - relative = 'editor',
94 - width = width,
95 - height = 1,
96 - row = screen_pos.row,
97 - col = 0,
98 - })
99 - local anchor = {
100 - screenpos = { row = screen_pos.row, col = 0 },
101 - cursor = 0,
102 - buf = buf,
103 - win = win,
104 - bufpos = { row = 0, col = 0 },
105 - }
106 - position.set_cmdline_anchor(anchor)
107 - vim.g.ui_cmdline_pos = { anchor.screenpos.row, anchor.screenpos.col }
108 - end
109 - if fmt_changed then
110 - vim.wo[win].winhighlight = format.winhighlight(fmt.type, 'NormalFloat')
111 - end
112 - vim.api.nvim_buf_set_lines(buf, 0, -1, false, { display_text })
113 - apply_icon(buf, fmt)
114 - highlight.apply(buf, hl_ns, text, fmt.lang, 0, 2)
61 +local function show( cmdline_state )
62 + local text = cmdline_state:text()
63 + local firstc = cmdline_state.firstc
64 + local prompt = cmdline_state.prompt
65 + state.text = text
66 + state.firstc = firstc
67 + state.prompt = prompt
68 + local fmt = format.detect( firstc, text, prompt )
69 + local fmt_changed = not state.current_fmt or fmt.type ~= state.current_fmt.type
70 + state.current_fmt = fmt
71 + local display_text = ' ' .. text .. ' '
72 + local width = vim.o.columns
73 + local buf = get_buf()
74 + local win = get_win( buf, width, fmt )
75 + local is_new = not position.get_cmdline_anchor()
76 + if is_new then
77 + local screen_pos = position.at_cursor( width )
78 + vim.api.nvim_win_set_config( win, {
79 + relative = 'editor',
80 + width = width,
81 + height = 1,
82 + row = screen_pos.row,
83 + col = 0,
84 + } )
85 + local anchor = {
86 + screenpos = { row = screen_pos.row, col = 0 },
87 + cursor = 0,
88 + buf = buf,
89 + win = win,
90 + bufpos = { row = 0, col = 0 },
91 + }
92 + position.set_cmdline_anchor( anchor )
93 + vim.g.ui_cmdline_pos = { anchor.screenpos.row, anchor.screenpos.col }
94 + end
95 + if fmt_changed then
96 + vim.wo[win].winhighlight = format.winhighlight( fmt.type, 'NormalFloat' )
97 + end
98 + vim.api.nvim_buf_set_lines( buf, 0, -1, false, { display_text } )
99 + apply_icon( buf, fmt )
100 + highlight.apply( buf, hl_ns, text, fmt.lang, 0, 2 )
116 - if fmt.type == 'input' and prompt and prompt ~= '' then
117 - vim.api.nvim_buf_clear_namespace(buf, prompt_ns, 0, -1)
118 - local max_prompt_width = math.floor(vim.o.columns * format.PROMPT_MAX_RATIO)
119 - local prompt_is_single_line = not prompt:match '\n'
120 - local prompt_fits = vim.fn.strdisplaywidth(prompt) <= max_prompt_width
121 - if prompt_is_single_line and prompt_fits then
122 - dbg('Path A', { prompt = prompt, prompt_fits = prompt_fits, max_prompt_width = max_prompt_width })
123 - local title_text = ' ' .. prompt .. ' '
124 - vim.api.nvim_win_set_config(win, {
125 - border = vim.g.border_style,
126 - title = { { title_text, 'CmdlineBorderInput' } },
127 - title_pos = 'left',
128 - })
129 - else
130 - local prompt_chunks = format.prompt_lines(prompt, max_prompt_width)
131 - dbg('Path B debug', {
132 - prompt = prompt,
133 - prompt_chunks = prompt_chunks,
134 - max_prompt_width = max_prompt_width,
135 - prompt_width = vim.fn.strdisplaywidth(prompt),
136 - })
137 - -- Put prompt chunks directly in buffer instead of virt_lines
138 - local buf_lines = {}
139 - for _, chunk in ipairs(prompt_chunks) do
140 - table.insert(buf_lines, ' ' .. chunk)
141 - end
142 - table.insert(buf_lines, ' ' .. display_text)
143 - vim.api.nvim_buf_set_lines(buf, 0, -1, false, buf_lines)
144 - dbg('buf_lines set', buf_lines)
145 - vim.api.nvim_win_set_config(win, {
146 - border = vim.g.border_style,
147 - title = { { ' ', 'Normal' } },
148 - title_pos = 'left',
149 - height = #buf_lines,
150 - })
151 - local cfg = vim.api.nvim_win_get_config(win)
152 - dbg('Path B window config', {
153 - height = cfg.height,
154 - width = cfg.width,
155 - buf_line_count = vim.api.nvim_buf_line_count(buf),
156 - })
157 - end
158 - end
102 + if fmt.type == 'input' and prompt and prompt ~= '' then
103 + vim.api.nvim_buf_clear_namespace( buf, prompt_ns, 0, -1 )
104 + local max_prompt_width = math.floor( vim.o.columns * format.PROMPT_MAX_RATIO )
105 + local prompt_is_single_line = not prompt:match '\n'
106 + local prompt_fits = vim.fn.strdisplaywidth( prompt ) <= max_prompt_width
107 + if prompt_is_single_line and prompt_fits then
108 + local title_text = ' ' .. prompt .. ' '
109 + vim.api.nvim_win_set_config( win, {
110 + border = vim.g.border_style,
111 + title = { { title_text, 'CmdlineBorderInput' } },
112 + title_pos = 'left',
113 + } )
114 + else
115 + local prompt_chunks = format.prompt_lines( prompt, max_prompt_width )
116 + local virt_line_tuples = {}
117 + for _, chunk in ipairs( prompt_chunks ) do
118 + vim.notify( 'adding prompt chunk', chunk )
119 + table.insert( virt_line_tuples, { { chunk, 'NormalFloat' } } )
120 + end
121 + vim.api.nvim_buf_set_extmark( buf, prompt_ns, 0, 0, {
122 + virt_lines = virt_line_tuples,
123 + virt_lines_above = true,
124 + } )
125 + vim.api.nvim_win_set_config( win, {
126 + border = vim.g.border_style,
127 + title = { { ' ', 'Normal' } },
128 + title_pos = 'left',
129 + height = #prompt_chunks + 1,
130 + } )
131 + end
132 + end
160 - local cursor_byte = cmdline_state:cursor_byte()
161 - local cursor_col = 2 + cursor_byte
162 - local anchor = position.get_cmdline_anchor()
163 - if anchor then
164 - anchor.cursor = cursor_col
165 - anchor.bufpos.col = cursor_col
166 - position.set_cmdline_anchor(anchor)
167 - end
168 - cursor.fix(win, cursor_col)
134 + local cursor_byte = cmdline_state:cursor_byte()
135 + local cursor_col = 2 + cursor_byte
136 + local anchor = position.get_cmdline_anchor()
137 + if anchor then
138 + anchor.cursor = cursor_col
139 + anchor.bufpos.col = cursor_col
140 + position.set_cmdline_anchor( anchor )
141 + end
142 + cursor.fix( win, cursor_col )
171 -function M.hide()
172 - if vim.api.nvim_win_is_valid(state.window) then
173 - vim.api.nvim_buf_clear_namespace(state.buffer, prompt_ns, 0, -1)
174 - vim.api.nvim_win_hide(state.window)
175 - end
176 - state.window = -1
177 - state.current_fmt = nil
178 - state.text = ''
179 - state.firstc = ''
180 - state.prompt = ''
181 - position.set_cmdline_anchor(nil)
182 - vim.g.ui_cmdline_pos = nil
145 +local function hide()
146 + if vim.api.nvim_win_is_valid( state.window ) then
147 + vim.api.nvim_buf_clear_namespace( state.buffer, prompt_ns, 0, -1 )
148 + vim.api.nvim_win_hide( state.window )
149 + end
150 + state.window = -1
151 + state.current_fmt = nil
152 + state.text = ''
153 + state.firstc = ''
154 + state.prompt = ''
155 + position.set_cmdline_anchor( nil )
156 + vim.g.ui_cmdline_pos = nil
185 -function M.update_cursor(cmdline_state)
186 - if not vim.api.nvim_win_is_valid(state.window) then
187 - return
188 - end
189 - local cursor_byte = cmdline_state:cursor_byte()
190 - local cursor_col = 2 + cursor_byte
191 - local anchor = position.get_cmdline_anchor()
192 - if anchor then
193 - anchor.cursor = cursor_col
194 - position.set_cmdline_anchor(anchor)
195 - end
196 - cursor.fix(state.window, cursor_col)
159 +local function update_cursor( cmdline_state )
160 + if not vim.api.nvim_win_is_valid( state.window ) then
161 + return
162 + end
163 + local cursor_byte = cmdline_state:cursor_byte()
164 + local cursor_col = 2 + cursor_byte
165 + local anchor = position.get_cmdline_anchor()
166 + if anchor then
167 + anchor.cursor = cursor_col
168 + position.set_cmdline_anchor( anchor )
169 + end
170 + cursor.fix( state.window, cursor_col )
199 -function M.get_format_type()
200 - return state.current_fmt and state.current_fmt.type or 'cmdline'
173 +local function get_format_type()
174 + return state.current_fmt and state.current_fmt.type or 'cmdline'
203 -function M.get_text()
204 - return state.text
177 +local function get_text()
178 + return state.text
207 -return M
181 +return {
182 + show = show,
183 + hide = hide,
184 + update_cursor = update_cursor,
185 + get_format_type = get_format_type,
186 + get_text = get_text,

neovim/lua/bugabinga/cmdline/state.lua

Modified
2 2 local table = require 'std.table'
4 4 local M = {}
6 -function M.new(content, pos, firstc, prompt, indent, level)
6 +local function new(content, pos, firstc, prompt, indent, level)
7 7 vim.validate({
8 8 content = { content, 'table' },
9 9 pos = { pos, 'number' },
21 21 }, { __index = M })
24 -function M:text()
24 +local function text(self)
25 25 local parts = {}
26 26 for _, chunk in ipairs(self.content) do
27 27 table.insert(parts, chunk[2])
29 29 return table.concat(parts)
32 -function M:cursor_byte()
32 +local function cursor_byte(self)
33 33 return self.pos
36 -function M:is_search()
36 +local function is_search(self)
37 37 return self.firstc == '/' or self.firstc == '?'
40 -function M:is_input()
40 +local function is_input(self)
41 41 return self.prompt ~= nil and self.prompt ~= ''
44 +M.new = new
45 +M.text = text
46 +M.cursor_byte = cursor_byte
47 +M.is_search = is_search
48 +M.is_input = is_input
44 50 return M

neovim/lua/bugabinga/health.lua

Modified
3 3 -- use the neovim healthcheck module to integrate into :checkhealth.
4 4 -- check the system for dependencies, that:
5 5 -- * i like to use with neovim.
6 6 -- * plugins need.
8 -local _ = {}
8 +local programs_to_check = {}
10 10 local function program_installed(name_of_executable)
11 11 return (vim.fn.executable(name_of_executable) ~= 0)
22 -local programs_to_check = {}
24 -_.add_dependency = function(program)
22 +local function add_dependency(program)
25 23 vim.validate('program', program, 'table')
26 24 vim.validate('name_of_executable', program.name_of_executable, 'string')
28 26 programs_to_check[program.name_of_executable] = program
30 - -- returns itself so that multiple programs can be chain-added
31 - return _.add_dependency
28 + return add_dependency
34 -_.check = function()
31 +local function check()
35 32 vim.iter(programs_to_check)
36 33 :each(function(_, program)
37 34 check_program(program.name, program.name_of_executable)
38 35 end)
41 -return _
38 +return {
39 + add_dependency = add_dependency,
40 + check = check,

neovim/lua/std/dbg/kinds.lua

Modified
38 38 return kind.icon .. ' ' .. kind_name
41 ----@type table<string, DbgKind>
42 -local M = {
41 +return {
43 42 DEBUG = DBG_KINDS.DEBUG,
44 43 TRACE = DBG_KINDS.TRACE,
45 44 ERROR = DBG_KINDS.ERROR,
46 -}
48 ----Returns the kind details for a given kind name.
49 ----@param kind_name string
50 ----@return DbgKind
51 -M.get = get
53 ----Returns a list of all available kind names.
54 ----@return string[]
55 -M.list = list
57 ----Formats a kind name into a display string with icon and name.
58 ----@param kind_name string
59 ----@return string
60 -M.format = format
62 -return M
45 + get = get,
46 + list = list,
47 + format = format,

neovim/lua/std/dbg/viewer.lua

Modified
98 98 local timeago = require 'std.timeago'
99 99 local map = require 'std.map'
101 -local M = {}
103 101 local H = {
104 102 state = nil,
133 131 { key = 'filename',prefix = ' file:', suffix = '' },
136 -function M.get_winbar()
134 +local FILTER_SPECS = {
136 + key = 'all',
137 + extract = function(ev) return ev.kind .. ' ' .. ev.message .. ' ' .. sanitize_src(ev.filename) end,
138 + },
140 + key = 'kind',
141 + extract = function(ev) return ev.kind end,
142 + },
144 + key = 'message',
145 + extract = function(ev) return ev.message end,
146 + },
148 + key = 'filename',
149 + extract = function(ev) return sanitize_src(ev.filename) end,
150 + },
153 +local function get_kind_hl_group(kind_name)
154 + return KIND_TO_HL[kind_name] or 'DiagnosticInfo'
155 +end
157 +local function sanitize_src(src)
158 + if not src or src == '' then return 'unknown' end
159 + return (src:gsub('^@', ''):gsub('^%[string .+%]$', 'unknown'))
160 +end
162 +local function make_winbar()
163 + return string.format(WINBAR_FORMAT, NS_HL.kind_icon)
164 +end
166 +local function get_winbar()
137 167 if not H.state or not H.state.win_id then return '' end
138 168 local s = H.state
139 169 local parts = {}
155 185 return ' ' .. kinds.get('DEBUG').icon .. ' Debug [' .. count_str .. ' events]' .. filter_str
158 -local function make_winbar()
159 - return string.format(WINBAR_FORMAT, NS_HL.kind_icon)
160 -end
162 -local function get_kind_hl_group(kind_name)
163 - return KIND_TO_HL[kind_name] or 'DiagnosticInfo'
164 -end
166 -local function sanitize_src(src)
167 - if not src or src == '' then return 'unknown' end
168 - return (src:gsub('^@', ''):gsub('^%[string .+%]$', 'unknown'))
169 -end
171 188 local function format_event_for_display(event)
172 189 local kind_info = kinds.get(event.kind)
173 190 local icon = kind_info.icon
281 298 return win_id
284 -local function setup_keymaps(buf_id, win_id)
285 - map.normal { keys = 'q', command = M.close, buffer = buf_id, desc = 'Close debug viewer' }
286 - map.normal { keys = '<Esc>', command = M.close, buffer = buf_id, desc = 'Close debug viewer' }
287 - map.normal { keys = 'r', command = M.refresh, buffer = buf_id, desc = 'Refresh events from disk' }
288 - map.normal { keys = '<CR>', command = function() M.jump_to_source(win_id) end, buffer = buf_id, desc = 'Jump to source location' }
289 - map.normal { keys = 'ff', command = function() M.filter_by_all() end, buffer = buf_id, desc = 'Filter combined (all)' }
290 - map.normal { keys = 'fc', command = function() M.clear_filters() vim.notify('Filters cleared') end, buffer = buf_id, desc = 'Clear filters + notify' }
291 - map.normal { keys = 'fk', command = function() M.filter_by_kind() end, buffer = buf_id, desc = 'Filter by kind' }
292 - map.normal { keys = 'fm', command = function() M.filter_by_message() end, buffer = buf_id, desc = 'Filter by message' }
293 - map.normal { keys = 'fl', command = function() M.filter_by_location() end, buffer = buf_id, desc = 'Filter by location' }
294 -end
296 -local FILTER_SPECS = {
297 - {
298 - key = 'all',
299 - extract = function(ev) return ev.kind .. ' ' .. ev.message .. ' ' .. sanitize_src(ev.filename) end,
300 - },
301 - {
302 - key = 'kind',
303 - extract = function(ev) return ev.kind end,
304 - },
305 - {
306 - key = 'message',
307 - extract = function(ev) return ev.message end,
308 - },
309 - {
310 - key = 'filename',
311 - extract = function(ev) return sanitize_src(ev.filename) end,
312 - },
315 301 local function event_passes_filters(event, filters)
316 302 for _, spec in ipairs(FILTER_SPECS) do
317 303 local fval = filters[spec.key]
490 -local function start_watcher()
491 - local path = file.get_path()
492 - local callback = function(err)
493 - if err then return end
494 - on_file_change()
495 - end
496 - local watcher = vim.uv.new_fs_event()
497 - watcher:start(path, {}, callback)
498 - return watcher
499 -end
501 476 local function on_file_change()
502 477 if not H.state then return end
529 504 H.state.watcher = start_watcher()
532 -function M.open()
507 +local function start_watcher()
508 + local path = file.get_path()
509 + local callback = function(err)
510 + if err then return end
511 + on_file_change()
512 + end
513 + local watcher = vim.uv.new_fs_event()
514 + watcher:start(path, {}, callback)
515 + return watcher
516 +end
518 +local function setup_keymaps(buf_id, win_id)
519 + map.normal { keys = 'q', command = close, buffer = buf_id, desc = 'Close debug viewer' }
520 + map.normal { keys = '<Esc>', command = close, buffer = buf_id, desc = 'Close debug viewer' }
521 + map.normal { keys = 'r', command = refresh, buffer = buf_id, desc = 'Refresh events from disk' }
522 + map.normal { keys = '<CR>', command = function() jump_to_source(win_id) end, buffer = buf_id, desc = 'Jump to source location' }
523 + map.normal { keys = 'ff', command = function() filter_by_all() end, buffer = buf_id, desc = 'Filter combined (all)' }
524 + map.normal { keys = 'fc', command = function() clear_filters() vim.notify('Filters cleared') end, buffer = buf_id, desc = 'Clear filters + notify' }
525 + map.normal { keys = 'fk', command = function() filter_by_kind() end, buffer = buf_id, desc = 'Filter by kind' }
526 + map.normal { keys = 'fm', command = function() filter_by_message() end, buffer = buf_id, desc = 'Filter by message' }
527 + map.normal { keys = 'fl', command = function() filter_by_location() end, buffer = buf_id, desc = 'Filter by location' }
528 +end
530 +function open()
533 531 if H.state and H.state.win_id and vim.api.nvim_win_is_valid(H.state.win_id) then
534 532 vim.api.nvim_set_current_win(H.state.win_id)
535 533 return
557 555 render()
560 -function M.close()
558 +function close()
561 559 if not H.state then return end
563 561 if H.state.watcher then
575 573 H.state = nil
578 -function M.refresh()
576 +function refresh()
579 577 if not H.state then return end
581 579 local events = file.read_all()
585 583 render()
588 -function M.is_open()
586 +function is_open()
589 587 return H.state ~= nil
590 588 and H.state.win_id ~= nil
591 589 and vim.api.nvim_win_is_valid(H.state.win_id)
594 -function M.jump_to_source(win_id)
592 +function jump_to_source(win_id)
595 593 if not H.state or not H.state.buf_id then return end
597 595 local cursor = vim.api.nvim_win_get_cursor(win_id or H.state.win_id)
612 610 local path = sanitize_src(event.filename)
613 611 if path == 'unknown' or path == '' then return end
615 - M.close()
613 + close()
616 614 vim.cmd.edit(path)
617 615 if event.line and event.line > 0 then
618 616 pcall(vim.api.nvim_win_set_cursor, 0, { event.line, 0 })
622 -function M.clear_filters()
620 +function clear_filters()
623 621 if not H.state then return end
624 622 H.state.filters = { all = '', kind = '', message = '', filename = '' }
625 623 H.state.filtered_indices = nil
627 625 render()
632 -function M.filter_by_all()
628 +function filter_by_all()
633 629 vim.ui.input({ prompt = 'Filter (all): ' }, function(value)
634 630 if value == nil then return end
635 631 if not H.state then return end
652 -function M.filter_by_kind()
648 +function filter_by_kind()
653 649 do_filter('kind', 'Filter kind')
656 -function M.filter_by_message()
652 +function filter_by_message()
657 653 do_filter('message', 'Filter message')
660 -function M.filter_by_location()
656 +function filter_by_location()
661 657 do_filter('filename', 'Filter location')
664 -M._on_file_change = on_file_change
665 -M._get_state = function() return H.state end
667 -return M
660 +return {
661 + open = open,
662 + close = close,
663 + refresh = refresh,
664 + is_open = is_open,
665 + jump_to_source = jump_to_source,
666 + clear_filters = clear_filters,
667 + filter_by_all = filter_by_all,
668 + filter_by_kind = filter_by_kind,
669 + filter_by_message = filter_by_message,
670 + filter_by_location = filter_by_location,
671 + get_winbar = get_winbar,
672 + _on_file_change = on_file_change,
673 + _get_state = function() return H.state end,

neovim/lua/std/light_up.lua

Modified
1 -local _ = {}
3 1 --- Highlights given words in a buffer.
4 2 --- Word matching is case sensitive.
5 3 --- @param bufnr number Number of buffer to highlight. If 0, current buffer is used.
6 4 --- @param words table Word to highlight group name mapping. Every occurrence with keys in this table, will be
7 5 --- highlighted with its mapped value, which is expected to be a highlight group name.
8 -_.buffer = function(bufnr, words)
9 - vim.validate('bufnr', bufnr, 'number')
10 - vim.validate('words', words, 'table')
6 +local function buffer( bufnr, words )
7 + vim.validate( 'bufnr', bufnr, 'number' )
8 + vim.validate( 'words', words, 'table' )
12 - local namespace = vim.api.nvim_create_namespace 'light_up_ns'
13 - vim.api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)
10 + local namespace = vim.api.nvim_create_namespace( 'light_up_ns' )
11 + vim.api.nvim_buf_clear_namespace( bufnr, namespace, 0, -1 )
15 - local handler = function(event, buffer_handle, tick, first_line, last_line_changed, last_line_updated, bytes)
16 - -- vim.print( event, buffer_handle, tick, first_line, last_line_changed, last_line_updated, bytes )
17 - assert(event == 'lines', 'this handler is for `on_lines` notification')
13 + local handler = function ( event, buffer_handle, tick, first_line, last_line_changed, last_line_updated, bytes )
14 + assert( event == 'lines', 'this handler is for `on_lines` notification' )
19 - local last = math.max(last_line_updated, last_line_changed)
20 - local lines = vim.api.nvim_buf_get_lines(bufnr, first_line, last, true)
21 - --NOTE: dubbel loop. can we faster?
22 - for index, line in ipairs(lines) do
23 - local current_line = first_line + index - 1 -- adjust for 0-based indexing
24 - for word, highlight_group in pairs(words) do
25 - -- NOTE: the way find is used here, we can only ever match one word per line
26 - -- vim.print( 'looking for word ', word, line )
27 - local start_offset, end_offset = line:find('[%s%.](' .. word .. ')[%s$]')
28 - if start_offset then
29 - -- vim.print( 'marking line ' .. current_line, start_offset, end_offset, highlight_group )
30 - vim.api.nvim_buf_set_extmark(bufnr, namespace, current_line, start_offset, {
31 - end_col = end_offset - 1,
32 - hl_group = highlight_group,
33 - })
34 - break
16 + local last = math.max( last_line_updated, last_line_changed )
17 + local lines
18 + if last < 0 then
19 + lines = vim.api.nvim_buf_get_lines( bufnr, first_line, -1, true )
20 + else
21 + lines = vim.api.nvim_buf_get_lines( bufnr, first_line, last, true )
22 + end
23 + for index, line in ipairs( lines ) do
24 + local current_line = first_line + index - 1
25 + for word, highlight_group in pairs( words ) do
26 + local s = 1
27 + while s <= #line do
28 + local word_start = line:find( word, s, true )
29 + if not word_start then break end
30 + local before_char = line:sub( word_start - 1, word_start - 1 )
31 + local after_char = line:sub( word_start + #word, word_start + #word )
32 + local before_ok = #before_char == 0 or before_char:match( '[%s%.]' )
33 + local after_ok = #after_char == 0 or after_char:match( '[%s%.]' )
34 + if before_ok and after_ok then
35 + local start_col = word_start - 1
36 + local end_col = word_start + #word - 1
37 + vim.api.nvim_buf_set_extmark( bufnr, namespace, current_line, start_col, {
38 + end_col = end_col,
39 + hl_group = highlight_group,
40 + } )
41 + end
42 + s = word_start + 1
40 - local ok = vim.api.nvim_buf_attach(bufnr, false, { on_lines = handler, })
41 - if not ok then error('unable to attach to buffer ' .. bufnr) end
48 + local ok = vim.api.nvim_buf_attach( bufnr, false, { on_lines = handler } )
49 + if not ok then error( 'unable to attach to buffer ' .. bufnr ) end
43 - -- initial scan of whole buffer
44 - handler('lines', bufnr, nil, 0, -1, -1, nil)
51 + handler( 'lines', bufnr, nil, 0, -1, -1, nil )
53 + return namespace
47 56 --- @return table groups A map of all highlight group names, mapping to themselves g:match('^([%w@%._]+)%s').
48 -_.get_vim_highlight_groups = function()
57 +local function get_vim_highlight_groups()
49 58 local groups = {}
51 60 local highlight_command_output = vim.fn.execute 'highlight'
52 61 for line in highlight_command_output:gmatch '[^\n]+' do
53 - -- Delimiter xxx guifg=#727272
54 - -- @punctuation.special xxx links to Special
55 - -- Match anything that could be a valid highlight name
56 62 local match = line:match '^([%w@%._]+)%s'
57 63 if match then groups[match] = match end
60 66 return groups
63 -return _
69 +return {
70 + buffer = buffer,
71 + get_vim_highlight_groups = get_vim_highlight_groups,

neovim/lua/std/timeago.lua

Modified
1 1 --- credit goes to https://github.com/f-person/lua-timeago
2 -local _ = {}
4 2 local words = {
5 3 justnow = 'now',
6 4 minute = { singular = '1 mi', plural = 'mis', },
18 16 ---@param time osdate
19 17 ---@return string
20 -function _.format( time )
18 +local function format( time )
21 19 local now = os.time()
22 20 local diff_seconds = os.difftime( now, time )
23 21 if diff_seconds < 45 then return words.justnow end
57 55 return round( diff_years ) .. ' ' .. words.year.plural
60 -return _
58 +return {
59 + format = format,