Skip to content

Support async and throws on property/subscript getters. #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,22 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {

override func visit(_ node: AccessorDeclSyntax) -> SyntaxVisitorContinueKind {
arrangeAttributeList(node.attributes)

if let asyncKeyword = node.asyncKeyword {
if node.throwsKeyword != nil {
before(asyncKeyword, tokens: .break, .open)
} else {
before(asyncKeyword, tokens: .break)
}
}

if let throwsKeyword = node.throwsKeyword {
before(node.throwsKeyword, tokens: .break)
if node.asyncKeyword != nil {
after(throwsKeyword, tokens: .close)
}
}

arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
return .visitChildren
}
Expand Down
157 changes: 157 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/AccessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,161 @@ final class AccessorTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)
}

func testPropertyEffectsWithBodyAfter() {
let input =
"""
var x: T {
get async {
foo()
bar()
}
}
var x: T {
get throws {
foo()
bar()
}
}
var x: T {
get async throws {
foo()
bar()
}
}
"""

assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)

let expected16 =
"""
var x: T {
get async {
foo()
bar()
}
}
var x: T {
get throws {
foo()
bar()
}
}
var x: T {
get
async throws
{
foo()
bar()
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected16, linelength: 16)

let expected10 =
"""
var x: T {
get
async
{
foo()
bar()
}
}
var x: T {
get
throws
{
foo()
bar()
}
}
var x: T {
get
async
throws
{
foo()
bar()
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected10, linelength: 10)
}

func testPropertyEffectsWithNoBodyAfter() {
let input =
"""
protocol P {
var x: T { get async }
var x: T { get throws }
var x: T { get async throws }
}
"""

assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)

let expected20 =
"""
protocol P {
var x: T {
get async
}
var x: T {
get throws
}
var x: T {
get async throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)

let expected18 =
"""
protocol P {
var x: T {
get async
}
var x: T {
get throws
}
var x: T {
get
async throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)

let expected12 =
"""
protocol P {
var x: T {
get
async
}
var x: T {
get
throws
}
var x: T {
get
async
throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected12, linelength: 12)
}
}
111 changes: 111 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/SubscriptDeclTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,115 @@ final class SubscriptDeclTests: PrettyPrintTestCase {
"""
assertPrettyPrintEqual(input: input, expected: wrapped, linelength: 28)
}

func testAccessorEffectsWithBodyAfter() {
let input =
"""
struct X {
subscript(i: Int) -> T {
get async throws {
foo()
bar()
}
}
}
"""

assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)

let expected18 =
"""
struct X {
subscript(
i: Int
) -> T {
get
async throws
{
foo()
bar()
}
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)

let expected12 =
"""
struct X {
subscript(
i: Int
) -> T {
get
async
throws
{
foo()
bar()
}
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected12, linelength: 12)
}

func testAccessorEffectsWithNoBodyAfter() {
let input =
"""
protocol P {
subscript(i: Int) -> T { get async throws }
}
"""

assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 80)

let expected20 =
"""
protocol P {
subscript(i: Int)
-> T
{
get async throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected20, linelength: 20)

let expected18 =
"""
protocol P {
subscript(
i: Int
) -> T {
get
async throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected18, linelength: 18)

let expected16 =
"""
protocol P {
subscript(
i: Int
) -> T {
get
async
throws
}
}

"""

assertPrettyPrintEqual(input: input, expected: expected16, linelength: 16)
}
}