From f87f029b40ef5bc2179722497c106d36252fa114 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Wed, 31 May 2023 23:52:42 +0800 Subject: [PATCH] fix(todo): fix todo manager for grep --- lua/spacevim/plugin/todo.lua | 80 ++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/lua/spacevim/plugin/todo.lua b/lua/spacevim/plugin/todo.lua index d85b446fa..e30863a3c 100644 --- a/lua/spacevim/plugin/todo.lua +++ b/lua/spacevim/plugin/todo.lua @@ -27,7 +27,9 @@ local logger = require('spacevim.logger').derive('todo') local jobstart = vim.fn.jobstart +-- the labels_partten is used for viml function: matchstr etc local labels_partten = '' +-- labels_regex is for command line argv local labels_regex = '' local function empty(d) -- {{{ @@ -46,26 +48,36 @@ local function stderr(id, data, event) -- {{{ 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) -- {{{ if id ~= todo_jobid then return end for _, d in ipairs(data) do if not empty(d) then - logger.debug('stdout:' .. 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, - }) + table.insert(todos, data_to_todo(d)) end end end @@ -110,7 +122,7 @@ end local function max_len(t, k) -- {{{ local l = 0 for _, v in ipairs(t) do - l = math.max(#v[k], l) + l = math.max(#v[k], l) end return l end @@ -126,20 +138,28 @@ local function on_exit(id, data, event) -- {{{ local fw = max_len(todos, 'file') + 1 local lines = {} for _, v in ipairs(todos) do - table.insert(lines, - prefix .. v.label .. string.rep(' ', lw - #v.label) .. v.file .. string.rep(' ', fw - #v.file) .. v.title + table.insert( + lines, + prefix + .. v.label + .. string.rep(' ', lw - #v.label) + .. v.file + .. string.rep(' ', fw - #v.file) + .. v.title ) end 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. if vim.fn.bufexists(bufnr) == 1 then vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines) end - vim.fn.setbufvar(bufnr,'&ma', ma) + vim.fn.setbufvar(bufnr, '&ma', ma) end -- }}} + +-- labels to command line regex local function get_labels_regex() -- {{{ local sep = '' if grep_default_exe == 'rg' then @@ -162,8 +182,24 @@ local function get_labels_regex() -- {{{ end -- }}} +-- labels to vim searching partten +-- [ todo ] [00:00:03:498] [ Info ] labels_partten: \v\@bug>|\@question>|\@fixme>|\@todo> 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 -- }}} @@ -217,8 +253,8 @@ local function open_win() -- {{{ bufnr = vim.fn.bufnr('%') update_todo_content() vim.api.nvim_buf_set_keymap(bufnr, 'n', '', '', { - callback = open_todo - } ) + callback = open_todo, + }) end -- }}}