Skip to content

Commit 242dc9d

Browse files
committed
Yet more string detection fixes and slight code clean up
1 parent e573da7 commit 242dc9d

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

indent/clojure.vim

+24-23
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,26 @@ function! s:Conf(opt, default)
4545
return get(b:, a:opt, get(g:, a:opt, a:default))
4646
endfunction
4747

48-
function! s:ShouldAlignMultiLineStrings()
49-
" Possible Values: (default is 0)
50-
" -1: Indent of 0, along left edge, like traditional Lisps.
51-
" 0: Indent in alignment with string start delimiter.
52-
" 1: Indent in alignment with end of the string start delimiter.
53-
return s:Conf('clojure_align_multiline_strings', 0)
48+
function! s:EqualsOperatorInEffect()
49+
" Returns 1 when the previous operator used is "=" and is currently in
50+
" effect (i.e. "state" includes "o").
51+
return v:operator ==# '=' && state('o') ==# 'o'
5452
endfunction
5553

5654
function! s:GetStringIndent(delim_pos, regex)
5755
" Mimic multi-line string indentation behaviour in VS Code and Emacs.
5856
let m = mode()
59-
if m ==# 'i' || (m ==# 'n' && ! (v:operator ==# '=' && state('o') ==# 'o'))
60-
" If in insert mode, or (in normal mode and last operator is
61-
" not "=" and is not currently active.
62-
let rule = s:ShouldAlignMultiLineStrings()
57+
if m ==# 'i' || (m ==# 'n' && ! s:EqualsOperatorInEffect())
58+
" If in insert mode, or normal mode but "=" is not in effect.
59+
let rule = s:Conf('clojure_align_multiline_strings', 0)
6360
if rule == -1
64-
return 0 " No indent.
61+
" Indent along left edge, like traditional Lisps.
62+
return 0
6563
elseif rule == 1
66-
" Align with start of delimiter.
64+
" Indent in alignment with end of the string start delimiter.
6765
return a:delim_pos[1]
6866
else
69-
" Align with end of delimiter.
67+
" Indent in alignment with string start delimiter.
7068
return a:delim_pos[1] - (a:regex ? 2 : 1)
7169
endif
7270
else
@@ -86,27 +84,30 @@ function! s:CheckPair(name, start, end, SkipFn)
8684
endif
8785
endfunction
8886

89-
function! s:GetClojureIndent()
90-
" Move cursor to the first column of the line we want to indent.
91-
call cursor(v:lnum, 1)
92-
93-
if empty(getline(v:lnum))
87+
function! s:GetCurrentSynName(lnum)
88+
if empty(getline(a:lnum))
9489
" Improves the accuracy of string detection when a newline is
9590
" entered while in insert mode.
96-
let strline = v:lnum - 1
97-
let synname = s:GetSynIdName(strline, strlen(getline(strline)))
91+
let strline = a:lnum - 1
92+
return s:GetSynIdName(strline, strlen(getline(strline)))
9893
else
99-
let synname = s:GetSynIdName(v:lnum, 1)
94+
return s:GetSynIdName(a:lnum, 1)
10095
endif
96+
endfunction
97+
98+
function! s:GetClojureIndent()
99+
" Move cursor to the first column of the line we want to indent.
100+
call cursor(v:lnum, 1)
101101

102102
let s:best_match = ['top', [0, 0]]
103103

104+
let synname = s:GetCurrentSynName(v:lnum)
104105
if synname =~? 'string'
106+
call s:CheckPair('str', '"', '"', function('<SID>NotStringDelimiter'))
105107
" Sometimes, string highlighting does not kick in correctly,
106108
" until after this first "s:CheckPair" call, so we have to
107109
" detect and attempt an automatic correction.
108-
call s:CheckPair('str', '"', '"', function('<SID>NotStringDelimiter'))
109-
let new_synname = s:GetSynIdName(v:lnum, 1)
110+
let new_synname = s:GetCurrentSynName(v:lnum)
110111
if new_synname !=# synname
111112
echoerr 'Misdetected string! Retrying...'
112113
let s:best_match = ['top', [0, 0]]

0 commit comments

Comments
 (0)