Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 956cb15

Browse files
author
Ari Archer
committed
Merge google#161
Signed-off-by: Ari Archer <truncateddinosour@gmail.com>
1 parent 01f352c commit 956cb15

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

autoload/codefmt/brittany.vim

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
" Copyright 2017 Google Inc. All rights reserved.
2+
" Copyright 2019 Jingrong Chen i@cjr.host
3+
"
4+
" Licensed under the Apache License, Version 2.0 (the "License");
5+
" you may not use this file except in compliance with the License.
6+
" You may obtain a copy of the License at
7+
"
8+
" http://www.apache.org/licenses/LICENSE-2.0
9+
"
10+
" Unless required by applicable law or agreed to in writing, software
11+
" distributed under the License is distributed on an "AS IS" BASIS,
12+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
" See the License for the specific language governing permissions and
14+
" limitations under the License.
15+
16+
17+
let s:plugin = maktaba#plugin#Get('codefmt')
18+
19+
20+
""
21+
" @private
22+
"
23+
" Formatter provider for Bazel BUILD files using buildifier.
24+
function! codefmt#brittany#GetFormatter() abort
25+
let l:formatter = {
26+
\ 'name': 'brittany',
27+
\ 'setup_instructions': 'Install brittany. ' .
28+
\ '(https://github.com/lspitzner/brittany).'}
29+
30+
function l:formatter.IsAvailable() abort
31+
return executable(s:plugin.Flag('brittany_executable'))
32+
endfunction
33+
34+
function l:formatter.AppliesToBuffer() abort
35+
return &filetype is# 'haskell'
36+
endfunction
37+
38+
""
39+
" Reformat the current buffer with yapf or the binary named in
40+
" @flag(brittany_executable), only targeting the range between {startline} and
41+
" {endline}.
42+
" @throws ShellError
43+
function l:formatter.FormatRange(startline, endline) abort
44+
let l:executable = s:plugin.Flag('brittany_executable')
45+
let l:indent = s:plugin.Flag('brittany_indent')
46+
let l:columns = s:plugin.Flag('brittany_columns')
47+
48+
call maktaba#ensure#IsNumber(a:startline)
49+
call maktaba#ensure#IsNumber(a:endline)
50+
let l:lines = getline(1, line('$'))
51+
52+
let l:cmd = [l:executable, "--indent", l:indent, "--columns", l:columns]
53+
let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n")
54+
55+
let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call(0)
56+
if v:shell_error == 1 " Indicates an error with parsing
57+
call maktaba#error#Shout('Error formatting file: %s', l:result.stderr)
58+
return
59+
endif
60+
let l:formatted = split(l:result.stdout, "\n")
61+
let l:before = a:startline > 1 ? l:lines[ : a:startline - 2 ] : []
62+
let l:full_formatted = l:before + l:formatted + l:lines[a:endline :]
63+
64+
call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted)
65+
endfunction
66+
67+
return l:formatter
68+
endfunction

autoload/codefmt/hindent.vim

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
" Copyright 2017 Google Inc. All rights reserved.
2+
" Copyright 2019 Jingrong Chen i@cjr.host
3+
"
4+
" Licensed under the Apache License, Version 2.0 (the "License");
5+
" you may not use this file except in compliance with the License.
6+
" You may obtain a copy of the License at
7+
"
8+
" http://www.apache.org/licenses/LICENSE-2.0
9+
"
10+
" Unless required by applicable law or agreed to in writing, software
11+
" distributed under the License is distributed on an "AS IS" BASIS,
12+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
" See the License for the specific language governing permissions and
14+
" limitations under the License.
15+
16+
17+
let s:plugin = maktaba#plugin#Get('codefmt')
18+
19+
20+
""
21+
" @private
22+
"
23+
" Formatter provider for Bazel BUILD files using buildifier.
24+
function! codefmt#hindent#GetFormatter() abort
25+
let l:formatter = {
26+
\ 'name': 'hindent',
27+
\ 'setup_instructions': 'Install hindent. ' .
28+
\ '(https://github.com/chrisdone/hindent).'}
29+
30+
function l:formatter.IsAvailable() abort
31+
return executable(s:plugin.Flag('hindent_executable'))
32+
endfunction
33+
34+
function l:formatter.AppliesToBuffer() abort
35+
return &filetype is# 'haskell'
36+
endfunction
37+
38+
""
39+
" Reformat the current buffer with yapf or the binary named in
40+
" @flag(hindent_executable), only targeting the range between {startline} and
41+
" {endline}.
42+
" @throws ShellError
43+
function l:formatter.FormatRange(startline, endline) abort
44+
let l:executable = s:plugin.Flag('hindent_executable')
45+
let l:indent_size = s:plugin.Flag('hindent_indent_size')
46+
let l:line_length = s:plugin.Flag('hindent_line_length')
47+
48+
call maktaba#ensure#IsNumber(a:startline)
49+
call maktaba#ensure#IsNumber(a:endline)
50+
let l:lines = getline(1, line('$'))
51+
52+
let l:cmd = [l:executable, "--indent-size", l:indent_size, "--line-length", l:line_length]
53+
let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n")
54+
55+
let l:result = maktaba#syscall#Create(l:cmd).WithStdin(l:input).Call(0)
56+
if v:shell_error == 1 " Indicates an error with parsing
57+
call maktaba#error#Shout('Error formatting file: %s', l:result.stderr)
58+
return
59+
endif
60+
let l:formatted = split(l:result.stdout, "\n")
61+
let l:before = a:startline > 1 ? l:lines[ : a:startline - 2 ] : []
62+
let l:full_formatted = l:before + l:formatted + l:lines[a:endline :]
63+
64+
call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted)
65+
endfunction
66+
67+
return l:formatter
68+
endfunction

instant/flags.vim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ call s:plugin.Flag('zprint_executable', 'zprint')
189189
" The path to the fish_indent executable.
190190
call s:plugin.Flag('fish_indent_executable', 'fish_indent')
191191

192+
""
193+
" The path to the hindent executable.
194+
call s:plugin.Flag('hindent_executable', 'hindent')
195+
call s:plugin.Flag('hindent_indent_size', '2')
196+
call s:plugin.Flag('hindent_line_length', '80')
197+
198+
" The path to the brittany executable.
199+
call s:plugin.Flag('brittany_executable', 'brittany')
200+
call s:plugin.Flag('brittany_indent', '2')
201+
call s:plugin.Flag('brittany_columns', '80')
202+
192203
""
193204
" The path to the nixpkgs-fmt executable.
194205
call s:plugin.Flag('nixpkgs_fmt_executable', 'nixpkgs-fmt')

plugin/register.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,5 @@ call s:registry.AddExtension(codefmt#isort#GetFormatter())
7676
call s:registry.AddExtension(codefmt#yapf#GetFormatter())
7777
call s:registry.AddExtension(codefmt#rustfmt#GetFormatter())
7878
call s:registry.AddExtension(codefmt#shfmt#GetFormatter())
79-
79+
call s:registry.AddExtension(codefmt#hindent#GetFormatter())
80+
call s:registry.AddExtension(codefmt#brittany#GetFormatter())

0 commit comments

Comments
 (0)