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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions autoload/codefmt.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions autoload/codefmt/fish_indent.vim
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions doc/codefmt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 `
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion instant/flags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
1 change: 1 addition & 0 deletions plugin/register.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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())
96 changes: 96 additions & 0 deletions vroom/fish_indent.vroom
Original file line number Diff line number Diff line change
@@ -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(...)<CR>
| call add(g:repeat_calls, a:000)<CR>
: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<CR>
|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'