Skip to content

Commit 279e415

Browse files
authored
Merge pull request #11 from clojure-vim/indent-fix
Fix indentation of subforms within `letfn`
2 parents 9798d86 + 89c9d71 commit 279e415

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(let [x (fn [y] 1)]
2+
(->> "ola"
3+
(x)))
4+
5+
(letfn [(x [y] 1)]
6+
(->> "ola"
7+
(x)))
8+
9+
(->> "ola"
10+
(x))
11+
12+
(defn foo []
13+
(letfn [(x [y] 1)]
14+
(->> "ola"
15+
(x))))
16+
17+
(letfn [(twice [x]
18+
(* x 2))
19+
(six-times [y]
20+
(* (twice y) 3))]
21+
(println "Twice 15 =" (twice 15))
22+
(println "Six times 15 =" (six-times 15)))
23+
24+
(letfn [(twice [x]
25+
(* x 2))]
26+
(->> "ola"
27+
(x)))
28+
29+
(letfn [(foo [x y]
30+
(->> x
31+
y
32+
:bar))
33+
(twice [x]
34+
(* x 2))
35+
(six-times [y]
36+
(* (twice y) 3))]
37+
(foo #{:foo :bar :biz} :foo))
38+
39+
;; vim:ft=clojure:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(let [x (fn [y] 1)]
2+
(->> "ola"
3+
(x)))
4+
5+
(letfn [(x [y] 1)]
6+
(->> "ola"
7+
(x)))
8+
9+
(->> "ola"
10+
(x))
11+
12+
(defn foo []
13+
(letfn [(x [y] 1)]
14+
(->> "ola"
15+
(x))))
16+
17+
(letfn [(twice [x]
18+
(* x 2))
19+
(six-times [y]
20+
(* (twice y) 3))]
21+
(println "Twice 15 =" (twice 15))
22+
(println "Six times 15 =" (six-times 15)))
23+
24+
(letfn [(twice [x]
25+
(* x 2))]
26+
(->> "ola"
27+
(x)))
28+
29+
(letfn [(foo [x y]
30+
(->> x
31+
y
32+
:bar))
33+
(twice [x]
34+
(* x 2))
35+
(six-times [y]
36+
(* (twice y) 3))]
37+
(foo #{:foo :bar :biz} :foo))
38+
39+
;; vim:ft=clojure:

clj/test/vim_clojure_static/indent_test.clj

+5
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@
3333
(test-indent "dispatch macro indentation is handled correctly"
3434
:in "test-dispatch-macro-indent.in"
3535
:out "test-dispatch-macro-indent.out"))
36+
37+
(deftest test-special-case-indent
38+
(test-indent "special case indentation is handled correctly"
39+
:in "test-special-case-indent.in"
40+
:out "test-special-case-indent.out"))

indent/clojure.vim

+28
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,35 @@ if exists("*searchpairpos")
169169

170170
call search('\S', 'W')
171171
let w = s:strip_namespace_and_macro_chars(s:current_word())
172+
172173
if g:clojure_special_indent_words =~# '\V\<' . w . '\>'
174+
175+
" `letfn` is a special-special-case.
176+
if w ==# 'letfn'
177+
" Earlier code left the cursor at:
178+
" (letfn [...] ...)
179+
" ^
180+
181+
" Search and get coordinates of first `[`
182+
" (letfn [...] ...)
183+
" ^
184+
call search('\[', 'W')
185+
let pos = getcurpos()
186+
let letfn_bracket = [pos[1], pos[2]]
187+
188+
" Move cursor to start of the form this function was
189+
" initially called on. Grab the coordinates of the
190+
" closest outer `[`.
191+
call cursor(a:position)
192+
let outer_bracket = s:match_pairs('\[', '\]', 0)
193+
194+
" If the located square brackets are not the same,
195+
" don't use special-case formatting.
196+
if outer_bracket != letfn_bracket
197+
return 0
198+
endif
199+
endif
200+
173201
return 1
174202
endif
175203

0 commit comments

Comments
 (0)