Skip to content

Commit 30ce897

Browse files
authored
Fix closing bracket indentation issue (#156)
1 parent a899f26 commit 30ce897

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

example/example.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,34 @@ A.b().application(
389389

390390
A.application(b(),
391391
application, didFinishLaunchingWithOptions: launchOptions)
392+
393+
struct Foo {
394+
func callee(delegate1: () -> Void, delegate2: () -> Void) {
395+
delegate1()
396+
delegate2()
397+
}
398+
func caller1() {
399+
callee(delegate1: {
400+
print("caller1 delegate1")
401+
}) {
402+
print("caller1 delegate2")
403+
}
404+
}
405+
func caller2() {
406+
callee(delegate1: {
407+
print("caller1 delegate1")
408+
}, delegate2: {
409+
print("caller1 delegate2")
410+
})
411+
}
412+
func caller3() {
413+
callee(delegate1: {
414+
print("caller1 delegate1")
415+
}, delegate2: {
416+
print([0, 1].map {
417+
$0 + 1
418+
})
419+
print("caller1 delegate2")
420+
})
421+
}
422+
}

indent/swift.vim

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ function! s:IsSyntaxNameExcludedFromIndent(name)
5151
return a:name ==# "swiftComment" || a:name ==# "swiftString" || a:name ==# "swiftInterpolatedWrapper" || a:name ==# "swiftMultilineInterpolatedWrapper" || a:name ==# "swiftMultilineString"
5252
endfunction
5353

54+
" Description: Search for the position of the opening parenthesis '(' starting from the specified position.
55+
" Parameters:
56+
" startingPosition - A list [line_number, column_number] representing the position to start the search.
57+
" Returns: A list [line_number, column_number] representing the position of the opening parenthesis.
58+
function! s:SearchOpeningParenPos(startingPosition)
59+
let currentLine = line(".")
60+
let currentColumn = col(".")
61+
call cursor(a:startingPosition[0], a:startingPosition[1])
62+
let openingParen = searchpairpos("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
63+
call cursor(currentLine, currentColumn)
64+
return openingParen
65+
endfunction
66+
67+
5468
function! s:IsCommentLine(lnum)
5569
return synIDattr(synID(a:lnum,
5670
\ match(getline(a:lnum), "\\S") + 1, 0), "name")
@@ -164,19 +178,11 @@ function! SwiftIndent(...)
164178
let numOpenParensBracketLine = s:NumberOfMatches("(", bracketLine, openingBracket)
165179
let numCloseParensBracketLine = s:NumberOfMatches(")", bracketLine, openingBracket)
166180
if numCloseParensBracketLine > numOpenParensBracketLine
167-
let line = line(".")
168-
let column = col(".")
169-
call cursor(openingParen, column)
170-
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
171-
call cursor(line, column)
172-
return indent(openingParen)
181+
let openingParenPos = s:SearchOpeningParenPos([openingBracket, 1])
182+
return indent(openingParenPos[0])
173183
elseif numOpenParensBracketLine > numCloseParensBracketLine
174-
let line = line(".")
175-
let column = col(".")
176-
call cursor(openingParen, column)
177-
let openingParenCol = searchpairpos("(", "", ")", "bWn", "s:IsExcludedFromIndent()")[1]
178-
call cursor(line, column)
179-
return openingParenCol
184+
let openingParenPos = s:SearchOpeningParenPos([line("."), col(".")])
185+
return openingParenPos[1]
180186
endif
181187

182188
return indent(openingBracket)

0 commit comments

Comments
 (0)