diff --git a/README.md b/README.md index cf5bf1f..1d3ccf3 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ helpfiles in the `doc/` directory. The helpfiles are also available via * [Clojure](https://clojure.org/) ([zprint](https://github.com/kkinnear/zprint)) * CSS, Sass, SCSS, Less (js-beautify) * Dart (dartfmt) +* Fish ([fish_indent](https://fishshell.com/docs/current/commands.html#fish_indent)) * Go (gofmt) * [GN](https://www.chromium.org/developers/gn-build-configuration) (gn) * HTML (js-beautify) diff --git a/autoload/codefmt.vim b/autoload/codefmt.vim index 9624e14..fd6a3b5 100644 --- a/autoload/codefmt.vim +++ b/autoload/codefmt.vim @@ -30,6 +30,7 @@ " * c, cpp, proto, javascript, typescript: clang-format " * clojure: zprint " * dart: dartfmt +" * fish: fish_indent " * gn: gn " * go: gofmt " * python: autopep8, yapf diff --git a/autoload/codefmt/fish_indent.vim b/autoload/codefmt/fish_indent.vim new file mode 100644 index 0000000..795b3b4 --- /dev/null +++ b/autoload/codefmt/fish_indent.vim @@ -0,0 +1,48 @@ +" Copyright 2020 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') + + +function! codefmt#fish_indent#GetFormatter() abort + let l:formatter = { + \ 'name': 'fish_indent', + \ 'setup_instructions': 'Install fish_indent (https://fishshell.com/docs/current/commands.html#fish_indent)' . + \ ' and configure the fish_indent_executable flag'} + + function l:formatter.IsAvailable() abort + return executable(s:plugin.Flag('fish_indent_executable')) + endfunction + + function l:formatter.AppliesToBuffer() abort + return &filetype is# 'fish' + endfunction + + "" + " Reformat the current buffer with fish_indent or the binary named in + " @flag(fish_indent_executable), only targeting the range between {startline} + " and {endline}. + function l:formatter.FormatRange(startline, endline) abort + let l:cmd = [ s:plugin.Flag('fish_indent_executable') ] + call maktaba#ensure#IsNumber(a:startline) + call maktaba#ensure#IsNumber(a:endline) + " fish_indent does not support range formatting yet: + " https://github.com/fish-shell/fish-shell/issues/6490 + call codefmt#formatterhelpers#AttemptFakeRangeFormatting( + \ a:startline, a:endline, l:cmd) + endfunction + + return l:formatter +endfunction diff --git a/doc/codefmt.txt b/doc/codefmt.txt index 4f55739..b9a219e 100644 --- a/doc/codefmt.txt +++ b/doc/codefmt.txt @@ -111,6 +111,10 @@ The path to the zprint executable. Typically this is one of the native images installed as zprint. Default: 'zprint' ` + *codefmt:fish_indent_executable* +The path to the fish_indent executable. +Default: 'fish_indent' ` + *codefmt:plugin[autocmds]* Configures whether plugin/autocmds.vim should be loaded. Default: 1 ` @@ -154,6 +158,7 @@ The current list of defaults by filetype is: * c, cpp, proto, javascript, typescript: clang-format * clojure: zprint * dart: dartfmt + * fish: fish_indent * gn: gn * go: gofmt * python: autopep8, yapf diff --git a/instant/flags.vim b/instant/flags.vim index 865082c..6ddb884 100644 --- a/instant/flags.vim +++ b/instant/flags.vim @@ -128,7 +128,6 @@ call s:plugin.Flag('rustfmt_options', []) " The path to the rustfmt executable. call s:plugin.Flag('rustfmt_executable', 'rustfmt') - "" " @private " This is declared above zprint_options to avoid interfering with vimdoc parsing @@ -148,3 +147,7 @@ call s:plugin.Flag('zprint_options', function('s:ZprintOptions')) " images (zprintl or zprintm) from https://github.com/kkinnear/zprint/releases " installed as zprint. call s:plugin.Flag('zprint_executable', 'zprint') + +"" +" The path to the fish_indent executable. +call s:plugin.Flag('fish_indent_executable', 'fish_indent') diff --git a/plugin/register.vim b/plugin/register.vim index 66486b0..91b5c52 100644 --- a/plugin/register.vim +++ b/plugin/register.vim @@ -36,3 +36,4 @@ call s:registry.AddExtension(codefmt#buildifier#GetFormatter()) call s:registry.AddExtension(codefmt#googlejava#GetFormatter()) call s:registry.AddExtension(codefmt#shfmt#GetFormatter()) call s:registry.AddExtension(codefmt#zprint#GetFormatter()) +call s:registry.AddExtension(codefmt#fish_indent#GetFormatter()) diff --git a/vroom/fish_indent.vroom b/vroom/fish_indent.vroom new file mode 100644 index 0000000..9681d33 --- /dev/null +++ b/vroom/fish_indent.vroom @@ -0,0 +1,96 @@ +The built-in fish_indent knows how to format fish code. +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) + +You can format any buffer with fish_indent specifying the formatter explicitly. + + @clear + % if test 42 -eq $truth; echo '42 is truth'; else; echo 'I do not know what to believe'; end + + :FormatCode fish_indent + ! fish_indent .*2>.* + $ if test 42 -eq $truth + $ echo '42 is truth' + $ else + $ echo 'I do not know what to believe' + $ end + if test 42 -eq $truth + echo '42 is truth' + else + echo 'I do not know what to believe' + end + @end + +The fish filetype will use the fish_indent formatter by default. + + @clear + % function f; echo f; end + + :set filetype=fish + :FormatCode + ! fish_indent .*2>.* + $ function f + $ echo f + $ end + function f + echo f + end + @end + :set filetype= + +It can format specific line ranges of code using :FormatLines. + + @clear + % function foo; echo "my name is:"; echo "foo"; end + |function bar; echo "my name is:"; echo "bar"; end + + :1,2FormatLines fish_indent + ! fish_indent .*2>.* + $ function foo echo "my name is:" + $ echo "my name is:" + $ echo "foo" + $ end + $ function bar echo "my name is:"; echo "bar"; end + function foo echo "my name is:" + echo "my name is:" + echo "foo" + end + function bar echo "my name is:"; echo "bar"; end + @end + +Errors are reported. + + @clear + % function f; + :FormatCode fish_indent + ! fish_indent .*2> (.*) + $ echo test error >\1 (command) + $ 1 (status) + ~ test error + function f; + @end + +The name or path of the fish_indent executable can be configured via the +fish_indent_executable flag if the default of "fish_indent" doesn't work. + + @clear + :Glaive codefmt fish_indent_executable='my_fish_indent' + % function f; + :FormatCode fish_indent + ! my_fish_indent .* + $ function f + function f + @end + :Glaive codefmt fish_indent_executable='fish_indent'