Skip to content

Expose methods for listing files #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions autoload/projectionist.vim
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,36 @@ function! s:match(file, pattern) abort
return clean ==# match ? '' : clean
endfunction

function! projectionist#list_project_files() abort
let found_files = {}
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_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! s:files_in_projections(projections) abort
let files_in_projections = []
for projection in a:projections
if projection !~# '\*'
continue
endif
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
endfunction

function! projectionist#query_raw(key, ...) abort
let candidates = []
let file = a:0 ? a:1 : get(b:, 'projectionist_file', expand('%:p'))
Expand Down Expand Up @@ -493,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 ''
Expand Down Expand Up @@ -527,20 +553,38 @@ 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\@<![' . join(keys(s:prefixes), '') . ']\w\+') . ' &'
let results = []
for format in s:last_formats
if format !~# '\*'
continue
endif
let glob = substitute(format, '[^\/]*\ze\*\*[\/]\*', '', 'g')
let glob = s:sanitize_glob(format)
let results += map(split(glob(glob), "\n"), 's:match(v:val, format)')
endfor
call s:uniq(results)
return projectionist#completion_filter(results, a:lead, '/')
endfunction

function! s:sanitize_glob(glob) abort
return substitute(a:glob, '[^\/]*\ze\*\*[\/]\*', '', 'g')
endfunction

" Section: :A

function! s:jumpopt(file) abort
Expand Down
34 changes: 34 additions & 0 deletions doc/projectionist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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: