From 3db40b345af78eff5bec09dd1a85d00bc92f085d Mon Sep 17 00:00:00 2001 From: Wang Shidong Date: Tue, 1 Nov 2022 00:21:03 +0800 Subject: [PATCH] feat(tasks): add telescope task --- autoload/SpaceVim/layers/core.vim | 1 + autoload/SpaceVim/layers/telescope.vim | 2 + autoload/SpaceVim/plugins/tasks.vim | 9 +++ config/plugins/telescope.vim | 1 + docs/documentation.md | 17 +++-- lua/telescope/_extensions/task.lua | 89 ++++++++++++++++++++++++++ 6 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 lua/telescope/_extensions/task.lua diff --git a/autoload/SpaceVim/layers/core.vim b/autoload/SpaceVim/layers/core.vim index 2e676e44d..c0ef0badd 100644 --- a/autoload/SpaceVim/layers/core.vim +++ b/autoload/SpaceVim/layers/core.vim @@ -380,6 +380,7 @@ function! SpaceVim#layers#core#config() abort call SpaceVim#mapping#space#def('nnoremap', ['p', 't', 'c'], 'call SpaceVim#plugins#runner#clear_tasks()', 'clear-tasks', 1) call SpaceVim#mapping#space#def('nnoremap', ['p', 't', 'r'], \ 'call SpaceVim#plugins#runner#run_task(SpaceVim#plugins#tasks#get())', 'pick-task-to-run', 1) + " SPC p t f is defined in fuzzy finder layer call SpaceVim#mapping#space#def('nnoremap', ['p', 'k'], 'call SpaceVim#plugins#projectmanager#kill_project()', 'kill-all-project-buffers', 1) call SpaceVim#mapping#space#def('nnoremap', ['p', 'p'], 'call SpaceVim#plugins#projectmanager#list()', 'list-all-projects', 1) call SpaceVim#mapping#space#def('nnoremap', ['p', '/'], 'Grepper', 'fuzzy search for text in current project', 1) diff --git a/autoload/SpaceVim/layers/telescope.vim b/autoload/SpaceVim/layers/telescope.vim index 7c4a41920..27c207e0c 100644 --- a/autoload/SpaceVim/layers/telescope.vim +++ b/autoload/SpaceVim/layers/telescope.vim @@ -165,6 +165,8 @@ function! SpaceVim#layers#telescope#config() abort \ ], \ 1) + call SpaceVim#mapping#space#def('nnoremap', ['p', 't', 'f'], + \ 'Telescope task', 'fuzzy-find-tasks', 1) let g:_spacevim_mappings.f = {'name' : '+Fuzzy Finder'} call s:defind_fuzzy_finder() diff --git a/autoload/SpaceVim/plugins/tasks.vim b/autoload/SpaceVim/plugins/tasks.vim index 7e370f281..f751a71e5 100644 --- a/autoload/SpaceVim/plugins/tasks.vim +++ b/autoload/SpaceVim/plugins/tasks.vim @@ -366,6 +366,15 @@ function! s:update_tasks_win_context() abort call s:BUF.buf_set_lines(s:task_viewer_bufnr, 0, -1, 0, sort(lines)) endfunction +function! SpaceVim#plugins#tasks#get_tasks() abort + call s:load() + for Provider in s:providers + call extend(s:task_config, call(Provider, [])) + endfor + call s:init_variables() + return s:task_config +endfunction + function! SpaceVim#plugins#tasks#edit(...) abort if get(a:000, 0, 0) exe 'e ~/.SpaceVim.d/tasks.toml' diff --git a/config/plugins/telescope.vim b/config/plugins/telescope.vim index 05d526b92..1766ec655 100644 --- a/config/plugins/telescope.vim +++ b/config/plugins/telescope.vim @@ -3,6 +3,7 @@ lua require('telescope').load_extension('messages') lua require('telescope').load_extension('project') lua require('telescope').load_extension('scriptnames') lua require('telescope').load_extension('neoyank') +lua require('telescope').load_extension('task') lua require('telescope').load_extension('zettelkasten_template') if filereadable(g:_spacevim_root_dir . 'bundle/telescope-fzf-native.nvim/build/libfzf.so') \ || filereadable(g:_spacevim_root_dir . 'bundle/telescope-fzf-native.nvim/build/libfzf.dll') diff --git a/docs/documentation.md b/docs/documentation.md index 9cecbcae0..332c712f4 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -2152,14 +2152,21 @@ which is similar to VSCode's tasks-manager. There are two kinds of task configur The tasks defined in the global tasks configuration can be overrided by project local tasks configuration. -| Key Bindings | Descriptions | -| ------------ | ----------------------------- | -| `SPC p t e` | edit tasks configuration file | -| `SPC p t r` | select task to run | -| `SPC p t l` | list all available tasks | +| Key Bindings | Descriptions | +| ------------ | ----------------------------------------- | +| `SPC p t e` | edit tasks configuration file | +| `SPC p t r` | select task to run | +| `SPC p t l` | list all available tasks | +| `SPC p t f` | fuzzy find tasks(require telescope layer) | + +The `SPC p t l` will open the tasks manager windows, in the tasks manager windows, you can use `Enter` to run task under the cursor. ![task_manager](https://user-images.githubusercontent.com/13142418/94822603-69d0c700-0435-11eb-95a7-b0b4fef91be5.png) +If the `telescope` layer is loaded, you can also use `SPC p t f` to fuzzy find specific task, and run the select task. + +![fuzzy-task](https://user-images.githubusercontent.com/13142418/199057483-d5cce17c-2f06-436d-bf7d-24a78d0eeb11.png) + #### Custom tasks This is a basic task configuration for running `echo hello world`, diff --git a/lua/telescope/_extensions/task.lua b/lua/telescope/_extensions/task.lua new file mode 100644 index 000000000..b8557d1ff --- /dev/null +++ b/lua/telescope/_extensions/task.lua @@ -0,0 +1,89 @@ +local action_state = require('telescope.actions.state') +local actions = require('telescope.actions') +local conf = require('telescope.config').values +local entry_display = require('telescope.pickers.entry_display') +local finders = require('telescope.finders') +local pickers = require('telescope.pickers') + +local function prepare_project_task_output(register) + local rst = {} + local taskconfig = vim.api.nvim_eval('SpaceVim#plugins#tasks#get_tasks()') + for k, v in pairs(taskconfig) do + table.insert(rst, { + name = k, + task = v, + }) + end + return rst +end + +local function show_taskconfig(opts) + local opts = opts or {} + local register = opts.register or '"' + local displayer = entry_display.create({ + separator = ' ', + items = { + { width = 25 }, + { width = 15 }, + { remaining = true }, + }, + }) + local function make_display(entry) + local n = entry.value.name + local desc = entry.value.task.description + if desc == nil then + desc = entry.value.task.command + if entry.value.task.args ~= nil then + desc = desc .. ' ' .. table.concat(entry.value.task.args, ' ') + end + end + local task_type = 'local' + if entry.value.task.isGlobal == 1 then + task_type = 'global' + elseif entry.value.task.isDetected == 1 then + task_type = 'detected' + end + return displayer({ + { '[' .. entry.value.name .. ']', 'TelescopeResultsVariable' }, + { '[' .. task_type .. ']', 'TelescopeResultsNumber' }, + { desc, 'TelescopeResultsComment' }, + }) + end + pickers + .new(opts, { + prompt_title = 'Project Tasks', + finder = finders.new_table({ + results = prepare_project_task_output(register), + entry_maker = function(entry) + return { + value = entry, + display = make_display, + ordinal = entry.name, + } + end, + }), + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local entry = action_state.get_selected_entry() + actions.close(prompt_bufnr) + -- vim.fn.setreg('"', entry.value[1]) + local task = entry.value.task + vim.fn['SpaceVim#plugins#runner#run_task'](task) + end) + return true + end, + }) + :find() +end + +local function run() + show_taskconfig() +end + +return require('telescope').register_extension({ + exports = { + -- Default when to argument is given, i.e. :Telescope neoyank + task = run, + }, +})