@@ -40,11 +40,15 @@ endfunction
40
40
41
41
function ! s: IsExcludedFromIndentAtPosition (line , column)
42
42
let name = s: SyntaxNameAtPosition (a: line , a: column )
43
- return name == # " swiftComment " || name == # " swiftString "
43
+ return s: IsSyntaxNameExcludedFromIndent ( name)
44
44
endfunction
45
45
46
46
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"
48
52
endfunction
49
53
50
54
function ! s: IsCommentLine (lnum)
@@ -101,10 +105,10 @@ function! SwiftIndent(...)
101
105
return indent (openingSquare) + shiftwidth ()
102
106
endif
103
107
104
- if line = ~ " :$"
108
+ if line = ~ " :$" && ( line = ~ ' ^\s*case\W ' || line = ~ ' ^\s*default\W ' )
105
109
let switch = search (" switch" , " bWn" )
106
110
return indent (switch)
107
- elseif previous = ~ " :$"
111
+ elseif previous = ~ " :$" && ( previous = ~ ' ^\s*case\W ' || previous = ~ ' ^\s*default\W ' )
108
112
return previousIndent + shiftwidth ()
109
113
endif
110
114
@@ -131,12 +135,26 @@ function! SwiftIndent(...)
131
135
return previousIndent + shiftwidth ()
132
136
elseif line = ~ " }.*{"
133
137
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
+
134
151
return indent (openingBracket)
135
152
elseif currentCloseBrackets > currentOpenBrackets
136
153
let column = col (" ." )
137
- call cursor (line (" ." ), 1 )
154
+ let line = line (" ." )
155
+ call cursor (line , 1 )
138
156
let openingBracket = searchpair (" {" , " " , " }" , " bWn" , " s:IsExcludedFromIndent()" )
139
- call cursor (line ( " . " ) , column)
157
+ call cursor (line , column)
140
158
141
159
let bracketLine = getline (openingBracket)
142
160
@@ -149,8 +167,23 @@ function! SwiftIndent(...)
149
167
let openingParen = searchpair (" (" , " " , " )" , " bWn" , " s:IsExcludedFromIndent()" )
150
168
call cursor (line , column)
151
169
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
152
177
endif
178
+
153
179
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)
154
187
else
155
188
" - Current line is blank, and the user presses 'o'
156
189
return previousIndent
@@ -194,8 +227,19 @@ function! SwiftIndent(...)
194
227
return previousIndent + shiftwidth ()
195
228
endif
196
229
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 ]
199
243
endif
200
244
201
245
if numOpenBrackets > numCloseBrackets
@@ -204,7 +248,7 @@ function! SwiftIndent(...)
204
248
call cursor (previousNum, column)
205
249
let openingParen = searchpair (" (" , " " , " )" , " bWn" , " s:IsExcludedFromIndent()" )
206
250
call cursor (line , column)
207
- return indent ( openingParen) + shiftwidth ()
251
+ return openingParen + 1
208
252
endif
209
253
210
254
" - Previous line has close then open braces, indent previous + 1 'sw'
@@ -224,11 +268,23 @@ function! SwiftIndent(...)
224
268
" - Line above has (unmatched) open paren, next line needs indent
225
269
if numOpenParens > 0
226
270
let savePosition = getcurpos ()
271
+ let lastColumnOfPreviousLine = col ([previousNum, " $" ]) - 1
227
272
" 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
+
230
286
call setpos (" ." , savePosition)
231
- return indent ( previousParen) + shiftwidth ()
287
+ return previousParen
232
288
endif
233
289
234
290
return cindent
0 commit comments