nvim-config

Log | Files | Refs | Submodules | README

commit 2922329186b21216b18ef19c2406c61ae58d3bcd
parent 747ae64f13cad81cdaa4cf41a9ae88975d3731e8
Author: Thomas Vigouroux <tomvig38@gmail.com>
Date:   Fri, 24 Sep 2021 09:13:30 +0200

feat(zest): simplify command

Diffstat:
Mafter/plugin/mappings.vim | 5+++--
Mplugin/zest.lua | 130+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
2 files changed, 104 insertions(+), 31 deletions(-)

diff --git a/after/plugin/mappings.vim b/after/plugin/mappings.vim @@ -1,4 +1,4 @@ -" Last Change: 2021 Sep 03 +" Last Change: 2021 Sep 10 vnoremap <silent> < <gv vnoremap <silent> > >gv @@ -28,4 +28,5 @@ nnoremap [c <cmd>cprevious<CR> " Got from https://prettier.io/docs/en/vim.html nnoremap gp :silent %!prettier --stdin-filepath % --trailing-comma all --single-quote<CR> -nnoremap <Leader>f <cmd>lua require'mem'.open()<CR> +nnoremap <Leader>zs <cmd>1Zest search<CR> +nnoremap <Leader>zc <cmd>Zest create<CR> diff --git a/plugin/zest.lua b/plugin/zest.lua @@ -6,47 +6,119 @@ local function zest(...) return vim.fn.systemlist{'zest', ...} end -function Zest_create() - local fname = (zest 'create')[1] - vim.cmd(string.format("edit %s", fname)) - - vim.cmd[[autocmd BufUnload <buffer> !zest update]] +local function buf_is_zest(bufnr) + bufnr = bufnr or vim.api.nvim_get_current_buf() + local name = vim.api.nvim_buf_get_name(bufnr) + local fullpath = vim.fn.fnamemodify(name, ':p') + return #(zest('search', string.format("file:%s", fullpath))) ~= 0 end -local function set_qflist_with(res) - local qflist = {} - for _,r in ipairs(res) do - local filename, title = unpack(vim.split(r, ':', true)) - table.insert(qflist, { - filename = filename, - text = title - }) +local search +if pcall(require, 'telescope') then + local pickers = require('telescope.pickers') + local sorters = require('telescope.sorters') + local finders = require('telescope.finders') + + search = function(query) + pickers.new { + results_title = 'Zests', + -- Run an external command and show the results in the finder window + finder = finders.new_oneshot_job( + {'zest', 'search', query}, + { + entry_maker = function (line) + local file, title = unpack(vim.split(line, ':', true)) + return { + display = title, + value = file, + ordinal = title + } + end + } + ), + sorter = sorters.get_fuzzy_file(), + }:find() end +else + local function set_qflist_with(res) + local qflist = {} + for _,r in ipairs(res) do + local filename, title = unpack(vim.split(r, ':', true)) + table.insert(qflist, { + filename = filename, + text = title + }) + end - vim.fn.setqflist(qflist, 'r') - vim.cmd[[copen]] + vim.fn.setqflist(qflist, 'r') + vim.cmd[[copen]] + end + search = function(query) + set_qflist_with(zest('search', query)) + end end -function Zest_refs() - local fname = vim.fn.expand("%:p") - local refs = zest('search', string.format("ref:%s", fname)) - set_qflist_with(refs) +local function mk_query(ask, ...) + local q = table.concat({...}, " ") + if #q > 0 then + return q + elseif ask then + return vim.fn.input("Query: ") + else + return "*" + end end -function Zest_search(query) - set_qflist_with(zest('search', query)) +local subcommands = { + search = function(count, ...) + search(mk_query(count > 0, ...)) + end, + goto = function(count, ...) + local res = zest('search', '-f', mk_query(false, ...)) + local file = res[count] or res[1] + + if not file then return end + vim.cmd(string.format("edit %s", file)) + end, + refs = function() + if buf_is_zest() then + search(string.format("ref:%s", vim.fn.expand("%:p"))) + end + end, + create = function() + local fname = (zest 'create')[1] + vim.cmd(string.format("edit %s", fname)) + end +} + +function Zest_complete(arglead, cmdline, cursorpos) + local line_to_cursor = string.sub(cmdline, 1, cursorpos+1) + local elements = vim.split(line_to_cursor, '%s+') + local suggestions = {} + + if #elements == 2 then + for sub, _ in pairs(subcommands) do + if vim.startswith(sub, arglead) then + table.insert(suggestions, sub) + end + end + end + + return suggestions end -function Zest_goto(query) - local res = zest('search', query)[1] - if not res then return end +function Zest(count, cmd, ...) + if not cmd or type(cmd) ~= 'string' then + print("Missing subcommand") + end + + for sub, func in pairs(subcommands) do + if cmd == sub then func(count, ...) return end + end - vim.cmd(string.format("edit %s", vim.split(res, ':', true)[1])) + print(string.format("Unsupported command: %s", cmd)) end -vim.cmd[[command! ZestCreate lua Zest_create()]] -vim.cmd[[command! ZestRefs lua Zest_refs()]] -vim.cmd[[command! -nargs=1 ZestSearch lua Zest_search(<f-args>)]] -vim.cmd[[command! -nargs=1 ZestGoto lua Zest_goto(<f-args>)]] +vim.api.nvim_command[[command! -complete=customlist,v:lua.Zest_complete -count=0 -nargs=+ Zest lua Zest(<count>, <f-args>)]] return M