Skip to content
Merged
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
30 changes: 25 additions & 5 deletions autoload/codefmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,23 @@ if !exists('s:gofmt')
endif


""
" Checks whether {formatter} is available.
" NOTE: If @function(#DisableIsAvailableChecksForTesting) has been called, skips
" the IsAvailable check and always returns true.
function! s:IsAvailable(formatter) abort
if get(s:, 'check_formatters_available', 1)
return a:formatter.IsAvailable()
endif
return 1
endfunction


""
" Detects whether a formatter has been defined for the current buffer/filetype.
function! codefmt#IsFormatterAvailable() abort
let l:formatters = copy(codefmtlib#GetFormatters())
let l:is_available = 'v:val.AppliesToBuffer() && v:val.IsAvailable()'
let l:is_available = 'v:val.AppliesToBuffer() && s:IsAvailable(v:val)'
return !empty(filter(l:formatters, l:is_available)) ||
\ !empty(get(b:, 'codefmt_formatter'))
endfunction
Expand All @@ -187,7 +199,7 @@ function! s:GetFormatter(...) abort
return
endif
let l:formatter = l:selected_formatters[0]
if !l:formatter.IsAvailable()
if !s:IsAvailable(l:formatter)
" Not available. Print setup instructions if possible.
let l:error = 'Formatter "%s" is not available.'
if has_key(l:formatter, 'setup_instructions')
Expand All @@ -199,7 +211,7 @@ function! s:GetFormatter(...) abort
else
" No explicit name, use default.
let l:default_formatters = filter(
\ copy(l:formatters), 'v:val.AppliesToBuffer() && v:val.IsAvailable()')
\ copy(l:formatters), 'v:val.AppliesToBuffer() && s:IsAvailable(v:val)')
if !empty(l:default_formatters)
let l:formatter = l:default_formatters[0]
else
Expand Down Expand Up @@ -272,15 +284,15 @@ endfunction
" @public
" Suitable for use as 'operatorfunc'; see |g@| for details.
" The type is ignored since formatting only works on complete lines.
function! codefmt#FormatMap(type) range
function! codefmt#FormatMap(type) range abort
call codefmt#FormatLines(line("'["), line("']"))
endfunction

""
" Generate the completion for supported formatters. Lists available formatters
" that apply to the current buffer first, then unavailable formatters that
" apply, then everything else.
function! codefmt#GetSupportedFormatters(ArgLead, CmdLine, CursorPos)
function! codefmt#GetSupportedFormatters(ArgLead, CmdLine, CursorPos) abort
let l:groups = [[], [], []]
for l:formatter in codefmtlib#GetFormatters()
let l:key = l:formatter.AppliesToBuffer() ? (
Expand All @@ -290,3 +302,11 @@ function! codefmt#GetSupportedFormatters(ArgLead, CmdLine, CursorPos)
return join(l:groups[0] + l:groups[1] + l:groups[2], "\n")
endfunction


""
" @private
" Bypasses FORMATTER.IsAvailable checks and assumes every formatter is available
" to avoid checking for executables on the path.
function! codefmt#DisableIsAvailableChecksForTesting() abort
let s:check_formatters_available = 0
endfunction
7 changes: 7 additions & 0 deletions vroom/autocmd.vroom
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
Install dependencies and setup maktaba:

:set nocompatible
:let g:repo = fnamemodify($VROOMFILE, ':p:h:h')
:execute 'source' g:repo . '/bootstrap.vim'
:call maktaba#syscall#SetUsableShellRegex('\v<shell\.vroomfaker$')
:filetype plugin on

We'll stub out codefmt to not care whether certain executables are installed on
your system.

:call codefmt#DisableIsAvailableChecksForTesting()


You can use the AutoFormatBuffer command to enable automatic code formatting
when you save a file with a given filetype.

Expand Down
5 changes: 5 additions & 0 deletions vroom/main.vroom
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ stub that out for vroom.
:endfunction
:call maktaba#test#Override('repeat#set', 'FakeRepeat')

We'll also stub it out to not care whether certain executables are installed on
your system.

:call codefmt#DisableIsAvailableChecksForTesting()


This plugin defines a :FormatCode command that can be used to reformat buffers.
It can format C++ code using clang-format.
Expand Down