Skip to content

Commit 36a6b10

Browse files
committed
feat: add zprint formatter for clojure
1 parent 18f47c3 commit 36a6b10

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

Diff for: autoload/codefmt/zprint.vim

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
" Copyright 2019 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+
let s:supported_filetypes = ['clojure']
19+
20+
21+
""
22+
" @private
23+
" Formatter: zprint
24+
function! codefmt#zprint#GetFormatter() abort
25+
let l:formatter = {
26+
\ 'name': 'zprint',
27+
\ 'setup_instructions':
28+
\ 'Install zprint filter (https://github.com/kkinnear/zprint) ' .
29+
\ 'and configure the zprint_executable flag'}
30+
31+
function l:formatter.IsAvailable() abort
32+
return executable(s:plugin.Flag('zprint_executable'))
33+
endfunction
34+
35+
function l:formatter.AppliesToBuffer() abort
36+
return index(s:supported_filetypes, &filetype) >= 0
37+
endfunction
38+
39+
""
40+
" Reformat the current buffer with zprint or the binary named in
41+
" @flag(zprint_executable), only targeting the range between {startline} and
42+
" {endline}.
43+
function l:formatter.FormatRange(startline, endline) abort
44+
let l:zprint_options = s:plugin.Flag('zprint_options')
45+
if maktaba#value#IsCallable(l:zprint_options)
46+
let l:zprint_options = maktaba#function#Call(l:zprint_options)
47+
elseif type(l:zprint_options) isnot# type([])
48+
throw maktaba#error#WrongType(
49+
\ 'zprint_options flag must be list or callable. Found %s',
50+
\ string(l:zprint_options))
51+
endif
52+
let l:cmd = [s:plugin.Flag('zprint_executable')]
53+
call extend(l:cmd, l:zprint_options)
54+
55+
call maktaba#ensure#IsNumber(a:startline)
56+
call maktaba#ensure#IsNumber(a:endline)
57+
let l:lines = getline(1, line('$'))
58+
59+
" zprint doesn't support formatting a range of lines, so format the range
60+
" individually, ignoring context. This works well for top-level forms, although it's
61+
" not ideal for inner forms because it loses the indentation.
62+
let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n")
63+
64+
" Prepare the syscall, optionally changing to the containing directory for
65+
" the sake of {:search-config? true}.
66+
let l:syscall = maktaba#syscall#Create(l:cmd).WithStdin(l:input)
67+
if s:plugin.Flag('zprint_chdir')
68+
let l:syscall = l:syscall.WithCwd(expand('%:p:h'))
69+
endif
70+
let l:result = l:syscall.Call()
71+
let l:formatted = split(l:result.stdout, "\n")
72+
73+
" Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right.
74+
let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : []
75+
let l:full_formatted = l:before + l:formatted + l:lines[a:endline :]
76+
77+
call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted)
78+
endfunction
79+
80+
return l:formatter
81+
endfunction

Diff for: instant/flags.vim

+15
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,18 @@ call s:plugin.Flag('prettier_options', [
115115
""
116116
" The path to the prettier executable.
117117
call s:plugin.Flag('prettier_executable', 'prettier')
118+
119+
""
120+
" Whether and where to change directory before calling zprint. If this is true,
121+
" then changes directory (temporarily) to location of file, so that
122+
" :search-config? can be effective.
123+
call s:plugin.Flag('zprint_chdir', v:true)
124+
125+
""
126+
" Command line arguments to feed zprint. Either a list or callable that takes no
127+
" args and returns a list with command line arguments.
128+
call s:plugin.Flag('zprint_options', ['{:search-config? true}'])
129+
130+
""
131+
" The path to the zprint executable.
132+
call s:plugin.Flag('zprint_executable', 'zprint')

Diff for: plugin/register.vim

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ call s:registry.AddExtension(codefmt#gn#GetFormatter())
3434
call s:registry.AddExtension(codefmt#buildifier#GetFormatter())
3535
call s:registry.AddExtension(codefmt#googlejava#GetFormatter())
3636
call s:registry.AddExtension(codefmt#shfmt#GetFormatter())
37+
call s:registry.AddExtension(codefmt#zprint#GetFormatter())

Diff for: vroom/zprint.vroom

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
The zprint formatter knows how to format Clojure.
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+
18+
The zprint formatter expects the zprint executable to be installed on your system.
19+
20+
% (defn x [] (cond nil 1 :else 2))
21+
:FormatCode zprint
22+
! zprint .*
23+
$ (defn x
24+
$ []
25+
$ (cond nil 1
26+
$ :else 2))
27+
28+
The name or path of the zprint executable can be configured via the
29+
zprint_executable flag if the default of "zprint" doesn't work.
30+
31+
:Glaive codefmt zprint_executable='myzprint'
32+
:FormatCode zprint
33+
! myzprint .*
34+
$ (defn x
35+
$ []
36+
$ (cond nil 1
37+
$ :else 2))
38+
:Glaive codefmt zprint_executable='zprint'
39+
40+
41+
You can format any buffer with zprint specifying the formatter explicitly.
42+
43+
@clear
44+
% (defn x [] (cond nil 1 :else 2))
45+
46+
:FormatCode zprint
47+
! zprint .*2>.*
48+
(defn x
49+
[]
50+
(cond nil 1
51+
:else 2))
52+
@end
53+
54+
It can format specific line ranges of code using :FormatLines.
55+
56+
@clear
57+
% (defn x [] (cond nil 1 :else 2))<CR>
58+
|(defn x [] (cond nil 1 :else 2))
59+
60+
:1,1FormatLines zprint
61+
! zprint .*
62+
$ (defn x
63+
$ []
64+
$ (cond nil 1
65+
$ :else 2))
66+
(defn x
67+
[]
68+
(cond nil 1
69+
:else 2))
70+
(defn x [] (cond nil 1 :else 2))
71+
@end
72+
73+
It is also the default formatter for the clojure file type.
74+
75+
@clear
76+
% (defn x [] (cond nil 1 :else 2))
77+
78+
:set filetype=clojure
79+
:FormatCode
80+
! zprint .*
81+
$ (defn x [] (cond nil 1 :else 2))

0 commit comments

Comments
 (0)