Skip to content

Commit a41fa1b

Browse files
committed
Improve indentation in "uniform" style and undo reader conditional work
When the "uniform" indent style is selected, we no longer apply other list indentation rules/analysis. Unfortunately the reader conditional changes broke indentation for the following: (ns my-ns (:require [clojure.string :as str] [clojure.spec.alpha :as s])) Indenting it as this: (ns my-ns (:require [clojure.string :as str] [clojure.spec.alpha :as s])) Which is incorrect.
1 parent 9b5e42c commit a41fa1b

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

indent/clojure.vim

+28-18
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,28 @@ endfunction
4040
" - traditional (Emacs equiv: align-arguments)
4141
" - uniform (Emacs equiv: always-indent)
4242
call s:SConf('clojure_indent_style', 'standard')
43-
4443
call s:SConf('clojure_align_multiline_strings', 0)
45-
4644
call s:SConf('clojure_fuzzy_indent_patterns', [
4745
\ '^with-\%(meta\|in-str\|out-str\|loading-context\)\@!',
4846
\ '^def',
4947
\ '^let'
5048
\ ])
5149

50+
" NOTE: When in "uniform" mode, ignores the "indent_style" and "indent_patterns" options.
51+
52+
" FIXME: fix reader conditional tests. Include (:require [...]) test cases.
53+
" Is it possible to fix reader conditional indentation?
54+
55+
" TODO: make the indentation function usable from other Clojure-like languages.
56+
57+
" TODO: explain the different numbers. The "indent_style" option can override "0"
58+
" - -1 Not in dictionary, follow defaults.
59+
" - 0: Align to first argument, else 2 space indentation.
60+
" - 1+: 2 space indentation, no alignment.
5261
" Defaults copied from: https://github.com/clojure-emacs/clojure-mode/blob/0e62583b5198f71856e4d7b80e1099789d47f2ed/clojure-mode.el#L1800-L1875
5362
call s:SConf('clojure_indent_rules', {
5463
\ 'ns': 1,
55-
\ 'fn': 1, 'def': 1, 'defn': 1, 'bound-fn': 1, 'fdef': 1,
64+
\ 'fn': 1, 'def': 1, 'defn': 1, 'bound-fn': 1,
5665
\ 'let': 1, 'binding': 1, 'defmethod': 1,
5766
\ 'if': 1, 'if-not': 1, 'if-some': 1, 'if-let': 1,
5867
\ 'when': 1, 'when-not': 1, 'when-some': 1, 'when-let': 1, 'when-first': 1,
@@ -64,14 +73,14 @@ call s:SConf('clojure_indent_rules', {
6473
\ 'reify': 1, 'proxy': 2, 'defrecord': 2, 'defprotocol': 1, 'definterface': 1,
6574
\ 'extend': 1, 'extend-protocol': 1, 'extend-type': 1,
6675
"\ (letfn) (1 ((:defn)) nil)
67-
"\ (reify) (:defn (1))
6876
"\ (deftype defrecord proxy) (2 nil nil (:defn))
6977
"\ (defprotocol definterface extend-protocol extend-type) (1 (:defn))
7078
"\ ClojureScript
7179
\ 'this-as': 1, 'specify': 1, 'specify!': 1,
72-
"\ (specify specify!) (1 :defn)
7380
"\ clojure.test
7481
\ 'deftest': 1, 'testing': 1, 'use-fixtures': 1, 'are': 2,
82+
"\ clojure.spec.alpha
83+
\ 'fdef': 1,
7584
"\ core.async
7685
\ 'alt!': 0, 'alt!!': 0, 'go': 0, 'go-loop': 1, 'thread': 0,
7786
"\ core.logic
@@ -217,11 +226,10 @@ function! s:InsideForm(lnum)
217226
return ['^', [0, 0]] " Default to top-level.
218227
endfunction
219228

220-
" Returns "1" when the "=" operator is currently active.
229+
" Returns "1" when the "=" operator is currently active, else "0".
221230
function! s:EqualsOperatorInEffect()
222231
return exists('*state')
223-
\ ? v:operator ==# '=' && state('o') ==# 'o'
224-
\ : 0
232+
\ ? v:operator ==# '=' && state('o') ==# 'o' : 0
225233
endfunction
226234

227235
function! s:StringIndent(delim_pos)
@@ -249,7 +257,12 @@ function! s:ListIndent(delim_pos)
249257
" TODO: extend "s:InsideForm" to provide information about the
250258
" subforms being formatted to avoid second parsing step.
251259

260+
let indent_style = s:Conf('clojure_indent_style', s:clojure_indent_style)
252261
let base_indent = s:PosToCharCol(a:delim_pos)
262+
263+
" Uniform indentation: just indent by 2 spaces.
264+
if indent_style ==# 'uniform' | return base_indent + 1 | endif
265+
253266
let ln = getline(a:delim_pos[0])
254267
let ln_content = ln[a:delim_pos[1]:]
255268
@@ -265,7 +278,7 @@ function! s:ListIndent(delim_pos)
265278
" TODO: handle complex indentation (e.g. letfn) and introduce
266279
" indentation config similar to Emacs' clojure-mode and cljfmt.
267280
" This new config option `clojure_indent_rules` should replace most
268-
" other indentation options.
281+
" other indentation options. Skip if "traditional" style was chosen.
269282

270283
" TODO: simplify this.
271284
let syms = split(ln_content, '[[:space:],;()\[\]{}@\\"^~`]', 1)
@@ -292,21 +305,18 @@ function! s:ListIndent(delim_pos)
292305
endif
293306

294307
" 2. Function indentation
295-
" if first operand is on the same line? (and not a keyword)
308+
" if first operand is on the same line?
296309
" - Indent subsequent lines to align with first operand.
297310
" else
298311
" - Indent 1 or 2 spaces.
299-
let indent_style = s:Conf('clojure_indent_style', s:clojure_indent_style)
300-
if indent_style !=# 'uniform' && ln_content[0] !=# ':'
301-
let pos = s:FirstFnArgPos(a:delim_pos)
302-
if pos != [0, 0] | return s:PosToCharCol(pos) - 1 | endif
303-
endif
312+
let pos = s:FirstFnArgPos(a:delim_pos)
313+
if pos != [0, 0] | return s:PosToCharCol(pos) - 1 | endif
304314

305315
" Fallback indentation for operands. When "clojure_indent_style" is
306-
" "standard", use 1 space indentation, else 2 space indentation.
316+
" "traditional", use 2 space indentation, else 1 space indentation.
307317
" The "sym_match" check handles the case when "clojure_indent_rules"
308-
" specified a value of "0".
309-
return base_indent + (indent_style !=# 'standard' || sym_match == 0)
318+
" specified a value of "0" for "standard" style.
319+
return base_indent + (indent_style ==# 'traditional' || sym_match == 0)
310320
endfunction
311321

312322
function! s:ClojureIndent()

0 commit comments

Comments
 (0)