Fixed indentation in block which is different from Xcode's (#159)

This commit is contained in:
Satoshi SAKAO
2025-02-12 02:33:03 +09:00
committed by GitHub
parent 511c37e150
commit c76b52c68b
4 changed files with 90 additions and 48 deletions

View File

@@ -3,6 +3,7 @@ import PlaygroundSupport
struct ExampleView: View { struct ExampleView: View {
@State var isShowingSheet = false @State var isShowingSheet = false
@State var isShowingSheet2 = false
var body: some View { var body: some View {
VStack { VStack {
@@ -38,12 +39,24 @@ struct ExampleView: View {
} }
} }
} }
Button(action: {
print("clicked")
isShowingSheet2 = true
}) {
Text("click me")
}
.buttonStyle(.bordered)
} }
.padding(12) .padding(12)
.frame(width: 200, height: 400) .frame(width: 200, height: 400)
.sheet(isPresented: $isShowingSheet) { .sheet(isPresented: $isShowingSheet) {
Text("sheet") Text("sheet")
} }
.sheet(isPresented: Binding(get: { isShowingSheet2 },
set: { v in isShowingSheet2 = v }
)) {
Text("sheet2")
}
} }
} }
@@ -121,4 +134,4 @@ struct ViewModifierPatternView: View {
PlaygroundPage.current.setLiveView(VStack { PlaygroundPage.current.setLiveView(VStack {
ViewModifierPatternView() ViewModifierPatternView()
ExampleView() ExampleView()
}) })

View File

@@ -354,9 +354,9 @@ let data = NSData(contentsOfFile: path) else
UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: { UIView.animateWithDuration(duration, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: {
view.backgroundColor = UIColor.redColor() view.backgroundColor = UIColor.redColor()
}, completion: { finished in }, completion: { finished in
print("indent?") print("indent?")
}) })
// Indent last line should hold // Indent last line should hold
self.init(className: "Item", dictionary: [ self.init(className: "Item", dictionary: [
@@ -380,9 +380,9 @@ public func find(closure: @noescape Element throws -> Bool) rethrows -> Element?
UIView.animate(withDuration: 0.2, animations: { UIView.animate(withDuration: 0.2, animations: {
self.foo.alpha = 1.0 self.foo.alpha = 1.0
self.bar.alpha = 1.0 self.bar.alpha = 1.0
}, completion: { _ in }, completion: { _ in
completion() completion()
}) })
A.b().application( A.b().application(
application, didFinishLaunchingWithOptions: launchOptions) application, didFinishLaunchingWithOptions: launchOptions)
@@ -420,3 +420,25 @@ struct Foo {
}) })
} }
} }
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(
))

View File

@@ -281,12 +281,7 @@ function! SwiftIndent(...)
let numOpenParensBracketLine = s:NumberOfMatches("(", bracketLine, openingBracket) let numOpenParensBracketLine = s:NumberOfMatches("(", bracketLine, openingBracket)
let numCloseParensBracketLine = s:NumberOfMatches(")", bracketLine, openingBracket) let numCloseParensBracketLine = s:NumberOfMatches(")", bracketLine, openingBracket)
if numOpenParensBracketLine > numCloseParensBracketLine if numOpenParensBracketLine > numCloseParensBracketLine
let line = line(".") return indent(openingBracket)
let column = col(".")
call cursor(openingParen, column)
let openingParenCol = searchpairpos("(", "", ")", "bWn", "s:IsExcludedFromIndent()")[1]
call cursor(line, column)
return openingParenCol
endif endif
if numOpenParensBracketLine == 0 && numCloseParensBracketLine == 0 if numOpenParensBracketLine == 0 && numCloseParensBracketLine == 0
return indent(openingBracket) + shiftwidth() return indent(openingBracket) + shiftwidth()
@@ -309,17 +304,26 @@ function! SwiftIndent(...)
return indent(openingParenPos[0]) return indent(openingParenPos[0])
elseif numOpenParensBracketLine > numCloseParensBracketLine elseif numOpenParensBracketLine > numCloseParensBracketLine
let openingParenPos = s:SearchOpeningParenPos([line("."), col(".")]) let openingParenPos = s:SearchOpeningParenPos([line("."), col(".")])
return openingParenPos[1] return indent(openingParenPos[0])
endif endif
return indent(openingBracket) return indent(openingBracket)
elseif line =~ '^\s*)$' elseif line =~ '^\s*)$'
let line = line(".")
let column = col(".") let column = col(".")
call cursor(line, 1) call cursor(clnum, 1)
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()") let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
call cursor(line, column) call cursor(clnum, column)
return indent(openingParen) 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 else
let dotIndent = DotIndent(line, previous, previousNum, previousIndent, numCloseBrackets, numOpenBrackets, numCloseParens, numOpenParens, clnum) let dotIndent = DotIndent(line, previous, previousNum, previousIndent, numCloseBrackets, numOpenBrackets, numCloseParens, numOpenParens, clnum)
if dotIndent != -1 if dotIndent != -1
@@ -344,9 +348,9 @@ function! SwiftIndent(...)
endif endif
if numCloseParens > numOpenParens if numCloseParens > numOpenParens
let line = line(".") let line = previousNum
let column = col(".") let column = col(".")
call cursor(line - 1, column) call cursor(line, column)
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()") let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
call cursor(line, column) call cursor(line, column)
return indent(openingParen) return indent(openingParen)
@@ -370,8 +374,12 @@ function! SwiftIndent(...)
let previousParen = match(previous, '\v\($') let previousParen = match(previous, '\v\($')
if previousParen != -1 if previousParen != -1
if line =~ '^\s*).*'
return previousIndent
else
return previousIndent + shiftwidth() return previousIndent + shiftwidth()
endif endif
endif
let line = line(".") let line = line(".")
let column = col(".") let column = col(".")
@@ -384,12 +392,8 @@ function! SwiftIndent(...)
endif endif
if numOpenBrackets > numCloseBrackets if numOpenBrackets > numCloseBrackets
let line = line(".") let nearestBlockStartLnum = s:SearchBackwardLineOrBlock(previousNum, '\s*[^ \t]\+')
let column = col(".") return indent(nearestBlockStartLnum) + shiftwidth()
call cursor(previousNum, column)
let openingParen = searchpair("(", "", ")", "bWn", "s:IsExcludedFromIndent()")
call cursor(line, column)
return openingParen + 1
endif endif
" - Previous line has close then open braces, indent previous + 1 'sw' " - 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 " The previous line opens a closure and doesn't close it
if numOpenBrackets > numCloseBrackets if numOpenBrackets > numCloseBrackets
return previousParen + shiftwidth() return previousIndent + shiftwidth()
endif endif
call setpos(".", savePosition) call setpos(".", savePosition)
return previousParen return previousParen
elseif line =~ '^\s*)$'
return previousIndent
endif endif
return cindent return cindent