From b09ff5e37a78d5f7f3471a26a69bc621409960d0 Mon Sep 17 00:00:00 2001 From: Satoshi SAKAO Date: Tue, 11 Feb 2025 19:46:01 +0900 Subject: [PATCH] Fix block indentation (#155) --- example/MainViewController.swift | 4 +- example/SwiftUIView.swift | 19 +++++++-- example/example.swift | 68 +++++++++++++++++++++----------- indent/swift.vim | 47 ++++++++++++---------- 4 files changed, 90 insertions(+), 48 deletions(-) diff --git a/example/MainViewController.swift b/example/MainViewController.swift index dec3b57..1ee93b5 100644 --- a/example/MainViewController.swift +++ b/example/MainViewController.swift @@ -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) { diff --git a/example/SwiftUIView.swift b/example/SwiftUIView.swift index 5f9794b..32262b0 100644 --- a/example/SwiftUIView.swift +++ b/example/SwiftUIView.swift @@ -3,6 +3,7 @@ import PlaygroundSupport struct ExampleView: View { @State var isShowingSheet = false + @State var isShowingSheet2 = false var body: some View { VStack { @@ -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") + } } } @@ -119,6 +132,6 @@ struct ViewModifierPatternView: View { } PlaygroundPage.current.setLiveView(VStack { - ViewModifierPatternView() - ExampleView() - }) + ViewModifierPatternView() + ExampleView() +}) diff --git a/example/example.swift b/example/example.swift index f6eb0b0..428c0ea 100644 --- a/example/example.swift +++ b/example/example.swift @@ -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: [ @@ -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) @@ -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( +)) diff --git a/indent/swift.vim b/indent/swift.vim index a82864d..a65b38a 100644 --- a/indent/swift.vim +++ b/indent/swift.vim @@ -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() @@ -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 @@ -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) @@ -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(".") @@ -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' @@ -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