-
Notifications
You must be signed in to change notification settings - Fork 107
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
" 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (two years later :)