From cad35273c0d57b1562fe222b4524acafadc3034a Mon Sep 17 00:00:00 2001 From: Josh Hoak Date: Thu, 26 Jan 2017 07:25:56 -0800 Subject: [PATCH 1/6] Add a google-java formatter. Fixes: https://github.com/google/vim-codefmt/issues/51 This commit adds a google-java-formatter. Some notes: - Mostly a port from the internal version. - I fixed a bug along the way, where if you didn't have clangformat installed, codefmt would error-out during tab-complete. - I fixed a typo in buildifier. - And some minor updates to the README --- README.md | 27 ++++++++---- autoload/codefmt/clangformat.vim | 4 ++ autoload/codefmt/googlejava.vim | 72 ++++++++++++++++++++++++++++++++ instant/flags.vim | 6 +++ plugin/register.vim | 1 + vroom/buildifier.vroom | 2 +- vroom/googlejava.vroom | 57 +++++++++++++++++++++++++ 7 files changed, 159 insertions(+), 10 deletions(-) create mode 100644 autoload/codefmt/googlejava.vim create mode 100644 vroom/googlejava.vroom diff --git a/README.md b/README.md index 59ed622..c1d73ea 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ helpfiles in the `doc/` directory. The helpfiles are also available via * Go (gofmt) * [GN](https://www.chromium.org/developers/gn-build-configuration) (gn) * HTML (js-beautify) +* Java (google-java-format or clang-format) * JavaScript (clang-format) * JSON (js-beautify) * Proto (clang-format) @@ -62,6 +63,7 @@ call vundle#end() call glaive#Install() " Optional: Enable codefmt's default mappings on the = prefix. Glaive codefmt plugin[mappings] +Glaive codefmt google_java_executable="java -jar /path/to/google-java-format-1.2-all-deps.jar" ``` Make sure you have updated maktaba recently. Codefmt depends upon maktaba @@ -80,11 +82,19 @@ augroup autoformat_settings autocmd FileType go AutoFormatBuffer gofmt autocmd FileType gn AutoFormatBuffer gn autocmd FileType html,css,json AutoFormatBuffer js-beautify + autocmd FileType java AutoFormatBuffer google-java-format autocmd FileType python AutoFormatBuffer yapf " Alternative: autocmd FileType python AutoFormatBuffer autopep8 augroup END ``` +# Configuring formatters. + +Most formatters have some options available that can be configured via +[Glaive](https://www.github.com/google/vim-glaive) +You can get a quick view of all codefmt flags by executing `:Glaive codefmt`, or +start typing flag names and use tab completion. See `:help Glaive` for usage +details. # Installing and configuring formatters @@ -99,31 +109,28 @@ trigger formatters via key mappings and/or autocommand hooks. See vroom/main.vroom to learn more about formatting features, and see vroom/FORMATTER-NAME.vroom to learn more about usage for individual formatters. -Most formatters have some options available that can be configured via Glaive. -You can get a quick view of all codefmt flags by executing `:Glaive codefmt`, or -start typing flag names and use tab completion. See `:help Glaive` for usage -details. - ## Creating a New Formatter Assume a filetype `myft` and a formatter called `MyFormatter`. Our detailed guide to creating a formatter [lives here](https://github.com/google/vim-codefmt/wiki/Formatter-Integration-Guide). -* Create an issue for your new formatter and discuss! Once there's consensus, - continue onward. +* Create an issue for your new formatter and discuss! * Create a new file in `autoload/codefmt/myformatter.vim` See `autoload/codefmt/buildifier.vim for an example. This is where all the logic for formatting goes. -* Register the formatter with: +* Register the formatter in + [plugin/register.vim](https://github.com/google/vim-codefmt/blob/master/plugin/register.vim) + with: ```vim call s:registry.AddExtension(codefmt#myformatter#GetFormatter()) ``` -* Create a flag in instant/flags.vim +* Create a flag in + [instant/flags.vim](https://github.com/Kashomon/vim-codefmt/blob/master/instant/flags.vim) ```vim "" @@ -134,6 +141,8 @@ here](https://github.com/google/vim-codefmt/wiki/Formatter-Integration-Guide). * Create a [vroom](https://github.com/google/vim-vroom) test named `vroom/myformatter.vroom` to ensure your formatter works properly. +* Update the README.md to mention your new filetype! + That's it! Of course, the complicated step is in the details of `myformatter.vim`. diff --git a/autoload/codefmt/clangformat.vim b/autoload/codefmt/clangformat.vim index 73178d2..1fa1227 100644 --- a/autoload/codefmt/clangformat.vim +++ b/autoload/codefmt/clangformat.vim @@ -19,6 +19,10 @@ let s:plugin = maktaba#plugin#Get('codefmt') function! s:ClangFormatHasAtLeastVersion(minimum_version) abort if !exists('s:clang_format_version') let l:executable = s:plugin.Flag('clang_format_executable') + if !executable(l:executable) + return 0 + endif + let l:version_output = \ maktaba#syscall#Create([l:executable, '--version']).Call().stdout let l:version_string = matchstr(l:version_output, '\v\d+(.\d+)+') diff --git a/autoload/codefmt/googlejava.vim b/autoload/codefmt/googlejava.vim new file mode 100644 index 0000000..fc74b78 --- /dev/null +++ b/autoload/codefmt/googlejava.vim @@ -0,0 +1,72 @@ +" Copyright 2017 Google Inc. All rights reserved. +" +" Licensed under the Apache License, Version 2.0 (the "License"); +" you may not use this file except in compliance with the License. +" You may obtain a copy of the License at +" +" http://www.apache.org/licenses/LICENSE-2.0 +" +" Unless required by applicable law or agreed to in writing, software +" distributed under the License is distributed on an "AS IS" BASIS, +" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +" See the License for the specific language governing permissions and +" limitations under the License. + + +let s:plugin = maktaba#plugin#Get('codefmt') + +"" +" @private +" Formatter: google-java-format +function! codefmt#googlejava#GetFormatter() abort + let l:formatter = { + \ 'name': 'google-java-format', + \ 'setup_instructions': 'Install google-java formatter ' . + \ "(https://github.com/google/google-java-format). \n" . + \ 'Enable with "Glaive codefmt google_java_executable=' . + \ '"java -jar /path/to/google-java-format-1.2-all-deps.jar" ' . + \ 'in your vimrc' } + + function l:formatter.IsAvailable() abort + let l:exec = s:plugin.Flag('google_java_executable') + if executable(l:exec) + return 1 + elseif !empty(l:exec) && l:exec isnot# 'google-java-format' + " The user has specified a custom formatter command. Hope it works. + " /shrug. + return 1 + else + return 0 + endif + endfunction + + function l:formatter.AppliesToBuffer() abort + return &filetype is# 'java' + endfunction + + "" + " Reformat the current buffer using java-format, only targeting {ranges}. + function l:formatter.FormatRanges(ranges) abort + if empty(a:ranges) + return + endif + " Split the command on spaces, except when there's a proceeding \ + let l:cmd = split(s:plugin.Flag('google_java_executable'), '\\\@ + | call add(g:repeat_calls, a:000) + :endfunction + :call maktaba#test#Override('repeat#set', 'FakeRepeat') + + :call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0) + + +The google-java formatter expects a google-java executable to be installed on +your system. + + % class Foo { public String getFoo() { return "bar"; } } + :FormatCode google-java-format + ! google-java-format .* + $ class Foo { + $ public String getFoo() { + $ return "bar"; + $ } + $ } + +The name or path of the google-java executable can be configured via the +google_java_executable flag if the default of "google-java" doesn't work. + + :Glaive codefmt google_java_executable='java -jar /path/to/google-java.jar' + :FormatCode google-java-format + ! java -jar /path/to/google-java.jar .* + $ class Foo { + $ public String getFoo() { + $ return "bar"; + $ } + $ } + :Glaive codefmt google_java_executable='google-java-format' + +The java filetype will use the google-java formatter by default. + + @clear + % class Foo { public String getFoo() { return "bar"; } } + + :set filetype=java + :FormatCode + ! google-java-format .* + $ class Foo { + $ public String getFoo() { + $ return "bar"; + $ } + $ } + + :set filetype= From 819d9a0b94dfdfe4e9f8994e2aa9f16630bb2ee3 Mon Sep 17 00:00:00 2001 From: Josh Hoak Date: Fri, 27 Jan 2017 07:45:29 -0800 Subject: [PATCH 2/6] Respond to comments. --- README.md | 6 +++--- autoload/codefmt/clangformat.vim | 5 ++++- autoload/codefmt/googlejava.vim | 5 ++--- vroom/clangformat.vroom | 8 ++++++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c1d73ea..324f837 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ augroup autoformat_settings augroup END ``` -# Configuring formatters. +# Configuring formatters Most formatters have some options available that can be configured via [Glaive](https://www.github.com/google/vim-glaive) @@ -96,7 +96,7 @@ You can get a quick view of all codefmt flags by executing `:Glaive codefmt`, or start typing flag names and use tab completion. See `:help Glaive` for usage details. -# Installing and configuring formatters +# Installing formatters Codefmt defines several built-in formatters. The easiest way to see the list of available formatters is via tab completion: Type `:FormatCode ` in vim. @@ -130,7 +130,7 @@ here](https://github.com/google/vim-codefmt/wiki/Formatter-Integration-Guide). ``` * Create a flag in - [instant/flags.vim](https://github.com/Kashomon/vim-codefmt/blob/master/instant/flags.vim) + [instant/flags.vim](https://github.com/google/vim-codefmt/blob/master/instant/flags.vim) ```vim "" diff --git a/autoload/codefmt/clangformat.vim b/autoload/codefmt/clangformat.vim index 1fa1227..730cdd7 100644 --- a/autoload/codefmt/clangformat.vim +++ b/autoload/codefmt/clangformat.vim @@ -15,11 +15,14 @@ let s:plugin = maktaba#plugin#Get('codefmt') +function! codefmt#clangformat#IsExecutable(exec) abort + return executable(a:exec) +endfunction function! s:ClangFormatHasAtLeastVersion(minimum_version) abort if !exists('s:clang_format_version') let l:executable = s:plugin.Flag('clang_format_executable') - if !executable(l:executable) + if !codefmt#clangformat#IsExecutable(l:executable) return 0 endif diff --git a/autoload/codefmt/googlejava.vim b/autoload/codefmt/googlejava.vim index fc74b78..618f0e7 100644 --- a/autoload/codefmt/googlejava.vim +++ b/autoload/codefmt/googlejava.vim @@ -24,7 +24,7 @@ function! codefmt#googlejava#GetFormatter() abort \ 'setup_instructions': 'Install google-java formatter ' . \ "(https://github.com/google/google-java-format). \n" . \ 'Enable with "Glaive codefmt google_java_executable=' . - \ '"java -jar /path/to/google-java-format-1.2-all-deps.jar" ' . + \ '"java -jar /path/to/google-java-format-VERSION-all-deps.jar" ' . \ 'in your vimrc' } function l:formatter.IsAvailable() abort @@ -56,7 +56,7 @@ function! codefmt#googlejava#GetFormatter() abort call maktaba#ensure#IsNumber(l:startline) call maktaba#ensure#IsNumber(l:endline) endfor - let l:ranges_str = join(map(a:ranges, 'v:val[0] . ":" . v:val[1]'), ',') + let l:ranges_str = join(map(copy(a:ranges), 'v:val[0] . ":" . v:val[1]'), ',') let l:cmd += ['--lines', l:ranges_str, '-'] let l:input = join(getline(1, line('$')), "\n") @@ -65,7 +65,6 @@ function! codefmt#googlejava#GetFormatter() abort call maktaba#buffer#Overwrite(1, line('$'), l:formatted) endfunction - let s:google_java_format = l:formatter return l:formatter endfunction diff --git a/vroom/clangformat.vroom b/vroom/clangformat.vroom index 05c67bd..3ec033f 100644 --- a/vroom/clangformat.vroom +++ b/vroom/clangformat.vroom @@ -12,7 +12,11 @@ briefly. :function FakeRepeat(...) | call add(g:repeat_calls, a:000) :endfunction + :function ClangExec(...) + | return 1 + :endfunction :call maktaba#test#Override('repeat#set', 'FakeRepeat') + :call maktaba#test#Override('codefmt#clangformat#IsExecutable', 'ClangExec') :call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0) @@ -77,7 +81,7 @@ You can format any buffer with clang-format specifying the formatter explicitly. Several filetypes will use the clang-format formatter by default: c, cpp, javascript, and proto. If the version of clang-format is >= 3.6, then it will -also format java files. +also format java files, but google-java-format is the default. @clear % f(); @@ -101,7 +105,7 @@ also format java files. $ f(); :set filetype=java - :FormatCode + :FormatCode clang-format ! clang-format .* $ { "Cursor": 0 } $ f(); From 5ae712b4c5b9334abbbcadeab9b6a272a1516c1e Mon Sep 17 00:00:00 2001 From: Josh Hoak Date: Fri, 27 Jan 2017 18:49:14 -0800 Subject: [PATCH 3/6] Fix clangformat more elegantly. --- autoload/codefmt.vim | 10 +++++++++- autoload/codefmt/clangformat.vim | 5 +---- vroom/clangformat.vroom | 4 ---- vroom/jsbeautify.vroom | 7 ------- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index 22758b7..49999fb 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -105,7 +105,7 @@ endfunction " @function(#SetWhetherToPerformIsAvailableChecksForTesting), skips the " IsAvailable check and always returns true. function! s:IsAvailable(formatter) abort - if get(s:, 'check_formatters_available', 1) + if codefmt#ShouldPerformIsAvailableChecks() return a:formatter.IsAvailable() endif return 1 @@ -253,6 +253,14 @@ function! codefmt#GetSupportedFormatters(ArgLead, CmdLine, CursorPos) abort endfunction +"" +" @private +" Returns whether to perform Availability checks, which is normall set for +" testing. Defaults to 1 (enable availablity checks). +function! codefmt#ShouldPerformIsAvailableChecks() abort + return get(s:, 'check_formatters_available', 1) +endfunction + "" " @private " Configures whether codefmt should bypass FORMATTER.IsAvailable checks and diff --git a/autoload/codefmt/clangformat.vim b/autoload/codefmt/clangformat.vim index 730cdd7..0a3810b 100644 --- a/autoload/codefmt/clangformat.vim +++ b/autoload/codefmt/clangformat.vim @@ -15,14 +15,11 @@ let s:plugin = maktaba#plugin#Get('codefmt') -function! codefmt#clangformat#IsExecutable(exec) abort - return executable(a:exec) -endfunction function! s:ClangFormatHasAtLeastVersion(minimum_version) abort if !exists('s:clang_format_version') let l:executable = s:plugin.Flag('clang_format_executable') - if !codefmt#clangformat#IsExecutable(l:executable) + if codefmt#ShouldPerformIsAvailableChecks() && !executable(l:executable) return 0 endif diff --git a/vroom/clangformat.vroom b/vroom/clangformat.vroom index 3ec033f..2fc012d 100644 --- a/vroom/clangformat.vroom +++ b/vroom/clangformat.vroom @@ -12,11 +12,7 @@ briefly. :function FakeRepeat(...) | call add(g:repeat_calls, a:000) :endfunction - :function ClangExec(...) - | return 1 - :endfunction :call maktaba#test#Override('repeat#set', 'FakeRepeat') - :call maktaba#test#Override('codefmt#clangformat#IsExecutable', 'ClangExec') :call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0) diff --git a/vroom/jsbeautify.vroom b/vroom/jsbeautify.vroom index e7022ed..921073d 100644 --- a/vroom/jsbeautify.vroom +++ b/vroom/jsbeautify.vroom @@ -61,13 +61,6 @@ javascript, json, html and css. @clear % f(); - :set filetype=javascript - :FormatCode - ! clang-format --version .* - $ clang-format version 3.3.0 (tags/testing) - ! clang-format .* - $ f(); - :set filetype=json :FormatCode ! js-beautify .* From 74ae5b2940facb71e6a7ae0f5fcfc690be5d9018 Mon Sep 17 00:00:00 2001 From: Josh Hoak Date: Fri, 27 Jan 2017 18:53:15 -0800 Subject: [PATCH 4/6] Fix google-java by removing a test. clang-format is, apparently, the default java formatter. ok --- vroom/googlejava.vroom | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/vroom/googlejava.vroom b/vroom/googlejava.vroom index e4885f4..afea0fa 100644 --- a/vroom/googlejava.vroom +++ b/vroom/googlejava.vroom @@ -40,18 +40,5 @@ google_java_executable flag if the default of "google-java" doesn't work. $ } :Glaive codefmt google_java_executable='google-java-format' -The java filetype will use the google-java formatter by default. - - @clear - % class Foo { public String getFoo() { return "bar"; } } - - :set filetype=java - :FormatCode - ! google-java-format .* - $ class Foo { - $ public String getFoo() { - $ return "bar"; - $ } - $ } - - :set filetype= +The java filetype will use the clang formatter by default, so the default +functionality is tested there. From 3c074fde7b19beecdbe80496eabf9126e734f002 Mon Sep 17 00:00:00 2001 From: Josh Hoak Date: Fri, 27 Jan 2017 19:51:25 -0800 Subject: [PATCH 5/6] Update readme with short lynx --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 324f837..ba91145 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ here](https://github.com/google/vim-codefmt/wiki/Formatter-Integration-Guide). logic for formatting goes. * Register the formatter in - [plugin/register.vim](https://github.com/google/vim-codefmt/blob/master/plugin/register.vim) + [plugin/register.vim](plugin/register.vim) with: ```vim @@ -130,7 +130,7 @@ here](https://github.com/google/vim-codefmt/wiki/Formatter-Integration-Guide). ``` * Create a flag in - [instant/flags.vim](https://github.com/google/vim-codefmt/blob/master/instant/flags.vim) + [instant/flags.vim](instant/flags.vim) ```vim "" From 1e5ad7a8ca383fbb03aa131bc87637296cf1b936 Mon Sep 17 00:00:00 2001 From: Josh Hoak Date: Fri, 27 Jan 2017 19:54:55 -0800 Subject: [PATCH 6/6] Address comments. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ba91145..a272322 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ call vundle#end() call glaive#Install() " Optional: Enable codefmt's default mappings on the = prefix. Glaive codefmt plugin[mappings] -Glaive codefmt google_java_executable="java -jar /path/to/google-java-format-1.2-all-deps.jar" +Glaive codefmt google_java_executable="java -jar /path/to/google-java-format-VERSION-all-deps.jar" ``` Make sure you have updated maktaba recently. Codefmt depends upon maktaba