From 1b7c6b56e6bbc444d9bef87602650ead55cb4559 Mon Sep 17 00:00:00 2001 From: Wang Shidong Date: Sun, 6 Jan 2019 13:36:33 +0800 Subject: [PATCH] Add alt file plugin (#2416) --- .SpaceVim.d/init.vim | 2 + autoload/SpaceVim/api/file.vim | 9 +++ autoload/SpaceVim/layers/core.vim | 2 - autoload/SpaceVim/plugins/a.vim | 74 +++++++++++++++++++ codecov.yml | 10 ++- config/commands.vim | 2 + .../2018-09-28-use-vim-as-a-python-ide.md | 2 +- docs/cn/layers/lang/vue.md | 2 +- docs/documentation.md | 2 +- test/plugin/a.vader | 12 +++ 10 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 autoload/SpaceVim/plugins/a.vim create mode 100644 test/plugin/a.vader diff --git a/.SpaceVim.d/init.vim b/.SpaceVim.d/init.vim index 2d6e18c3d..8c2993963 100644 --- a/.SpaceVim.d/init.vim +++ b/.SpaceVim.d/init.vim @@ -24,3 +24,5 @@ function! s:language_specified_mappings() abort \ 'execute current file', 1) endfunction call SpaceVim#mapping#space#regesit_lang_mappings('vader', function('s:language_specified_mappings')) +call SpaceVim#plugins#a#set_config_name('.projections.json') + diff --git a/autoload/SpaceVim/api/file.vim b/autoload/SpaceVim/api/file.vim index 87513c391..c750a1681 100644 --- a/autoload/SpaceVim/api/file.vim +++ b/autoload/SpaceVim/api/file.vim @@ -219,8 +219,17 @@ function! s:updatefiles(files) abort return failed endfunction + let s:file['updateFiles'] = function('s:updatefiles') +function! s:unify_path(path, ...) abort + let mod = a:0 > 0 ? a:1 : ':p' + return resolve(fnamemodify(a:path, mod . ':gs?[\\/]?/?')) +endfunction + +let s:file['unify_path'] = function('s:unify_path') + + function! SpaceVim#api#file#get() abort return deepcopy(s:file) endfunction diff --git a/autoload/SpaceVim/layers/core.vim b/autoload/SpaceVim/layers/core.vim index 9963985d4..46635b475 100644 --- a/autoload/SpaceVim/layers/core.vim +++ b/autoload/SpaceVim/layers/core.vim @@ -34,8 +34,6 @@ function! SpaceVim#layers#core#plugins() abort \'on_map' : '(openbrowser-', \ 'loadconf' : 1, \}]) - call add(plugins, ['tpope/vim-projectionist', { 'on_cmd' : ['A', 'AS', 'AV', - \ 'AT', 'AD', 'Cd', 'Lcd', 'ProjectDo']}]) call add(plugins, ['mhinz/vim-grepper' , { 'on_cmd' : 'Grepper', \ 'loadconf' : 1} ]) return plugins diff --git a/autoload/SpaceVim/plugins/a.vim b/autoload/SpaceVim/plugins/a.vim new file mode 100644 index 000000000..2f9d06748 --- /dev/null +++ b/autoload/SpaceVim/plugins/a.vim @@ -0,0 +1,74 @@ +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:project_config = {} + +function! SpaceVim#plugins#a#set_config_name(name) + + let s:conf = a:name + +endfunction + +function! SpaceVim#plugins#a#alt() + let conf_file = s:FILE.unify_path(s:conf, ':p') + let file = s:FILE.unify_path(bufname('%'), ':.') + let alt = SpaceVim#plugins#a#get_alt(file, conf_file) + if !empty(alt) + exe 'e ' . alt + endif +endfunction + +function! s:paser(conf, root) abort + for key in keys(a:conf) + for file in s:CMP.globpath('.', substitute(key, '*', '**/*', 'g')) + let file = s:FILE.unify_path(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 + endif + endfor + endfor +endfunction + +function! s:add_alternate_file(a, f, b) abort + let begin_len = strlen(a:a[0]) + let end_len = strlen(a:a[1]) + "docs/*.md": {"alternate": "docs/cn/{}.md"}, + "begin_end = 5 + "end_len = 3 + "docs/index.md + return substitute(a:b, '{}', a:f[begin_len : (end_len+1) * -1], 'g') +endfunction + +function! Log() abort + return s:project_config +endfunction + +function! SpaceVim#plugins#a#get_alt(file, root) + if !has_key(s:project_config, a:root) + let altconfa = s:JSON.json_decode(join(readfile(a:root), "\n")) + let s:project_config[a:root] = {} + call s:paser(altconfa, a:root) + endif + try + return s:project_config[a:root][a:file]['alternate'] + catch + return '' + endtry +endfunction + + +function! SpaceVim#plugins#a#get_root() + return s:FILE.unify_path(s:conf, ':p') +endfunction diff --git a/codecov.yml b/codecov.yml index cec7dbcfd..437f430bf 100644 --- a/codecov.yml +++ b/codecov.yml @@ -10,7 +10,13 @@ coverage: project: default: false # disable the default status that measures entire project api: # declare a new status context "api" - target: 70% # we always want 100% coverage here + target: 35% # we always want 100% coverage here paths: "autoload/SpaceVim/api/" # only include coverage in "tests/" folder - app: # declare a new status context "app" + leyer: # declare a new status context "api" + target: 35% # we always want 100% coverage here + paths: "autoload/SpaceVim/layers/" # only include coverage in "tests/" folder + plugins: # declare a new status context "api" + target: 35% # we always want 100% coverage here + paths: "autoload/SpaceVim/plugins/" # only include coverage in "tests/" folder + all: # declare a new status context "app" paths: "!tests/" # remove all files in "tests/" diff --git a/config/commands.vim b/config/commands.vim index 48ab64c3a..b60095b38 100644 --- a/config/commands.vim +++ b/config/commands.vim @@ -8,3 +8,5 @@ command! -nargs=* -complete=custom,zvim#util#complete_plugs Plugin :call zvim#ut command! -nargs=+ -complete=custom,zvim#util#complete_project OpenProject :call zvim#util#OpenProject() command! -nargs=* -complete=custom,SpaceVim#plugins#pmd#complete PMD :call SpaceVim#plugins#pmd#run() + +command! -nargs=0 A :call SpaceVim#plugins#a#alt() diff --git a/docs/_posts/2018-09-28-use-vim-as-a-python-ide.md b/docs/_posts/2018-09-28-use-vim-as-a-python-ide.md index fd306dba9..fed090847 100644 --- a/docs/_posts/2018-09-28-use-vim-as-a-python-ide.md +++ b/docs/_posts/2018-09-28-use-vim-as-a-python-ide.md @@ -62,7 +62,7 @@ pip install --user isort ### 工程文件跳转 -工程文件跳转,主要依赖 vim-projectionist, 需要在项目根目录加入 .projections.json 文件,标记源文件和测试文件之间的关系。 +SpaceVim 自带工程管理插件,可以识别项目根目录,自动跳转alternate文件。 ### 快速运行 diff --git a/docs/cn/layers/lang/vue.md b/docs/cn/layers/lang/vue.md index 2a8489ac7..b00cb4e63 100644 --- a/docs/cn/layers/lang/vue.md +++ b/docs/cn/layers/lang/vue.md @@ -21,7 +21,7 @@ lang: cn ## 功能特性 - 语法高亮 -- lsp 支持(需要 [lsp](/cn/layers/language-server-protocol/) 模块) +- lsp 支持(需要 [lsp](../../language-server-protocol/) 模块) ## 启用模块 diff --git a/docs/documentation.md b/docs/documentation.md index 1a3994e04..d4e5ece6c 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -1792,7 +1792,7 @@ Custom sign symbol: ### Managing projects -Projects in SpaceVim are managed by vim-projectionist and vim-rooter, vim-rooter will find the root of the project when a `.git` directory or a `.projections.json` file is encountered in the file tree. +SpaceVim will find the root of the project when a `.git` directory or a `.project_alt.json` file is encountered in the file tree. project manager commands start with `p`: diff --git a/test/plugin/a.vader b/test/plugin/a.vader new file mode 100644 index 000000000..922809363 --- /dev/null +++ b/test/plugin/a.vader @@ -0,0 +1,12 @@ +Execute ( SpaceVim plugin: a.vim ): + call SpaceVim#plugins#a#set_config_name('.projections.json') + 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('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' + unlet root +