Skip to content

Commit

Permalink
Fixed indentation in block which is different from Xcode's (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
ottijp authored Feb 11, 2025
1 parent 511c37e commit c76b52c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 48 deletions.
4 changes: 2 additions & 2 deletions example/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ class MainViewController: UIViewController, UITableViewDataSource {
self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Fade)
self.refreshControl.endRefreshing()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
})
})
} else {
println("Could not fetch posts!")
self.refreshControl.endRefreshing()
UIApplication.sharedApplication().networkActivityIndicatorVisible = false;
}
})
})
}

func stylePostCellAsRead(cell: UITableViewCell) {
Expand Down
19 changes: 16 additions & 3 deletions example/SwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PlaygroundSupport

struct ExampleView: View {
@State var isShowingSheet = false
@State var isShowingSheet2 = false

var body: some View {
VStack {
Expand Down Expand Up @@ -38,12 +39,24 @@ struct ExampleView: View {
}
}
}
Button(action: {
print("clicked")
isShowingSheet2 = true
}) {
Text("click me")
}
.buttonStyle(.bordered)
}
.padding(12)
.frame(width: 200, height: 400)
.sheet(isPresented: $isShowingSheet) {
Text("sheet")
}
.sheet(isPresented: Binding(get: { isShowingSheet2 },
set: { v in isShowingSheet2 = v }
)) {
Text("sheet2")
}
}
}

Expand Down Expand Up @@ -119,6 +132,6 @@ struct ViewModifierPatternView: View {
}

PlaygroundPage.current.setLiveView(VStack {
ViewModifierPatternView()
ExampleView()
})
ViewModifierPatternView()
ExampleView()
})
68 changes: 45 additions & 23 deletions example/example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ let data = NSData(contentsOfFile: path) else
}

UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: {
view.backgroundColor = UIColor.redColor()
}, completion: { finished in
print("indent?")
})
view.backgroundColor = UIColor.redColor()
}, completion: { finished in
print("indent?")
})

// Indent last line should hold
self.init(className: "Item", dictionary: [
Expand All @@ -378,11 +378,11 @@ public func find(closure: @noescape Element throws -> Bool) rethrows -> Element?
}

UIView.animate(withDuration: 0.2, animations: {
self.foo.alpha = 1.0
self.bar.alpha = 1.0
}, completion: { _ in
completion()
})
self.foo.alpha = 1.0
self.bar.alpha = 1.0
}, completion: { _ in
completion()
})

A.b().application(
application, didFinishLaunchingWithOptions: launchOptions)
Expand All @@ -397,26 +397,48 @@ struct Foo {
}
func caller1() {
callee(delegate1: {
print("caller1 delegate1")
}) {
print("caller1 delegate2")
print("caller1 delegate1")
}) {
print("caller1 delegate2")
}
}
func caller2() {
callee(delegate1: {
print("caller1 delegate1")
}, delegate2: {
print("caller1 delegate2")
})
print("caller1 delegate1")
}, delegate2: {
print("caller1 delegate2")
})
}
func caller3() {
callee(delegate1: {
print("caller1 delegate1")
}, delegate2: {
print([0, 1].map {
$0 + 1
})
print("caller1 delegate2")
})
print("caller1 delegate1")
}, delegate2: {
print([0, 1].map {
$0 + 1
})
print("caller1 delegate2")
})
}
}

struct Bar {
func f(title: String, delegate: (Int) -> Void) {
print(title)
delegate(42)
}
}

Bar().f(
title: "the title"
) { n in
print("n")
}

func createDelegate() -> ((Int) -> Void) {
return { n in
print("n")
}
}

Bar().f(title: "the title", delegate: createDelegate(
))
47 changes: 27 additions & 20 deletions indent/swift.vim
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,7 @@ function! SwiftIndent(...)
let numOpenParensBracketLine = s:NumberOfMatches("(", bracketLine, openingBracket)
let numCloseParensBracketLine = s:NumberOfMatches(")", bracketLine, openingBracket)
if numOpenParensBracketLine > numCloseParensBracketLine
let line = line(".")
let column = col(".")
call cursor(openingParen, column)
let openingParenCol = searchpairpos("(", "", ")", "bWn", "s:IsExcludedFromIndent()")[1]
call cursor(line, column)
return openingParenCol
return indent(openingBracket)
endif
if numOpenParensBracketLine == 0 && numCloseParensBracketLine == 0
return indent(openingBracket) + shiftwidth()
Expand All @@ -309,17 +304,26 @@ function! SwiftIndent(...)
return indent(openingParenPos[0])
elseif numOpenParensBracketLine > numCloseParensBracketLine
let openingParenPos = s:SearchOpeningParenPos([line("."), col(".")])
return openingParenPos[1]
return indent(openingParenPos[0])
endif

return indent(openingBracket)
elseif line =~ '^\s*)$'
let line = line(".")
let column = col(".")
call cursor(line, 1)
call cursor(clnum, 1)
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
call cursor(line, column)
call cursor(clnum, column)
return indent(openingParen)
elseif line =~ '^\s*).*'
let firstCloseParenCol = stridx(line, ')')
let openingParenPos = s:SearchOpeningParenPos([clnum, firstCloseParenCol])
if s:SearchOpeningParenPos(openingParenPos)[0] == 0
return indent(openingParenPos[0])
elseif getline(openingParenPos[0]) =~ '($'
return indent(openingParenPos[0])
else
return openingParenPos[1] - 1
endif
else
let dotIndent = DotIndent(line, previous, previousNum, previousIndent, numCloseBrackets, numOpenBrackets, numCloseParens, numOpenParens, clnum)
if dotIndent != -1
Expand All @@ -344,9 +348,9 @@ function! SwiftIndent(...)
endif

if numCloseParens > numOpenParens
let line = line(".")
let line = previousNum
let column = col(".")
call cursor(line - 1, column)
call cursor(line, column)
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
call cursor(line, column)
return indent(openingParen)
Expand All @@ -370,7 +374,11 @@ function! SwiftIndent(...)

let previousParen = match(previous, '\v\($')
if previousParen != -1
return previousIndent + shiftwidth()
if line =~ '^\s*).*'
return previousIndent
else
return previousIndent + shiftwidth()
endif
endif

let line = line(".")
Expand All @@ -384,12 +392,8 @@ function! SwiftIndent(...)
endif

if numOpenBrackets > numCloseBrackets
let line = line(".")
let column = col(".")
call cursor(previousNum, column)
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
call cursor(line, column)
return openingParen + 1
let nearestBlockStartLnum = s:SearchBackwardLineOrBlock(previousNum, '\s*[^ \t]\+')
return indent(nearestBlockStartLnum) + shiftwidth()
endif

" - Previous line has close then open braces, indent previous + 1 'sw'
Expand Down Expand Up @@ -426,11 +430,14 @@ function! SwiftIndent(...)

" The previous line opens a closure and doesn't close it
if numOpenBrackets > numCloseBrackets
return previousParen + shiftwidth()
return previousIndent + shiftwidth()
endif

call setpos(".", savePosition)
return previousParen

elseif line =~ '^\s*)$'
return previousIndent
endif

return cindent
Expand Down

0 comments on commit c76b52c

Please sign in to comment.