Skip to content

Commit 7b83a9b

Browse files
committed
Make indent line up with opening parens
Previously we did this: ```swift let foo = Bar(arg1: 1 arg2: 2) ``` Now we do: ```swift let foo = Bar(arg1: 1 arg2: 2) ```
1 parent f94bdfe commit 7b83a9b

File tree

1 file changed

+68
-12
lines changed

1 file changed

+68
-12
lines changed

indent/swift.vim

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ endfunction
4040

4141
function! s:IsExcludedFromIndentAtPosition(line, column)
4242
let name = s:SyntaxNameAtPosition(a:line, a:column)
43-
return name ==# "swiftComment" || name ==# "swiftString"
43+
return s:IsSyntaxNameExcludedFromIndent(name)
4444
endfunction
4545

4646
function! s:IsExcludedFromIndent()
47-
return s:SyntaxName() ==# "swiftComment" || s:SyntaxName() ==# "swiftString"
47+
return s:IsSyntaxNameExcludedFromIndent(s:SyntaxName())
48+
endfunction
49+
50+
function! s:IsSyntaxNameExcludedFromIndent(name)
51+
return a:name ==# "swiftComment" || a:name ==# "swiftString" || a:name ==# "swiftInterpolatedWrapper" || a:name ==# "swiftMultilineInterpolatedWrapper" || a:name ==# "swiftMultilineString"
4852
endfunction
4953

5054
function! s:IsCommentLine(lnum)
@@ -101,10 +105,10 @@ function! SwiftIndent(...)
101105
return indent(openingSquare) + shiftwidth()
102106
endif
103107

104-
if line =~ ":$"
108+
if line =~ ":$" && (line =~ '^\s*case\W' || line =~ '^\s*default\W')
105109
let switch = search("switch", "bWn")
106110
return indent(switch)
107-
elseif previous =~ ":$"
111+
elseif previous =~ ":$" && (previous =~ '^\s*case\W' || previous =~ '^\s*default\W')
108112
return previousIndent + shiftwidth()
109113
endif
110114

@@ -131,12 +135,26 @@ function! SwiftIndent(...)
131135
return previousIndent + shiftwidth()
132136
elseif line =~ "}.*{"
133137
let openingBracket = searchpair("{", "", "}", "bWn", "s:IsExcludedFromIndent()")
138+
139+
let bracketLine = getline(openingBracket)
140+
let numOpenParensBracketLine = s:NumberOfMatches("(", bracketLine, openingBracket)
141+
let numCloseParensBracketLine = s:NumberOfMatches(")", bracketLine, openingBracket)
142+
if numOpenParensBracketLine > numCloseParensBracketLine
143+
let line = line(".")
144+
let column = col(".")
145+
call cursor(openingParen, column)
146+
let openingParenCol = searchpairpos("(", "", ")", "bWn", "s:IsExcludedFromIndent()")[1]
147+
call cursor(line, column)
148+
return openingParenCol
149+
endif
150+
134151
return indent(openingBracket)
135152
elseif currentCloseBrackets > currentOpenBrackets
136153
let column = col(".")
137-
call cursor(line("."), 1)
154+
let line = line(".")
155+
call cursor(line, 1)
138156
let openingBracket = searchpair("{", "", "}", "bWn", "s:IsExcludedFromIndent()")
139-
call cursor(line("."), column)
157+
call cursor(line, column)
140158

141159
let bracketLine = getline(openingBracket)
142160

@@ -149,8 +167,23 @@ function! SwiftIndent(...)
149167
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
150168
call cursor(line, column)
151169
return indent(openingParen)
170+
elseif numOpenParensBracketLine > numCloseParensBracketLine
171+
let line = line(".")
172+
let column = col(".")
173+
call cursor(openingParen, column)
174+
let openingParenCol = searchpairpos("(", "", ")", "bWn", "s:IsExcludedFromIndent()")[1]
175+
call cursor(line, column)
176+
return openingParenCol
152177
endif
178+
153179
return indent(openingBracket)
180+
elseif line =~ '^\s*)$'
181+
let line = line(".")
182+
let column = col(".")
183+
call cursor(line, 1)
184+
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
185+
call cursor(line, column)
186+
return indent(openingParen)
154187
else
155188
" - Current line is blank, and the user presses 'o'
156189
return previousIndent
@@ -194,8 +227,19 @@ function! SwiftIndent(...)
194227
return previousIndent + shiftwidth()
195228
endif
196229

197-
let previousParen = match(previous, "(")
198-
return indent(previousParen) + shiftwidth()
230+
let previousParen = match(previous, '\v\($')
231+
if previousParen != -1
232+
return previousIndent + shiftwidth()
233+
endif
234+
235+
let line = line(".")
236+
let column = col(".")
237+
call cursor(previousNum, col([previousNum, "$"]))
238+
let previousParen = searchpairpos("(", "", ")", "cbWn", "s:IsExcludedFromIndent()")
239+
call cursor(line, column)
240+
241+
" Match the last non escaped paren on the previous line
242+
return previousParen[1]
199243
endif
200244

201245
if numOpenBrackets > numCloseBrackets
@@ -204,7 +248,7 @@ function! SwiftIndent(...)
204248
call cursor(previousNum, column)
205249
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
206250
call cursor(line, column)
207-
return indent(openingParen) + shiftwidth()
251+
return openingParen + 1
208252
endif
209253

210254
" - Previous line has close then open braces, indent previous + 1 'sw'
@@ -224,11 +268,23 @@ function! SwiftIndent(...)
224268
" - Line above has (unmatched) open paren, next line needs indent
225269
if numOpenParens > 0
226270
let savePosition = getcurpos()
271+
let lastColumnOfPreviousLine = col([previousNum, "$"]) - 1
227272
" Must be at EOL because open paren has to be above (left of) the cursor
228-
call cursor(previousNum, [previousNum, col("$")])
229-
let previousParen = searchpair("(", "", ")", "cbWn", "s:IsExcludedFromIndent()")
273+
call cursor(previousNum, lastColumnOfPreviousLine)
274+
let previousParen = searchpairpos("(", "", ")", "cbWn", "s:IsExcludedFromIndent()")[1]
275+
" If the paren on the last line is the last character, indent the contents
276+
" at shiftwidth + previous indent
277+
if previousParen == lastColumnOfPreviousLine
278+
return previousIndent + shiftwidth()
279+
endif
280+
281+
" The previous line opens a closure and doesn't close it
282+
if numOpenBrackets > numCloseBrackets
283+
return previousParen + shiftwidth()
284+
endif
285+
230286
call setpos(".", savePosition)
231-
return indent(previousParen) + shiftwidth()
287+
return previousParen
232288
endif
233289

234290
return cindent

0 commit comments

Comments
 (0)