1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-04-14 15:19:12 +08:00

Add type support for A plugin (#3308)

This commit is contained in:
Wang Shidong 2020-01-28 16:10:31 +08:00 committed by GitHub
parent b2a0a3a78a
commit 7c6fdad120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 202 additions and 21 deletions

View File

@ -1,5 +1,6 @@
{
"autoload/SpaceVim/api/*.vim": {"alternate": "test/api/{}.vader"},
"autoload/SpaceVim/layers/lang/*.vim": {"doc": "docs/layers/lang/{}.md"},
"test/api/*.vader": {"alternate": "autoload/SpaceVim/api/{}.vim"},
"docs/*.md": {"alternate": "docs/cn/{}.md"},
"docs/cn/*.md": {"alternate": "docs/{}.md"},
@ -30,5 +31,9 @@
"docs/_posts/2018-09-27-use-vim-as-ide.md": {"alternate": "docs/_posts/2018-09-28-use-vim-as-ide.md"},
"docs/_posts/2018-01-23-grep-on-the-fly-in-spacevim.md": {"alternate": "docs/_posts/2018-01-31-grep-on-the-fly-in-spacevim.md"},
"docs/_posts/2018-01-31-grep-on-the-fly-in-spacevim.md": {"alternate": "docs/_posts/2018-01-23-grep-on-the-fly-in-spacevim.md"},
"docs/_posts/2018-09-28-use-vim-as-ide.md": {"alternate": "docs/_posts/2018-09-27-use-vim-as-ide.md"}
"docs/_posts/2018-09-28-use-vim-as-ide.md": {"alternate": "docs/_posts/2018-09-27-use-vim-as-ide.md"},
"docs/_posts/2020-01-27-manage-project-alternate-files.md": {"alternate": "docs/_posts/2020-01-28-manage-project-alternate-files.md"},
"docs/_posts/2020-01-28-manage-project-alternate-files.md": {"alternate": "docs/_posts/2020-01-27-manage-project-alternate-files.md"},
"docs/_posts/2019-07-17-Asynchronous-todo-manager.md": {"alternate": "docs/_posts/2019-07-16-Asynchronous-todo-manager.md"},
"docs/_posts/2019-07-16-Asynchronous-todo-manager.md": {"alternate": "docs/_posts/2019-07-17-Asynchronous-todo-manager.md"}
}

View File

@ -17,6 +17,7 @@ let s:CMP = SpaceVim#api#import('vim#compatible')
let s:JSON = SpaceVim#api#import('data#json')
let s:FILE = SpaceVim#api#import('file')
let s:conf = '.project_alt.json'
let s:cache_path = '~/.cache/SpaceVim/a.json'
" this is for saving the project configuration information. Use the path of
@ -24,6 +25,17 @@ let s:conf = '.project_alt.json'
let s:project_config = {}
" saving cache
function! s:cache() abort
call writefile([s:JSON.json_encode(s:project_config)], s:FILE.unify_path(s:cache_path, ':p'))
endfunction
function! s:load_cache() abort
let s:project_config = s:JSON.json_decode(join(readfile(s:cache_path, ''), ''))
endfunction
" when this function is called, the project_config file name is changed, and
" the project_config info is cleared.
@ -40,10 +52,11 @@ function! s:get_project_config(conf_file) abort
return s:JSON.json_decode(join(readfile(a:conf_file), "\n"))
endfunction
function! SpaceVim#plugins#a#alt() abort
function! SpaceVim#plugins#a#alt(...) abort
let type = get(a:000, 0, 'alternate')
let conf_file_path = s:FILE.unify_path(s:conf, ':p')
let file = s:FILE.unify_path(bufname('%'), ':.')
let alt = SpaceVim#plugins#a#get_alt(file, conf_file_path)
let alt = SpaceVim#plugins#a#get_alt(file, conf_file_path, type)
if !empty(alt)
exe 'e ' . alt
endif
@ -52,29 +65,32 @@ endfunction
function! s:paser(conf, root) abort
for key in keys(a:conf)
let searchpath = key
if match(key, '/*')
if match(key, '/\*')
let searchpath = substitute(key, '*', '**/*', 'g')
endif
for file in s:CMP.globpath('.', searchpath)
let file = s:FILE.unify_path(file, ':.')
let s:project_config[a:root][file] = {}
if has_key(a:conf, file)
if has_key(a:conf[file], 'alternate')
let s:project_config[a:root][file] = {'alternate' : a:conf[file]['alternate']}
continue
endif
endif
let conf = a:conf[key]
if has_key(conf, 'alternate')
let begin_end = split(key, '*')
if len(begin_end) == 2
let s:project_config[a:root][file] = {'alternate' : s:add_alternate_file(begin_end, file, a:conf[key]['alternate'])}
endif
for type in keys(a:conf[file])
if len(begin_end) == 2
let s:project_config[a:root][file][type] = a:conf[key][type]
endif
endfor
else
for type in keys(a:conf[key])
let begin_end = split(key, '*')
if len(begin_end) == 2
let s:project_config[a:root][file][type] = s:get_type_path(begin_end, file, a:conf[key][type])
endif
endfor
endif
endfor
endfor
call s:cache()
endfunction
function! s:add_alternate_file(a, f, b) abort
function! s:get_type_path(a, f, b) abort
let begin_len = strlen(a:a[0])
let end_len = strlen(a:a[1])
"docs/*.md": {"alternate": "docs/cn/{}.md"},
@ -84,14 +100,16 @@ function! s:add_alternate_file(a, f, b) abort
return substitute(a:b, '{}', a:f[begin_len : (end_len+1) * -1], 'g')
endfunction
function! SpaceVim#plugins#a#get_alt(file, conf_path) abort
function! SpaceVim#plugins#a#get_alt(file, conf_path, ...) abort
if getftime(a:conf_path) < getftime(s:cache_path)
endif
if !has_key(s:project_config, a:conf_path)
let altconfa = s:get_project_config(a:conf_path)
let s:project_config[a:conf_path] = {}
call s:paser(altconfa, a:conf_path)
endif
try
return s:project_config[a:conf_path][a:file]['alternate']
return s:project_config[a:conf_path][a:file][get(a:000, 0, 'alternate')]
catch
return ''
endtry
@ -102,6 +120,23 @@ function! SpaceVim#plugins#a#get_root() abort
return s:FILE.unify_path(s:conf, ':p')
endfunction
function! SpaceVim#plugins#a#complete(ArgLead, CmdLine, CursorPos) abort
let file = s:FILE.unify_path(bufname('%'), ':.')
let conf = s:FILE.unify_path(s:conf, ':p')
if !has_key(s:project_config, conf )
let altconfa = s:get_project_config(conf)
let s:project_config[conf] = {}
call s:paser(altconfa, conf)
endif
try
let a = s:project_config[s:FILE.unify_path(s:conf, ':p')][file]
catch
let a = {}
endtry
return join(keys(a), "\n")
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo

View File

@ -9,4 +9,4 @@ command! -nargs=+ -complete=custom,SpaceVim#plugins#projectmanager#complete_proj
command! -nargs=* -complete=custom,SpaceVim#plugins#pmd#complete PMD :call SpaceVim#plugins#pmd#run(<f-args>)
command! -nargs=0 A :call SpaceVim#plugins#a#alt()
command! -nargs=? -complete=custom,SpaceVim#plugins#a#complete A :call SpaceVim#plugins#a#alt(<f-args>)

View File

@ -0,0 +1,23 @@
---
title: "异步待办事项管理器"
categories: [feature_cn, blog_cn]
excerpt: "通过异步检索展示整个项目中所有的待办事项,显示待办事项标签以及标题,同时支持跳转待办事项"
image: https://user-images.githubusercontent.com/13142418/35278709-7856ed62-0010-11e8-8b1e-e6cc6374b0dc.gif
permalink: /cn/:title/
lang: zh
type: BlogPosting
comments: true
commentsID: "异步待办事项管理器"
---
# [Blogs](../blog/) >> 异步待办事项管理器
{{ page.date | date_to_string }}
SpaceVim 内置了一个异步待办事项管理器,默认的快捷键为 `SPC a o`,默认的标签包括:
`'fixme', 'question', 'todo', 'idea'`
![todo manager](https://user-images.githubusercontent.com/13142418/61462920-0bd9d000-a9a6-11e9-8e1f-c70d6ec6ca1e.png)
这一插件也自动同步到独立仓库,以便于非 SpaceVim 用户使用:[vim-todo](https://github.com/wsdjeg/vim-todo/).

View File

@ -0,0 +1,60 @@
---
title: "自定义工程文件跳转"
categories: [feature_cn, blog_cn]
excerpt: "通过配置文件,自定义工程文件跳转路径,包括跳转至测试源文件、文档源文件等。"
image: https://user-images.githubusercontent.com/13142418/73239989-98c4d800-41d8-11ea-8c5b-383076cfcd6c.png
permalink: /cn/:title/
lang: zh
type: BlogPosting
comments: true
commentsID: "自定义工程文件跳转"
---
# [Blogs](../blog/) >> 自定义工程文件跳转
{{ page.date | date_to_string }}
## 起因和目的
起初,在管理 SpaceVim 这一项目时,每编辑一个模块源文件,总是需要关注以下几件事:
1. 相关的文档是否存在,是否需要修改,在工程内存在中英文版本的文档,是否内容保持一致。
2. 测试文件是否存在,是否需要修改。
出于以上两点的考虑,衍生出如下需求:
1. 在编辑源文件时,迅速跳转至文档所在的源文件;
2. 在编辑中文文档时,迅速跳转至英文文档,反之亦然;
3. 在编辑源文件时,迅速跳转至测试文件,反之亦然;
目前SpaceVim 内置的这一插件基本实现了以上功能,以便于快速在相关文件之间进行跳转。
## 基本的使用
SpaceVim 提供了一个内置的工程文件跳转插件,默认的命令为 `:A`
该命令可接收参数,指定跳转类别:
![a](https://user-images.githubusercontent.com/13142418/73239989-98c4d800-41d8-11ea-8c5b-383076cfcd6c.png)
在使用这一特性之前,需要在工程根目录添加配置文件 `.project_alt.json`。例如:
```json
{
"autoload/SpaceVim/layers/lang/*.vim": {"doc": "docs/layers/lang/{}.md"},
}
```
加入以上配置文件后,当编辑 `autoload/SpaceVim/layers/lang/java.vim` 文件时,
可以通过 `:A doc` 跳转至 `docs/layers/lang/java.md` 文件。

View File

@ -0,0 +1,55 @@
---
title: "Manage project alternate files"
categories: [feature, blog]
excerpt: "Manage the alternate file of current project within SpaceVim."
image: https://user-images.githubusercontent.com/13142418/73239989-98c4d800-41d8-11ea-8c5b-383076cfcd6c.png
commentsID: "Manage project alternate files"
comments: true
---
# [Blogs](../blog/) >> Manage project alternate files
{{ page.date | date_to_string }}
<!-- vim-markdown-toc GFM -->
- [Causes and purposes](#causes-and-purposes)
- [Basic usage](#basic-usage)
<!-- vim-markdown-toc -->
## Causes and purposes
At first, when managing the SpaceVim project,
every time you edited a module source file,
you always need to pay attention to the following things:
1. Whether the relevant documents exist, whether they need to be modified, whether there are Chinese and English versions of documents in the project, and whether the contents are consistent.
2. Test whether the file exists and whether it needs to be modified.
In view of the above two points, the following requirements are derived:
1. When editing a source file, quickly jump to the source file where the document is located;
2. When editing Chinese documents, quickly jump to English documents and vice versa;
3. When editing the source file, quickly jump to the test file and vice versa;
At present, this plugin built into SpaceVim basically implements the above functions, in order to quickly jump between related files.
## Basic usage
SpaceVim provides a built-in alternate file manager, the command is `:A`.
![a](https://user-images.githubusercontent.com/13142418/73239989-98c4d800-41d8-11ea-8c5b-383076cfcd6c.png)
To use this feature, you can create a `.project_alt.json` file in the root of your project. for example:
```json
{
"autoload/SpaceVim/layers/lang/*.vim": {"doc": "docs/layers/lang/{}.md"},
}
```
after adding this configuration, when edit `autoload/SpaceVim/layers/lang/java.vim`,
you can use `:A doc` switch to `docs/layers/lang/java.md`

View File

@ -3,10 +3,13 @@ Execute ( SpaceVim plugin: a.vim ):
let root = SpaceVim#plugins#a#get_root()
AssertEqual SpaceVim#plugins#a#get_alt('docs/index.md', root), 'docs/cn/index.md'
AssertEqual SpaceVim#plugins#a#get_alt('docs/cn/index.md', root), 'docs/index.md'
" AssertEqual SpaceVim#plugins#a#get_alt('docs/api/job.md', root), 'docs/cn/api/job.md'
" AssertEqual SpaceVim#plugins#a#get_alt('docs/cn/api/job.md', root), 'docs/api/job.md'
AssertEqual SpaceVim#plugins#a#get_alt('docs/api/job.md', root), 'docs/cn/api/job.md'
AssertEqual SpaceVim#plugins#a#get_alt('docs/cn/api/job.md', root), 'docs/api/job.md'
AssertEqual SpaceVim#plugins#a#get_alt('wiki/en/Project-layout.md', root), 'wiki/cn/Project-layout.md'
AssertEqual SpaceVim#plugins#a#get_alt('wiki/cn/Project-layout.md', root), 'wiki/en/Project-layout.md'
AssertEqual SpaceVim#plugins#a#get_alt('docs/_posts/2018-09-28-use-vim-as-ide.md', root), 'docs/_posts/2018-09-27-use-vim-as-ide.md'
AssertEqual SpaceVim#plugins#a#get_alt('docs/_posts/2018-09-27-use-vim-as-ide.md', root), 'docs/_posts/2018-09-28-use-vim-as-ide.md'
AssertEqual SpaceVim#plugins#a#get_alt('docs/_posts/2020-01-28-manage-project-alternate-files.md', root), 'docs/_posts/2020-01-27-manage-project-alternate-files.md'
AssertEqual SpaceVim#plugins#a#get_alt('docs/_posts/2020-01-27-manage-project-alternate-files.md', root), 'docs/_posts/2020-01-28-manage-project-alternate-files.md'
unlet root