diff --git a/.SpaceVim.d/tasks.toml b/.SpaceVim.d/tasks.toml index 28b563198..8b1378917 100644 --- a/.SpaceVim.d/tasks.toml +++ b/.SpaceVim.d/tasks.toml @@ -1,10 +1 @@ -[file-build] - command = 'echo ${relativeFile}' - isBackground = false -[file-build.options] - cmd = '${workspaceFolder}' -[file-run] - command = "echo" - args = ['hello'] - isBackground = false diff --git a/autoload/SpaceVim/plugins/tasks.vim b/autoload/SpaceVim/plugins/tasks.vim index 6743711dc..2419e7e4f 100644 --- a/autoload/SpaceVim/plugins/tasks.vim +++ b/autoload/SpaceVim/plugins/tasks.vim @@ -24,6 +24,7 @@ let s:select_task = {} let s:conf = [] let s:bufnr = -1 let s:variables = {} +let s:providers = [] function! s:load() abort @@ -86,7 +87,9 @@ endfunction function! SpaceVim#plugins#tasks#get() call s:load() - call s:detect_npm_tasks() + for Provider in s:providers + call extend(s:conf, call(Provider, [])) + endfor call s:init_variables() let task = s:pick() if has_key(task, 'windows') && s:SYS.isWindows @@ -152,15 +155,23 @@ function! SpaceVim#plugins#tasks#edit(...) endfunction function! s:detect_npm_tasks() abort + let detect_task = {} let conf = {} if filereadable('package.json') let conf = s:JSON.json_decode(join(readfile('package.json', ''), '')) endif if has_key(conf, 'scripts') for task_name in keys(conf.scripts) - call extend(s:conf, { + call extend(detect_task, { \ task_name : {'command' : conf.scripts[task_name], 'isDetected' : 1, 'detectedName' : 'npm:'} \ }) endfor endif + return detect_task endfunction + +function! SpaceVim#plugins#tasks#reg_provider(provider) + call add(s:providers, a:provider) +endfunction + +call SpaceVim#plugins#tasks#reg_provider(funcref('s:detect_npm_tasks')) diff --git a/docs/cn/documentation.md b/docs/cn/documentation.md index a211a0378..3ba8ebcbd 100644 --- a/docs/cn/documentation.md +++ b/docs/cn/documentation.md @@ -80,6 +80,7 @@ lang: zh - [标签管理](#标签管理) - [任务管理](#任务管理) - [任务自动识别](#任务自动识别) + - [任务提供源](#任务提供源) - [自定义任务](#自定义任务) - [Iedit 多光标编辑](#iedit-多光标编辑) - [Iedit 快捷键](#iedit-快捷键) @@ -1704,6 +1705,49 @@ SpaceVim 目前支持自动识别以下构建系统的任务:npm。 ![task-auto-detection](https://user-images.githubusercontent.com/13142418/75089003-471d2c80-558f-11ea-8aea-cbf7417191d9.png) +#### 任务提供源 + +任务提供源可以自动检测并新建任务。例如,一个任务提供源可以自动检测是否存在项目构建文件,比如:`package.json`, +如果存在则根据其内容创建 npm 的构建任务。 + + +在 SpaceVim 里,如果需要新建任务提供源,需要使用启动函数,任务提供源是一个 Vim 函数,该函数返回一系列任务对象。 + +以下为一个简单的示例: + + +```vim +function! s:make_tasks() abort + if filereadable('Makefile') + let subcmd = filter(readfile('Makefile', ''), "v:val=~#'^.PHONY'") + if !empty(subcmd) + let commands = split(subcmd[0])[1:] + let conf = {} + for cmd in commands + call extend(conf, { + \ cmd : { + \ 'command': 'make', + \ 'args' : [cmd], + \ 'isDetected' : 1, + \ 'detectedName' : 'make:' + \ } + \ }) + endfor + return conf + else + return {} + endif + else + return {} + endif +endfunction +call SpaceVim#plugins#tasks#reg_provider(funcref('s:make_tasks')) +``` + +将以上内容加入启动函数,在 SpceVim 仓库内按下 `SPC p t r` 快捷键,将会展示如下任务: + +![task-make](https://user-images.githubusercontent.com/13142418/75105016-084cac80-564b-11ea-9fe6-75d86a0dbb9b.png) + #### 自定义任务 diff --git a/docs/documentation.md b/docs/documentation.md index e0e08df12..fac6cf6dc 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -79,6 +79,7 @@ description: "General documentation about how to using SpaceVim, including the q - [Bookmarks management](#bookmarks-management) - [Tasks](#tasks) - [Task auto-detection](#task-auto-detection) + - [Task provider](#task-provider) - [Custom tasks](#custom-tasks) - [Replace text with iedit](#replace-text-with-iedit) - [iedit states key bindings](#iedit-states-key-bindings) @@ -1729,6 +1730,50 @@ then pressing `SPC p t r` shows the following list: ![task-auto-detection](https://user-images.githubusercontent.com/13142418/75089003-471d2c80-558f-11ea-8aea-cbf7417191d9.png) + +#### Task provider + +Some tasks can be automatically detected by task provider. For example, +a Task Provider could check if there is a specific build file, such as `package.json`, +and create npm tasks. + +To build a task provider, you need to use Bootstrap function. The task provider should be a vim function. +and return a task object. + +here is an example for building task provider. + +```vim +function! s:make_tasks() abort + if filereadable('Makefile') + let subcmd = filter(readfile('Makefile', ''), "v:val=~#'^.PHONY'") + if !empty(subcmd) + let commands = split(subcmd[0])[1:] + let conf = {} + for cmd in commands + call extend(conf, { + \ cmd : { + \ 'command': 'make', + \ 'args' : [cmd], + \ 'isDetected' : 1, + \ 'detectedName' : 'make:' + \ } + \ }) + endfor + return conf + else + return {} + endif + else + return {} + endif +endfunction +call SpaceVim#plugins#tasks#reg_provider(funcref('s:make_tasks')) +``` + +with above configuration, you will see following tasks in SpaceVim repo: + +![task-make](https://user-images.githubusercontent.com/13142418/75105016-084cac80-564b-11ea-9fe6-75d86a0dbb9b.png) + #### Custom tasks this is basic task configuration for running `echo hello world`, and print results to runner windows.