1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-13 02:05:40 +08:00

fix(todo): fix todo manager for grep

This commit is contained in:
Eric Wong 2023-05-31 23:52:42 +08:00 committed by GitHub
parent d14af8a1d9
commit f87f029b40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -27,7 +27,9 @@ local logger = require('spacevim.logger').derive('todo')
local jobstart = vim.fn.jobstart local jobstart = vim.fn.jobstart
-- the labels_partten is used for viml function: matchstr etc
local labels_partten = '' local labels_partten = ''
-- labels_regex is for command line argv
local labels_regex = '' local labels_regex = ''
local function empty(d) -- {{{ local function empty(d) -- {{{
@ -46,26 +48,36 @@ local function stderr(id, data, event) -- {{{
end end
end end
-- }}} -- }}}
local function data_to_todo(d) -- {{{
logger.debug('stdout:' .. d)
local f = vim.split(d, ':%d+:')[1]
logger.debug('file is:' .. f)
local i, j = string.find(d, ':%d+:')
local line = string.sub(d, i + 1, j - 1)
logger.debug('line is:' .. line)
local column = string.sub(vim.fn.matchstr(d, [[\(:\d\+\)\@<=:\d\+:]]), 1, -2)
local label = string.sub(vim.fn.matchstr(d, labels_partten), #prefix + 1, -1)
logger.debug('label is:' .. label)
local title = vim.fn.get(vim.fn.split(d, prefix .. label), 1, '')
logger.debug('title is:' .. title)
return {
file = f,
line = line,
column = column,
title = title,
label = label,
}
end
-- }}}
local function stdout(id, data, event) -- {{{ local function stdout(id, data, event) -- {{{
if id ~= todo_jobid then if id ~= todo_jobid then
return return
end end
for _, d in ipairs(data) do for _, d in ipairs(data) do
if not empty(d) then if not empty(d) then
logger.debug('stdout:' .. d) table.insert(todos, data_to_todo(d))
local f = vim.split(d, ':%d+:')[1]
local i, j = string.find(d, ':%d+:')
local line = string.sub(d, i + 1, j - 1)
local column = string.sub(vim.fn.matchstr(d, [[\(:\d\+\)\@<=:\d\+:]]), 1, -2)
local label = string.sub(vim.fn.matchstr(d, labels_partten), #prefix + 1, -1)
local title = vim.fn.get(vim.fn.split(d, prefix .. label), 1, '')
table.insert(todos, {
file = f,
line = line,
column = column,
title = title,
label = label,
})
end end
end end
end end
@ -110,7 +122,7 @@ end
local function max_len(t, k) -- {{{ local function max_len(t, k) -- {{{
local l = 0 local l = 0
for _, v in ipairs(t) do for _, v in ipairs(t) do
l = math.max(#v[k], l) l = math.max(#v[k], l)
end end
return l return l
end end
@ -126,20 +138,28 @@ local function on_exit(id, data, event) -- {{{
local fw = max_len(todos, 'file') + 1 local fw = max_len(todos, 'file') + 1
local lines = {} local lines = {}
for _, v in ipairs(todos) do for _, v in ipairs(todos) do
table.insert(lines, table.insert(
prefix .. v.label .. string.rep(' ', lw - #v.label) .. v.file .. string.rep(' ', fw - #v.file) .. v.title lines,
prefix
.. v.label
.. string.rep(' ', lw - #v.label)
.. v.file
.. string.rep(' ', fw - #v.file)
.. v.title
) )
end end
local ma = vim.fn.getbufvar(bufnr, '&ma') local ma = vim.fn.getbufvar(bufnr, '&ma')
vim.fn.setbufvar(bufnr,'&ma', 1) vim.fn.setbufvar(bufnr, '&ma', 1)
-- update the buffer only when the buffer exists. -- update the buffer only when the buffer exists.
if vim.fn.bufexists(bufnr) == 1 then if vim.fn.bufexists(bufnr) == 1 then
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
end end
vim.fn.setbufvar(bufnr,'&ma', ma) vim.fn.setbufvar(bufnr, '&ma', ma)
end end
-- }}} -- }}}
-- labels to command line regex
local function get_labels_regex() -- {{{ local function get_labels_regex() -- {{{
local sep = '' local sep = ''
if grep_default_exe == 'rg' then if grep_default_exe == 'rg' then
@ -162,8 +182,24 @@ local function get_labels_regex() -- {{{
end end
-- }}} -- }}}
-- labels to vim searching partten
-- [ todo ] [00:00:03:498] [ Info ] labels_partten: \v\@bug>|\@question>|\@fixme>|\@todo>
local function get_labels_partten() -- {{{ local function get_labels_partten() -- {{{
return reg.parser(get_labels_regex(), false) local sep = '|'
local rst = [[\v]]
local i = 1
local p = prefix
if prefix == '@' then
p = [[\@]]
end
for _, v in ipairs(labels) do
rst = rst .. p .. v .. [[>]]
if i ~= #labels then
rst = rst .. sep
end
i = i + 1
end
return rst
end end
-- }}} -- }}}
@ -217,8 +253,8 @@ local function open_win() -- {{{
bufnr = vim.fn.bufnr('%') bufnr = vim.fn.bufnr('%')
update_todo_content() update_todo_content()
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<Enter>', '', { vim.api.nvim_buf_set_keymap(bufnr, 'n', '<Enter>', '', {
callback = open_todo callback = open_todo,
} ) })
end end
-- }}} -- }}}