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

Tasks provider (#3375)

This commit is contained in:
Wang Shidong 2020-02-23 21:29:04 +08:00 committed by GitHub
parent b8e22780a8
commit d8e238b586
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 102 additions and 11 deletions

View File

@ -1,10 +1 @@
[file-build]
command = 'echo ${relativeFile}'
isBackground = false
[file-build.options]
cmd = '${workspaceFolder}'
[file-run]
command = "echo"
args = ['hello']
isBackground = false

View File

@ -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'))

View File

@ -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)
#### 自定义任务

View File

@ -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.