Skip to content

Commit 787e1e8

Browse files
committed
Replace clojure_align_subforms with clojure_indent_style
1 parent 35e0234 commit 787e1e8

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

indent/clojure.vim

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -163,52 +163,49 @@ function! s:ListIndent(delim_pos)
163163
" the subforms within the form being formatted to avoid second parsing
164164
" step.
165165

166-
" TODO: The overcomplicated list indentation rules in Clojure!
167-
" 1. If starts with a symbol or keyword: extract it.
168-
" 1.1. Look up in rules table.
169-
" 1.1.1. See number for forms forward to lookup.
170-
" 1.1.2. Start parsing forwards "x" forms. (Skip metadata)
171-
" 1.1.3. Apply indentation.
172-
" 1.2. Not found, goto 2.
173-
" 2. Else, format like a function.
174-
" 2.1. Is first operand on the same line? (Treat metadata as args)
175-
" 2.1.1. Indent subsequent lines to align with first operand.
176-
" 2.2. Else.
177-
" 2.2.1. Indent 1 or 2 spaces.
178-
179166
call cursor(a:delim_pos)
180167
let ln = getline(a:delim_pos[0])
181168
let base_indent = a:delim_pos[1]
182169

183170
" 1. TODO: Macro/rule indentation
184-
" let syms = split(ln[base_indent:], '[[:space:],;()\[\]{}@\\"^~`]', 1)
171+
" if starts with a symbol or keyword: extract it.
172+
" - Look up in rules table.
173+
" - See number for forms forward to lookup.
174+
" - Start parsing forwards "x" forms. (Skip metadata)
175+
" - Apply indentation.
176+
" else, not found, go to 2.
185177
" TODO: complex indentation (e.g. letfn)
186178
" TODO: namespaces.
179+
" let syms = split(ln[base_indent:], '[[:space:],;()\[\]{}@\\"^~`]', 1)
187180

188181
" 2. Function indentation
189-
let cur_pos = [a:delim_pos[0], a:delim_pos[1] + 1]
190-
call cursor(cur_pos)
191-
192-
let ch = ln[cur_pos[1] - 1]
193-
if ch =~# '[([{]'
194-
normal! %w
195-
elseif ch !~# '[;"]'
196-
normal! w
197-
endif
198-
199-
let new_cur_pos = getcurpos()[1:2]
182+
" if first operand is on the same line? (Treat metadata as args.)
183+
" - Indent subsequent lines to align with first operand.
184+
" else
185+
" - Indent 1 or 2 spaces.
186+
let indent_style = s:Conf('clojure_indent_style', 'always-align')
187+
if indent_style !=# 'always-indent'
188+
let init_col = a:delim_pos[1] + 1
189+
call cursor(a:delim_pos[0], init_col)
190+
191+
" TODO: replace cursor translation with searching?
192+
let ch = ln[base_indent]
193+
if ch ==# '(' || ch ==# '[' || ch ==# '{'
194+
normal! %w
195+
elseif ch !=# ';' && ch !=# '"'
196+
normal! w
197+
endif
200198

201-
" TODO: option to disable operand-alignment.
202-
if cur_pos[0] == new_cur_pos[0] && cur_pos[1] != new_cur_pos[1]
203-
" Align operands
204-
return new_cur_pos[1] - 1
199+
let cur_pos = getcurpos()[1:2]
200+
if a:delim_pos[0] == cur_pos[0] && init_col != cur_pos[1]
201+
" Align operands.
202+
return cur_pos[1] - 1
203+
endif
205204
endif
206205

207-
" TODO: create an alternative option with a better name.
208-
" <https://github.com/clojure-emacs/clojure-mode/#indentation-of-function-forms>
209-
" Base indentation for forms. When "clojure_align_subforms" is "1",
210-
" use 1 space indentation, otherwise 2 space indentation.
211-
return base_indent + ! s:Conf('clojure_align_subforms', 1)
206+
" Fallback indentation for operands. When "clojure_indent_style" is
207+
" "always-align", use 1 space indentation, else 2 space indentation.
208+
return base_indent + (indent_style !=# 'always-align')
212209
endfunction
213210

214211
function! s:ClojureIndent()

0 commit comments

Comments
 (0)