From 1b8c5dd443e855023f95e3be21372ded574902b5 Mon Sep 17 00:00:00 2001 From: Wang Shidong Date: Mon, 31 May 2021 23:32:48 +0800 Subject: [PATCH] Improve alternate file config (#3493) --- .SpaceVim.d/init.vim | 2 +- autoload/SpaceVim/plugins/a.vim | 12 ++++++------ docs/cn/documentation.md | 20 ++++++++++++++++++++ docs/documentation.md | 20 ++++++++++++++++++++ test/plugin/a.vader | 3 +-- 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/.SpaceVim.d/init.vim b/.SpaceVim.d/init.vim index 852a1f7a7..55e66b9ed 100644 --- a/.SpaceVim.d/init.vim +++ b/.SpaceVim.d/init.vim @@ -27,8 +27,8 @@ function! s:language_specified_mappings() abort \ 'Vader', \ '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(getcwd(), '.projections.json') +call SpaceVim#mapping#space#regesit_lang_mappings('vader', function('s:language_specified_mappings')) command! -nargs=1 IssueEdit call SpaceVim#dev#issuemanager#edit() command! -nargs=1 PullCreate call SpaceVim#dev#pull#create() command! -nargs=1 PullMerge call SpaceVim#dev#pull#merge() diff --git a/autoload/SpaceVim/plugins/a.vim b/autoload/SpaceVim/plugins/a.vim index ac15d0eda..209f3c34c 100644 --- a/autoload/SpaceVim/plugins/a.vim +++ b/autoload/SpaceVim/plugins/a.vim @@ -48,14 +48,11 @@ function! s:load_cache() abort endif endfunction - - " when this function is called, the project_config file name is changed, and " the project_config info is cleared. function! SpaceVim#plugins#a#set_config_name(path, name) abort let s:alternate_conf[a:path] = a:name endfunction - function! s:get_project_config(conf_file) abort call s:LOGGER.info('read context from: '. a:conf_file) let context = join(readfile(a:conf_file), "\n") @@ -77,9 +74,12 @@ endfunction function! SpaceVim#plugins#a#alt(request_parse,...) abort let type = get(a:000, 0, 'alternate') - let conf_file_path = SpaceVim#plugins#a#getConfigPath() - let file = s:FILE.unify_path(bufname('%'), ':.') - let alt = SpaceVim#plugins#a#get_alt(file, conf_file_path, a:request_parse, type) + if !exists('b:alternate_file_config') + let conf_file_path = SpaceVim#plugins#a#getConfigPath() + let file = s:FILE.unify_path(bufname('%'), ':.') + let alt = SpaceVim#plugins#a#get_alt(file, conf_file_path, a:request_parse, type) + else + endif if !empty(alt) exe 'e ' . alt else diff --git a/docs/cn/documentation.md b/docs/cn/documentation.md index 154767836..ba2579c1e 100644 --- a/docs/cn/documentation.md +++ b/docs/cn/documentation.md @@ -1925,6 +1925,26 @@ Denite/Unite 是一个强大的信息筛选浏览器,这类似于 Emacs 中的 } ``` +除了使用 `.project_alt.json` 文件以外,还可以在启动函数中设置 `b:alternate_file_config`, +例如: + +```vim +augroup myspacevim + autocmd! + autocmd BufNewFile,BufEnter *.c let b:alternate_file_config = { + \ "src/*.c" : { + \ "doc" : "docs/{}.md", + \ "alternate" : "include/{}.h", + \ } + \ } + autocmd BufNewFile,BufEnter *.h let b:alternate_file_config = { + \ "include/*.h" : { + \ "alternate" : "scr/{}.c", + \ } + \ } +augroup END +``` + ### 标签管理 在浏览代码时,通常需要给指定位置添加标签,方便快速跳转,在 SpaceVim diff --git a/docs/documentation.md b/docs/documentation.md index 66c7fd081..c6f68c168 100644 --- a/docs/documentation.md +++ b/docs/documentation.md @@ -2039,6 +2039,26 @@ here is an example of `.project_alt.json`: } ``` +instead of using `.project_alt.json`, `b:alternate_file_config` +can be used in bootstrap function, for example: + +```vim +augroup myspacevim + autocmd! + autocmd BufNewFile,BufEnter *.c let b:alternate_file_config = { + \ "src/*.c" : { + \ "doc" : "docs/{}.md", + \ "alternate" : "include/{}.h", + \ } + \ } + autocmd BufNewFile,BufEnter *.h let b:alternate_file_config = { + \ "include/*.h" : { + \ "alternate" : "scr/{}.c", + \ } + \ } +augroup END +``` + ### Bookmarks management Bookmarks manager is included in `tools` layer, to use following key bindings, you need to enable diff --git a/test/plugin/a.vader b/test/plugin/a.vader index fb62bd5cc..d0f33392b 100644 --- a/test/plugin/a.vader +++ b/test/plugin/a.vader @@ -1,6 +1,5 @@ Execute ( SpaceVim plugin: a.vim ): - call SpaceVim#plugins#a#set_config_name(getcwd(), '.projections.json') - let root = SpaceVim#plugins#a#getConfigPath() + let root = SpaceVim#plugins#a#get_root() AssertEqual SpaceVim#plugins#a#get_alt('docs/index.md', root, 1), 'docs/cn/index.md' AssertEqual SpaceVim#plugins#a#get_alt('docs/cn/index.md', root, 1), 'docs/index.md' AssertEqual SpaceVim#plugins#a#get_alt('docs/api/job.md', root, 1), 'docs/cn/api/job.md'