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:
parent
b8e22780a8
commit
d8e238b586
@ -1,10 +1 @@
|
||||
[file-build]
|
||||
command = 'echo ${relativeFile}'
|
||||
isBackground = false
|
||||
[file-build.options]
|
||||
cmd = '${workspaceFolder}'
|
||||
[file-run]
|
||||
command = "echo"
|
||||
args = ['hello']
|
||||
isBackground = false
|
||||
|
||||
|
@ -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'))
|
||||
|
@ -80,6 +80,7 @@ lang: zh
|
||||
- [标签管理](#标签管理)
|
||||
- [任务管理](#任务管理)
|
||||
- [任务自动识别](#任务自动识别)
|
||||
- [任务提供源](#任务提供源)
|
||||
- [自定义任务](#自定义任务)
|
||||
- [Iedit 多光标编辑](#iedit-多光标编辑)
|
||||
- [Iedit 快捷键](#iedit-快捷键)
|
||||
@ -1704,6 +1705,49 @@ SpaceVim 目前支持自动识别以下构建系统的任务:npm。
|
||||
|
||||

|
||||
|
||||
#### 任务提供源
|
||||
|
||||
任务提供源可以自动检测并新建任务。例如,一个任务提供源可以自动检测是否存在项目构建文件,比如:`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` 快捷键,将会展示如下任务:
|
||||
|
||||

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

|
||||
|
||||
#### Custom tasks
|
||||
|
||||
this is basic task configuration for running `echo hello world`, and print results to runner windows.
|
||||
|
Loading…
x
Reference in New Issue
Block a user