From 6050014420c4357f5029259d470b3a47846d4c9d Mon Sep 17 00:00:00 2001 From: Nils Leuzinger Date: Sat, 21 Apr 2018 05:05:28 +0200 Subject: [PATCH 1/5] Expose a method to list commands and scoped files --- autoload/projectionist.vim | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/autoload/projectionist.vim b/autoload/projectionist.vim index f5ba406..1dcbb22 100644 --- a/autoload/projectionist.vim +++ b/autoload/projectionist.vim @@ -239,6 +239,31 @@ function! s:match(file, pattern) abort return clean ==# match ? '' : clean endfunction +function! projectionist#list_project_files() abort + let found_files = {} + for [category, projection_variants] in items(projectionist#navigation_commands()) + let projections = [] + + for variant in projection_variants + call add(projections, variant[0] . projectionist#slash() . (variant[1] =~# '\*\*' + \ ? variant[1] : substitute(variant[1], '\*', '**/*', ''))) + endfor + + let files_in_category = [] + for projection in projections + if projection !~# '\*' + continue + endif + let glob = substitute(projection, '[^\/]*\ze\*\*[\/]\*', '', 'g') + let files_in_category += map(split(glob(glob), "\n"), '[s:match(v:val, projection), v:val]') + endfor + let found_files[category] = files_in_category + " let found_files[category] = projectionist#completion_filter(files_in_category, a:lead, '/') + endfor + + return found_files +endfunction + function! projectionist#query_raw(key, ...) abort let candidates = [] let file = a:0 ? a:1 : get(b:, 'projectionist_file', expand('%:p')) From 73c89f7dde440ae132c80a8752ea900eed1ce57b Mon Sep 17 00:00:00 2001 From: Nils Leuzinger Date: Sat, 21 Apr 2018 05:22:08 +0200 Subject: [PATCH 2/5] Add function to list files filtered by category --- autoload/projectionist.vim | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/autoload/projectionist.vim b/autoload/projectionist.vim index 1dcbb22..032002e 100644 --- a/autoload/projectionist.vim +++ b/autoload/projectionist.vim @@ -242,28 +242,32 @@ endfunction function! projectionist#list_project_files() abort let found_files = {} for [category, projection_variants] in items(projectionist#navigation_commands()) - let projections = [] - - for variant in projection_variants - call add(projections, variant[0] . projectionist#slash() . (variant[1] =~# '\*\*' - \ ? variant[1] : substitute(variant[1], '\*', '**/*', ''))) - endfor - - let files_in_category = [] - for projection in projections - if projection !~# '\*' - continue - endif - let glob = substitute(projection, '[^\/]*\ze\*\*[\/]\*', '', 'g') - let files_in_category += map(split(glob(glob), "\n"), '[s:match(v:val, projection), v:val]') - endfor - let found_files[category] = files_in_category + let found_files[category] = projectionist#list_files_for_category(category) " let found_files[category] = projectionist#completion_filter(files_in_category, a:lead, '/') endfor return found_files endfunction +function! projectionist#list_files_for_category(category) abort + let projection_variants = projectionist#navigation_commands()[a:category] + let projections = [] + for variant in projection_variants + call add(projections, variant[0] . projectionist#slash() . (variant[1] =~# '\*\*' + \ ? variant[1] : substitute(variant[1], '\*', '**/*', ''))) + endfor + + let files_in_category = [] + for projection in projections + if projection !~# '\*' + continue + endif + let glob = substitute(projection, '[^\/]*\ze\*\*[\/]\*', '', 'g') + let files_in_category += map(split(glob(glob), "\n"), '[s:match(v:val, projection), v:val]') + endfor + return files_in_category +endfunction + function! projectionist#query_raw(key, ...) abort let candidates = [] let file = a:0 ? a:1 : get(b:, 'projectionist_file', expand('%:p')) From 5f06128bc51b26d0841b2e8e8836094a47ca81e0 Mon Sep 17 00:00:00 2001 From: Nils Leuzinger Date: Sat, 21 Apr 2018 06:02:31 +0200 Subject: [PATCH 3/5] Add function to list files filtered by projection --- autoload/projectionist.vim | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/autoload/projectionist.vim b/autoload/projectionist.vim index 032002e..efe7ab8 100644 --- a/autoload/projectionist.vim +++ b/autoload/projectionist.vim @@ -253,19 +253,41 @@ function! projectionist#list_files_for_category(category) abort let projection_variants = projectionist#navigation_commands()[a:category] let projections = [] for variant in projection_variants - call add(projections, variant[0] . projectionist#slash() . (variant[1] =~# '\*\*' - \ ? variant[1] : substitute(variant[1], '\*', '**/*', ''))) + call add(projections, s:get_projection_from_variant(variant)) + endfor + return s:files_in_projections(projections) +endfunction + +function! projectionist#list_files_for_projection(projection) abort + for [category, projection_variants] in items(projectionist#navigation_commands()) + for variant in projection_variants + if (variant[1] !=# a:projection) + continue + else + let projection = s:get_projection_from_variant(variant) + return {category: s:files_in_projections([projection])} + endif + endfor endfor + return {} +endfunction + +function! s:get_projection_from_variant(variant) abort + let projection = a:variant[0] . projectionist#slash() . (a:variant[1] =~# '\*\*' + \ ? a:variant[1] : substitute(a:variant[1], '\*', '**/*', '')) + return projection +endfunction - let files_in_category = [] - for projection in projections +function! s:files_in_projections(projections) + let files_in_projections = [] + for projection in a:projections if projection !~# '\*' continue endif let glob = substitute(projection, '[^\/]*\ze\*\*[\/]\*', '', 'g') - let files_in_category += map(split(glob(glob), "\n"), '[s:match(v:val, projection), v:val]') + let files_in_projections += map(split(glob(glob), "\n"), '[s:match(v:val, projection), v:val]') endfor - return files_in_category + return files_in_projections endfunction function! projectionist#query_raw(key, ...) abort From 3166d8ad249917bb02d0574d67724c86769db650 Mon Sep 17 00:00:00 2001 From: Nils Leuzinger Date: Sat, 21 Apr 2018 06:19:11 +0200 Subject: [PATCH 4/5] Put duplicate code parts into their own functions Remove function to list projections --- autoload/projectionist.vim | 69 +++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 38 deletions(-) diff --git a/autoload/projectionist.vim b/autoload/projectionist.vim index efe7ab8..9aeeaad 100644 --- a/autoload/projectionist.vim +++ b/autoload/projectionist.vim @@ -241,50 +241,29 @@ endfunction function! projectionist#list_project_files() abort let found_files = {} - for [category, projection_variants] in items(projectionist#navigation_commands()) - let found_files[category] = projectionist#list_files_for_category(category) - " let found_files[category] = projectionist#completion_filter(files_in_category, a:lead, '/') + for [type, projection_variants] in items(projectionist#navigation_commands()) + let found_files[type] = projectionist#list_files_for_type(type) endfor - return found_files endfunction -function! projectionist#list_files_for_category(category) abort - let projection_variants = projectionist#navigation_commands()[a:category] - let projections = [] - for variant in projection_variants - call add(projections, s:get_projection_from_variant(variant)) - endfor +function! projectionist#list_files_for_type(type) abort + let nav_commands = projectionist#navigation_commands() + if !has_key(nav_commands, a:type) + return [] + endif + let projection_variants = nav_commands[a:type] + let projections = s:get_projections_from_variants(projection_variants) return s:files_in_projections(projections) endfunction -function! projectionist#list_files_for_projection(projection) abort - for [category, projection_variants] in items(projectionist#navigation_commands()) - for variant in projection_variants - if (variant[1] !=# a:projection) - continue - else - let projection = s:get_projection_from_variant(variant) - return {category: s:files_in_projections([projection])} - endif - endfor - endfor - return {} -endfunction - -function! s:get_projection_from_variant(variant) abort - let projection = a:variant[0] . projectionist#slash() . (a:variant[1] =~# '\*\*' - \ ? a:variant[1] : substitute(a:variant[1], '\*', '**/*', '')) - return projection -endfunction - -function! s:files_in_projections(projections) +function! s:files_in_projections(projections) abort let files_in_projections = [] for projection in a:projections if projection !~# '\*' continue endif - let glob = substitute(projection, '[^\/]*\ze\*\*[\/]\*', '', 'g') + let glob = s:sanitize_glob(projection) let files_in_projections += map(split(glob(glob), "\n"), '[s:match(v:val, projection), v:val]') endfor return files_in_projections @@ -544,11 +523,7 @@ function! projectionist#navigation_commands() abort endfunction function! s:open_projection(cmd, variants, ...) abort - let formats = [] - for variant in a:variants - call add(formats, variant[0] . projectionist#slash() . (variant[1] =~# '\*\*' - \ ? variant[1] : substitute(variant[1], '\*', '**/*', ''))) - endfor + let formats = s:get_projections_from_variants(a:variants) if a:0 && a:1 ==# '&' let s:last_formats = formats return '' @@ -578,6 +553,20 @@ function! s:open_projection(cmd, variants, ...) abort \ fnameescape(fnamemodify(target, ':~:.')) endfunction +function! s:get_projection_from_variant(variant) abort + let projection = a:variant[0] . projectionist#slash() . (a:variant[1] =~# '\*\*' + \ ? a:variant[1] : substitute(a:variant[1], '\*', '**/*', '')) + return projection +endfunction + +function! s:get_projections_from_variants(variants) abort + let projections = [] + for variant in a:variants + call add(projections, s:get_projection_from_variant(variant)) + endfor + return projections +endfunction + function! s:projection_complete(lead, cmdline, _) abort execute matchstr(a:cmdline, '\a\@ Date: Tue, 24 Apr 2018 04:27:48 +0200 Subject: [PATCH 5/5] Add documentation for exposed functions Fix help coloring Fix duplicate helptag --- doc/projectionist.txt | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/doc/projectionist.txt b/doc/projectionist.txt index fbb87a8..5b3e0fb 100644 --- a/doc/projectionist.txt +++ b/doc/projectionist.txt @@ -191,4 +191,38 @@ You can also call *projectionist#path()* to get the root of the innermost set of projections, which is useful for implementing commands like |projectionist-:Cd|. +FUNCTIONS *projectionist-functions* + +Projectionist.vim exposes some functions to make interoperability with +other plugins easier. + + + *projectionist#list_project_files()* +projectionist#list_project_files() + Returns a dictionary of files belonging to the current + project, keyed on projection types. The dictionary has + the following form: +> + {'type1': [['file1', '/home/user/full/path/to/file1'], + \ ['file2', '/home/user/full/path/to/file2']], + \ 'type2': [['file1', '/home/user/full/path/to/file1'], + \ ['file2', '/home/user/full/path/to/file4']]} +< + The short versions of the filenames (`file1`, `file2` etc.) are + there for use with |projectionist-commands|. + For instance, `:Etype1 file1` would edit `file1`. + + + *projectionist#list_files_for_type()* +projectionist#list_files_for_type({type}) + Returns the list of files that belong to the current project and + are of type {type}. The list has the following form: +> + [['file1', '/home/user/full/path/to/file1'], + \ ['file2', '/home/user/full/path/to/file2']] +< + If {type} is not an existing projection type, an empty list is + returned. If {type} exists, calling this function is equivalent + to calling `projectionist#list_project_files()[{type}]`. + vim:tw=78:et:ft=help:norl: