Skip to content

Use vim's builtin format mapping #121

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 2 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions autoload/codefmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,16 @@ function! codefmt#FormatMap(type) range abort
call codefmt#FormatLines(line("'["), line("']"))
endfunction

""
" @public
" To map the builtin |gq| command to invoke codefmt, set 'formatexpr' to call
" this function. Example: >
" set formatexpr=codefmt#FormatExpr()
" <
function! codefmt#FormatExpr() abort
call codefmt#FormatLines(v:lnum, v:lnum + v:count)
endfunction

""
" Generate the completion for supported formatters. Lists available formatters
" that apply to the current buffer first, then unavailable formatters that
Expand All @@ -260,6 +270,23 @@ function! codefmt#GetSupportedFormatters(ArgLead, CmdLine, CursorPos) abort
return join(l:groups[0] + l:groups[1] + l:groups[2], "\n")
endfunction

""
" Returns whether there is a default formatter available for the current
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this @public and regenerate the vimdoc?

It should have a sample of how to use it somewhere in the help docs, but for that part I'm happy to merge it w/o and add the docs after.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (two years later :)

" buffer.
function! codefmt#AvailableInCurrrentBuffer() abort
let l:formatters = s:registry.GetExtensions()
if !empty(get(b:, 'codefmt_formatter'))
let l:Predicate = {f -> f.name ==# b:codefmt_formatter}
else
let l:Predicate = {f -> f.AppliesToBuffer() && s:IsAvailable(f)}
endif
Comment on lines +278 to +282
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference: We haven't been using lambdas in any of our plugins yet, but I checked and they were originally added in 7.4.2044 from 2016. That's probably long enough ago. At some point I should probably declare a min supported vim version for the plugin and get it testing against that version, though.

for l:formatter in s:registry.GetExtensions()
if l:Predicate(l:formatter)
return 1
endif
endfor
return 0
endfunction

""
" @private
Expand Down
Loading