Skip to content

Commit 7d2daef

Browse files
authored
Merge pull request #130 from keith/indent-parens
Make indent line up with opening parens
2 parents f94bdfe + 46a93db commit 7d2daef

File tree

3 files changed

+96
-25
lines changed

3 files changed

+96
-25
lines changed

example/example.swift

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ struct ArgumentList {
223223
var arguments: String[]
224224

225225
init(argv: UnsafePointer<CString>,
226-
count: CInt)
226+
count: CInt)
227227
{
228228
foo
229229
}
@@ -249,9 +249,9 @@ func simpleDescription() -> String {
249249

250250
let library = [
251251
Movie(name: "foo bar",
252-
dfasdfsdfdirector: "someone",
253-
foo: "bar",
254-
bazzzer: "qux")
252+
dfasdfsdfdirector: "someone",
253+
foo: "bar",
254+
bazzzer: "qux")
255255
]
256256

257257

@@ -266,7 +266,9 @@ class MainViewController: UIViewController, UITableViewDataSource {}
266266

267267
@IBAction func changePostFilter(sender: UISegmentedControl) {}
268268
override func prepareForSegue(segue: UIStoryboardSegue,
269-
sender: AnyObject) {}
269+
sender: AnyObject) {}
270+
override func prepareForSegue(
271+
segue: UIStoryboardSegue, sender: AnyObject) {}
270272
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {}
271273
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {}
272274
lazy var foo : String
@@ -293,7 +295,7 @@ func foo () {
293295
}
294296

295297
let foo = CGRectMake(0, (5 - 2),
296-
100, 200)
298+
100, 200)
297299

298300

299301
let dict = [
@@ -351,10 +353,10 @@ let data = NSData(contentsOfFile: path) else
351353
}
352354

353355
UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: {
354-
view.backgroundColor = UIColor.redColor()
355-
}) { finished in
356-
print("indent?")
357-
}
356+
view.backgroundColor = UIColor.redColor()
357+
}, completion: { finished in
358+
print("indent?")
359+
})
358360

359361
// Indent last line should hold
360362
self.init(className: "Item", dictionary: [
@@ -374,3 +376,16 @@ NSWorkspace.sharedWorkspace().notificationCenter.addObserver(
374376
public func find(closure: @noescape Element throws -> Bool) rethrows -> Element? {
375377

376378
}
379+
380+
UIView.animate(withDuration: 0.2, animations: {
381+
self.foo.alpha = 1.0
382+
self.bar.alpha = 1.0
383+
}, completion: { _ in
384+
completion()
385+
})
386+
387+
A.b().application(
388+
application, didFinishLaunchingWithOptions: launchOptions)
389+
390+
A.application(b(),
391+
application, didFinishLaunchingWithOptions: launchOptions)

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

syntax/swift.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ delfunction s:CommentKeywordMatch
5252

5353
" Literals
5454
" Strings
55-
syntax region swiftString start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=swiftMultilineInterpolatedWrapper oneline
55+
syntax region swiftString start=/"/ skip=/\\\\\|\\"/ end=/"/ contains=swiftInterpolatedWrapper oneline
5656
syntax region swiftMultilineString start=/"""/ end=/"""/ contains=swiftMultilineInterpolatedWrapper
57-
syntax region swiftMultilineInterpolatedWrapper start="\v\\\(\s*" end="\v\s*\)" contained containedin=swiftMultilineString contains=swiftInterpolatedString oneline
58-
syntax region swiftInterpolatedWrapper start="\v[^\\]\zs\\\(\s*" end="\v\s*\)" contained containedin=swiftString contains=swiftInterpolatedString,swiftString oneline
57+
syntax region swiftMultilineInterpolatedWrapper start='\v\zs\\\(\s*' end='\v\s*\)' contained containedin=swiftMultilineString contains=swiftInterpolatedString oneline
58+
syntax region swiftInterpolatedWrapper start='\v(^|[^\\])\zs\\\(\s*' end='\v\s*\)' contained containedin=swiftString contains=swiftInterpolatedString,swiftString oneline
5959
syntax match swiftInterpolatedString "\v\w+(\(\))?" contained containedin=swiftInterpolatedWrapper,swiftMultilineInterpolatedWrapper oneline
6060

6161
" Numbers

0 commit comments

Comments
 (0)