From 2f55a1790fecbc808870f9b3c874c6bbab0f2d6a Mon Sep 17 00:00:00 2001 From: David Barnett Date: Fri, 23 Jan 2015 21:14:34 -0800 Subject: [PATCH 1/2] Fix vroom tests to not depend on executables installed on host --- vroom/autocmd.vroom | 15 +++++++++++++++ vroom/main.vroom | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/vroom/autocmd.vroom b/vroom/autocmd.vroom index 9582d8d..98633ce 100644 --- a/vroom/autocmd.vroom +++ b/vroom/autocmd.vroom @@ -1,10 +1,25 @@ 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 + | let formatters_by_name[formatter.name] = formatter + |endfor + :unlockvar formatters_by_name['clang-format'].IsAvailable + :function! formatters_by_name['clang-format'].IsAvailable() + | return 1 + |endfunction + + You can use the AutoFormatBuffer command to enable automatic code formatting when you save a file with a given filetype. diff --git a/vroom/main.vroom b/vroom/main.vroom index 61b993f..383f6c9 100644 --- a/vroom/main.vroom +++ b/vroom/main.vroom @@ -18,6 +18,22 @@ 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. + + :silent! call codefmt#DummyFunctionNameToForceFormatterInitialization() + :let formatters_by_name = {} + :for formatter in codefmtlib#GetFormatters() + | let formatters_by_name[formatter.name] = formatter + |endfor + :unlockvar formatters_by_name['clang-format'].IsAvailable + :function! formatters_by_name['clang-format'].IsAvailable() + | return 1 + |endfunction + :unlockvar formatters_by_name['gofmt'].IsAvailable + :function! formatters_by_name['gofmt'].IsAvailable() + | return 1 + |endfunction This plugin defines a :FormatCode command that can be used to reformat buffers. It can format C++ code using clang-format. From 3d900139c5692d967da9ecc69133064b0359e281 Mon Sep 17 00:00:00 2001 From: David Barnett Date: Fri, 23 Jan 2015 21:35:50 -0800 Subject: [PATCH 2/2] Add and use a codefmt#DisableIsAvailableChecksForTesting helper --- autoload/codefmt.vim | 30 +++++++++++++++++++++++++----- vroom/autocmd.vroom | 10 +--------- vroom/main.vroom | 15 ++------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index d1dc179..5b3cea4 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -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 @@ -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') @@ -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 @@ -272,7 +284,7 @@ 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 @@ -280,7 +292,7 @@ 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() ? ( @@ -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 diff --git a/vroom/autocmd.vroom b/vroom/autocmd.vroom index 98633ce..7130fb4 100644 --- a/vroom/autocmd.vroom +++ b/vroom/autocmd.vroom @@ -9,15 +9,7 @@ Install dependencies and setup maktaba: We'll stub out codefmt to not care whether certain executables are installed on your system. - :silent! call codefmt#DummyFunctionNameToForceFormatterInitialization() - :let formatters_by_name = {} - :for formatter in codefmtlib#GetFormatters() - | let formatters_by_name[formatter.name] = formatter - |endfor - :unlockvar formatters_by_name['clang-format'].IsAvailable - :function! formatters_by_name['clang-format'].IsAvailable() - | return 1 - |endfunction + :call codefmt#DisableIsAvailableChecksForTesting() You can use the AutoFormatBuffer command to enable automatic code formatting diff --git a/vroom/main.vroom b/vroom/main.vroom index 383f6c9..1d4c123 100644 --- a/vroom/main.vroom +++ b/vroom/main.vroom @@ -21,19 +21,8 @@ stub that out for vroom. We'll also stub it out to not care whether certain executables are installed on your system. - :silent! call codefmt#DummyFunctionNameToForceFormatterInitialization() - :let formatters_by_name = {} - :for formatter in codefmtlib#GetFormatters() - | let formatters_by_name[formatter.name] = formatter - |endfor - :unlockvar formatters_by_name['clang-format'].IsAvailable - :function! formatters_by_name['clang-format'].IsAvailable() - | return 1 - |endfunction - :unlockvar formatters_by_name['gofmt'].IsAvailable - :function! formatters_by_name['gofmt'].IsAvailable() - | return 1 - |endfunction + :call codefmt#DisableIsAvailableChecksForTesting() + This plugin defines a :FormatCode command that can be used to reformat buffers. It can format C++ code using clang-format.