|
| 1 | +// |
| 2 | +// PatternBindingSyntax+.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by p-x9 on 2023/06/17. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import SwiftSyntax |
| 11 | + |
| 12 | +extension PatternBindingSyntax { |
| 13 | + var setter: AccessorDeclSyntax? { |
| 14 | + get { |
| 15 | + if case let .accessors(list) = accessor { |
| 16 | + return list.accessors.first(where: { |
| 17 | + $0.accessorKind.tokenKind == .keyword(.set) |
| 18 | + }) |
| 19 | + } |
| 20 | + return nil |
| 21 | + } |
| 22 | + |
| 23 | + set { |
| 24 | + // NOTE: Be careful that setter cannot be implemented without a getter. |
| 25 | + setNewAccessor(kind: .keyword(.set), newValue: newValue) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + var getter: AccessorDeclSyntax? { |
| 30 | + get { |
| 31 | + switch accessor { |
| 32 | + case let .accessors(list): |
| 33 | + return list.accessors.first(where: { |
| 34 | + $0.accessorKind.tokenKind == .keyword(.get) |
| 35 | + }) |
| 36 | + case let .getter(body): |
| 37 | + return AccessorDeclSyntax(accessorKind: .keyword(.get), body: body) |
| 38 | + case .none: |
| 39 | + return nil |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + set { |
| 44 | + switch accessor { |
| 45 | + case .getter, .none: |
| 46 | + if let newValue { |
| 47 | + if let body = newValue.body { |
| 48 | + accessor = .getter(body) |
| 49 | + } else { |
| 50 | + let accessors = AccessorListSyntax { |
| 51 | + newValue |
| 52 | + } |
| 53 | + accessor = .accessors(.init(accessors: accessors)) |
| 54 | + } |
| 55 | + } else { |
| 56 | + accessor = .none |
| 57 | + } |
| 58 | + |
| 59 | + case let .accessors(block): |
| 60 | + var accessors = block.accessors |
| 61 | + for (i, accessor) in block.accessors.enumerated() { |
| 62 | + guard accessor.accessorKind.tokenKind == .keyword(.get) else { |
| 63 | + continue |
| 64 | + } |
| 65 | + if let newValue { |
| 66 | + accessors = accessors.replacing(childAt: i, with: newValue) |
| 67 | + } else { |
| 68 | + accessors = accessors.removing(childAt: i) |
| 69 | + } |
| 70 | + break |
| 71 | + } |
| 72 | + let newBlock = block.with(\.accessors, accessors) |
| 73 | + accessor = .accessors(newBlock) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + var isGetOnly: Bool { |
| 79 | + if initializer != nil { |
| 80 | + return false |
| 81 | + } |
| 82 | + if case let .accessors(list) = accessor, |
| 83 | + list.accessors.contains(where: { $0.accessorKind.tokenKind == .keyword(.set) }) { |
| 84 | + return false |
| 85 | + } |
| 86 | + if accessor == nil && initializer == nil { |
| 87 | + return false |
| 88 | + } |
| 89 | + return true |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +extension PatternBindingSyntax { |
| 94 | + var willSet: AccessorDeclSyntax? { |
| 95 | + get { |
| 96 | + if case let .accessors(list) = accessor { |
| 97 | + return list.accessors.first(where: { |
| 98 | + $0.accessorKind.tokenKind == .keyword(.willSet) |
| 99 | + }) |
| 100 | + } |
| 101 | + return nil |
| 102 | + } |
| 103 | + set { |
| 104 | + // NOTE: Be careful that willSet cannot be implemented without a setter. |
| 105 | + setNewAccessor(kind: .keyword(.willSet), newValue: newValue) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + var didSet: AccessorDeclSyntax? { |
| 110 | + get { |
| 111 | + if case let .accessors(list) = accessor { |
| 112 | + return list.accessors.first(where: { |
| 113 | + $0.accessorKind.tokenKind == .keyword(.didSet) |
| 114 | + }) |
| 115 | + } |
| 116 | + return nil |
| 117 | + } |
| 118 | + set { |
| 119 | + // NOTE: Be careful that willSet cannot be implemented without a setter. |
| 120 | + setNewAccessor(kind: .keyword(.willSet), newValue: newValue) |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +extension PatternBindingSyntax { |
| 126 | + // NOTE: - getter requires extra steps and should not be used. |
| 127 | + private mutating func setNewAccessor(kind: TokenKind, newValue: AccessorDeclSyntax?) { |
| 128 | + var newAccessor: Accessor? |
| 129 | + |
| 130 | + switch accessor { |
| 131 | + case let .getter(body): |
| 132 | + guard let newValue else { return } |
| 133 | + newAccessor = .accessors(.init(accessors: AccessorListSyntax { |
| 134 | + AccessorDeclSyntax(accessorKind: .keyword(.get), body: body) |
| 135 | + newValue |
| 136 | + })) |
| 137 | + case let .accessors(block): |
| 138 | + var accessors = block.accessors |
| 139 | + for (i, accessor) in block.accessors.enumerated() { |
| 140 | + guard accessor.accessorKind.tokenKind == kind else { |
| 141 | + continue |
| 142 | + } |
| 143 | + if let newValue { |
| 144 | + accessors = accessors.replacing(childAt: i, with: newValue) |
| 145 | + } else { |
| 146 | + accessors = accessors.removing(childAt: i) |
| 147 | + } |
| 148 | + break |
| 149 | + } |
| 150 | + let newBlock = block.with(\.accessors, accessors) |
| 151 | + newAccessor = .accessors(newBlock) |
| 152 | + case .none: |
| 153 | + guard let newValue else { return } |
| 154 | + newAccessor = .accessors(.init(accessors: AccessorListSyntax { |
| 155 | + newValue |
| 156 | + })) |
| 157 | + } |
| 158 | + |
| 159 | + self.accessor = newAccessor |
| 160 | + } |
| 161 | +} |
0 commit comments