Fix SwiftUI indentation issues (#154)

This commit is contained in:
Satoshi SAKAO
2025-02-05 03:40:23 +09:00
committed by GitHub
parent 87ea6da902
commit a899f26994
3 changed files with 65 additions and 2 deletions

50
example/SwiftUIView.swift Normal file
View File

@@ -0,0 +1,50 @@
import SwiftUI
import PlaygroundSupport
struct ExampleView: View {
@State var isShowingSheet = false
var body: some View {
VStack {
Text("Hello, World!")
// comment
.font(.caption)
.monospaced()
// red font
.foregroundStyle(.red)
Button(action: { print("clicked") }) {
Text("click me")
}
.buttonStyle(.bordered)
Button(action: { isShowingSheet = true }) {
Text("show sheet")
}
Text("sample")
.overlay {
Circle()
.fill(.gray.opacity(0.25))
}
.lineLimit(2)
Text("sample2")
HStack {
ForEach(0...5, id: \.self) { n in
if n % 2 == 0 {
Text(n.description)
.foregroundStyle(.green)
}
else {
Text(n.description)
}
}
}
}
.padding(12)
.frame(width: 200, height: 400)
.sheet(isPresented: $isShowingSheet) {
Text("sheet")
}
}
}
PlaygroundPage.current.setLiveView(ExampleView())

View File

@@ -147,6 +147,9 @@ function! SwiftIndent(...)
call cursor(line, column) call cursor(line, column)
return openingParenCol return openingParenCol
endif endif
if numOpenParensBracketLine == 0 && numCloseParensBracketLine == 0
return indent(openingBracket) + shiftwidth()
endif
return indent(openingBracket) return indent(openingBracket)
elseif currentCloseBrackets > currentOpenBrackets elseif currentCloseBrackets > currentOpenBrackets
@@ -184,6 +187,16 @@ function! SwiftIndent(...)
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)
elseif line =~ '^\s*\.' && previous !~ '^\s*\.' && numOpenParens > 0
return previousIndent + shiftwidth()
elseif previous =~ '^\s*\.'
if s:IsCommentLine(clnum) != 0
return previousIndent - shiftwidth()
elseif line =~ '^\s*\.'
return previousIndent
else
return previousIndent - shiftwidth()
endif
else else
" - Current line is blank, and the user presses 'o' " - Current line is blank, and the user presses 'o'
return previousIndent return previousIndent