Skip to content

Commit af796cf

Browse files
jayzhuangdbarnett
authored andcommitted
Add support for fish files using fish_indent (#134)
Developed and tested (manual and unit test) with neovim `vroom --neovim vroom/fish_indent.vroom` Fixes: #133
1 parent 024911c commit af796cf

File tree

7 files changed

+156
-1
lines changed

7 files changed

+156
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ helpfiles in the `doc/` directory. The helpfiles are also available via
1515
* [Clojure](https://clojure.org/) ([zprint](https://github.com/kkinnear/zprint))
1616
* CSS, Sass, SCSS, Less (js-beautify)
1717
* Dart (dartfmt)
18+
* Fish ([fish_indent](https://fishshell.com/docs/current/commands.html#fish_indent))
1819
* Go (gofmt)
1920
* [GN](https://www.chromium.org/developers/gn-build-configuration) (gn)
2021
* HTML (js-beautify)

autoload/codefmt.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
" * c, cpp, proto, javascript, typescript: clang-format
3131
" * clojure: zprint
3232
" * dart: dartfmt
33+
" * fish: fish_indent
3334
" * gn: gn
3435
" * go: gofmt
3536
" * python: autopep8, yapf

autoload/codefmt/fish_indent.vim

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
" Copyright 2020 Google Inc. All rights reserved.
2+
"
3+
" Licensed under the Apache License, Version 2.0 (the "License");
4+
" you may not use this file except in compliance with the License.
5+
" You may obtain a copy of the License at
6+
"
7+
" http://www.apache.org/licenses/LICENSE-2.0
8+
"
9+
" Unless required by applicable law or agreed to in writing, software
10+
" distributed under the License is distributed on an "AS IS" BASIS,
11+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
" See the License for the specific language governing permissions and
13+
" limitations under the License.
14+
15+
16+
let s:plugin = maktaba#plugin#Get('codefmt')
17+
18+
19+
function! codefmt#fish_indent#GetFormatter() abort
20+
let l:formatter = {
21+
\ 'name': 'fish_indent',
22+
\ 'setup_instructions': 'Install fish_indent (https://fishshell.com/docs/current/commands.html#fish_indent)' .
23+
\ ' and configure the fish_indent_executable flag'}
24+
25+
function l:formatter.IsAvailable() abort
26+
return executable(s:plugin.Flag('fish_indent_executable'))
27+
endfunction
28+
29+
function l:formatter.AppliesToBuffer() abort
30+
return &filetype is# 'fish'
31+
endfunction
32+
33+
""
34+
" Reformat the current buffer with fish_indent or the binary named in
35+
" @flag(fish_indent_executable), only targeting the range between {startline}
36+
" and {endline}.
37+
function l:formatter.FormatRange(startline, endline) abort
38+
let l:cmd = [ s:plugin.Flag('fish_indent_executable') ]
39+
call maktaba#ensure#IsNumber(a:startline)
40+
call maktaba#ensure#IsNumber(a:endline)
41+
" fish_indent does not support range formatting yet:
42+
" https://github.com/fish-shell/fish-shell/issues/6490
43+
call codefmt#formatterhelpers#AttemptFakeRangeFormatting(
44+
\ a:startline, a:endline, l:cmd)
45+
endfunction
46+
47+
return l:formatter
48+
endfunction

doc/codefmt.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ The path to the zprint executable. Typically this is one of the native images
111111
installed as zprint.
112112
Default: 'zprint' `
113113

114+
*codefmt:fish_indent_executable*
115+
The path to the fish_indent executable.
116+
Default: 'fish_indent' `
117+
114118
*codefmt:plugin[autocmds]*
115119
Configures whether plugin/autocmds.vim should be loaded.
116120
Default: 1 `
@@ -154,6 +158,7 @@ The current list of defaults by filetype is:
154158
* c, cpp, proto, javascript, typescript: clang-format
155159
* clojure: zprint
156160
* dart: dartfmt
161+
* fish: fish_indent
157162
* gn: gn
158163
* go: gofmt
159164
* python: autopep8, yapf

instant/flags.vim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ call s:plugin.Flag('rustfmt_options', [])
128128
" The path to the rustfmt executable.
129129
call s:plugin.Flag('rustfmt_executable', 'rustfmt')
130130

131-
132131
""
133132
" @private
134133
" 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'))
148147
" images (zprintl or zprintm) from https://github.com/kkinnear/zprint/releases
149148
" installed as zprint.
150149
call s:plugin.Flag('zprint_executable', 'zprint')
150+
151+
""
152+
" The path to the fish_indent executable.
153+
call s:plugin.Flag('fish_indent_executable', 'fish_indent')

plugin/register.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ call s:registry.AddExtension(codefmt#buildifier#GetFormatter())
3636
call s:registry.AddExtension(codefmt#googlejava#GetFormatter())
3737
call s:registry.AddExtension(codefmt#shfmt#GetFormatter())
3838
call s:registry.AddExtension(codefmt#zprint#GetFormatter())
39+
call s:registry.AddExtension(codefmt#fish_indent#GetFormatter())

vroom/fish_indent.vroom

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
The built-in fish_indent knows how to format fish code.
2+
If you aren't familiar with basic codefmt usage yet, see main.vroom first.
3+
4+
We'll set up codefmt and configure the vroom environment, then jump into some
5+
examples.
6+
7+
:source $VROOMDIR/setupvroom.vim
8+
9+
:let g:repeat_calls = []
10+
:function FakeRepeat(...)<CR>
11+
| call add(g:repeat_calls, a:000)<CR>
12+
:endfunction
13+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
14+
15+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
16+
17+
You can format any buffer with fish_indent specifying the formatter explicitly.
18+
19+
@clear
20+
% if test 42 -eq $truth; echo '42 is truth'; else; echo 'I do not know what to believe'; end
21+
22+
:FormatCode fish_indent
23+
! fish_indent .*2>.*
24+
$ if test 42 -eq $truth
25+
$ echo '42 is truth'
26+
$ else
27+
$ echo 'I do not know what to believe'
28+
$ end
29+
if test 42 -eq $truth
30+
echo '42 is truth'
31+
else
32+
echo 'I do not know what to believe'
33+
end
34+
@end
35+
36+
The fish filetype will use the fish_indent formatter by default.
37+
38+
@clear
39+
% function f; echo f; end
40+
41+
:set filetype=fish
42+
:FormatCode
43+
! fish_indent .*2>.*
44+
$ function f
45+
$ echo f
46+
$ end
47+
function f
48+
echo f
49+
end
50+
@end
51+
:set filetype=
52+
53+
It can format specific line ranges of code using :FormatLines.
54+
55+
@clear
56+
% function foo; echo "my name is:"; echo "foo"; end<CR>
57+
|function bar; echo "my name is:"; echo "bar"; end
58+
59+
:1,2FormatLines fish_indent
60+
! fish_indent .*2>.*
61+
$ function foo echo "my name is:"
62+
$ echo "my name is:"
63+
$ echo "foo"
64+
$ end
65+
$ function bar echo "my name is:"; echo "bar"; end
66+
function foo echo "my name is:"
67+
echo "my name is:"
68+
echo "foo"
69+
end
70+
function bar echo "my name is:"; echo "bar"; end
71+
@end
72+
73+
Errors are reported.
74+
75+
@clear
76+
% function f;
77+
:FormatCode fish_indent
78+
! fish_indent .*2> (.*)
79+
$ echo test error >\1 (command)
80+
$ 1 (status)
81+
~ test error
82+
function f;
83+
@end
84+
85+
The name or path of the fish_indent executable can be configured via the
86+
fish_indent_executable flag if the default of "fish_indent" doesn't work.
87+
88+
@clear
89+
:Glaive codefmt fish_indent_executable='my_fish_indent'
90+
% function f;
91+
:FormatCode fish_indent
92+
! my_fish_indent .*
93+
$ function f
94+
function f
95+
@end
96+
:Glaive codefmt fish_indent_executable='fish_indent'

0 commit comments

Comments
 (0)