diff --git a/README.md b/README.md index 59ed622..a272322 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-VERSION-all-deps.jar" ``` Make sure you have updated maktaba recently. Codefmt depends upon maktaba @@ -80,13 +82,21 @@ 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 -# Installing and 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 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. @@ -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](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](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.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 73178d2..0a3810b 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 codefmt#ShouldPerformIsAvailableChecks() && !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..618f0e7 --- /dev/null +++ b/autoload/codefmt/googlejava.vim @@ -0,0 +1,71 @@ +" 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-VERSION-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'), '\\\@= 3.6, then it will -also format java files. +also format java files, but google-java-format is the default. @clear % f(); @@ -101,7 +101,7 @@ also format java files. $ f(); :set filetype=java - :FormatCode + :FormatCode clang-format ! clang-format .* $ { "Cursor": 0 } $ f(); diff --git a/vroom/googlejava.vroom b/vroom/googlejava.vroom new file mode 100644 index 0000000..afea0fa --- /dev/null +++ b/vroom/googlejava.vroom @@ -0,0 +1,44 @@ +The built-in google-java formatter knows how to format Java BUILD files. If you +aren't familiar with basic codefmt usage yet, see main.vroom first. + +We'll set up codefmt and configure the vroom environment, then jump into some +examples. + + :source $VROOMDIR/setupvroom.vim + + :let g:repeat_calls = [] + :function FakeRepeat(...) + | 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 clang formatter by default, so the default +functionality is tested there. 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 .*