Skip to content

Instantly share code, notes, and snippets.

@bannzai
Created April 19, 2019 05:20
Show Gist options
  • Save bannzai/e159d6bed2da432a9dce041c3c203eff to your computer and use it in GitHub Desktop.
Save bannzai/e159d6bed2da432a9dce041c3c203eff to your computer and use it in GitHub Desktop.
FunctionParameterSyntax
public struct FunctionParameterSyntax: Syntax, _SyntaxBase, Hashable {
enum Cursor: Int {
case attributes
case firstName
case secondName
case colon
case type
case ellipsis
case defaultArgument
case trailingComma
}
let _root: SyntaxData
unowned let _data: SyntaxData
/// Creates a `FunctionParameterSyntax` node from the provided root and data.
internal init(root: SyntaxData, data: SyntaxData) {
self._root = root
self._data = data
}
public var attributes: AttributeListSyntax? {
let child = data.cachedChild(at: Cursor.attributes)
if child == nil { return nil }
return makeSyntax(root: _root, data: child!) as? AttributeListSyntax
}
/// Adds the provided `Attribute` to the node's `attributes`
/// collection.
/// - param element: The new `Attribute` to add to the node's
/// `attributes` collection.
/// - returns: A copy of the receiver with the provided `Attribute`
/// appended to its `attributes` collection.
public func addAttribute(_ element: AttributeSyntax) -> FunctionParameterSyntax {
var collection: RawSyntax
if let col = raw[Cursor.attributes] {
collection = col.appending(element.raw)
} else {
collection = RawSyntax(kind: SyntaxKind.attributeList,
layout: [element.raw], presence: .present)
}
let (root, newData) = data.replacingChild(collection,
at: Cursor.attributes)
return FunctionParameterSyntax(root: root, data: newData)
}
/// Returns a copy of the receiver with its `attributes` replaced.
/// - param newChild: The new `attributes` to replace the node's
/// current `attributes`, if present.
public func withAttributes(
_ newChild: AttributeListSyntax?) -> FunctionParameterSyntax {
let raw = newChild?.raw ?? RawSyntax.missing(.attributeList)
let (root, newData) = data.replacingChild(raw,
at: Cursor.attributes)
return FunctionParameterSyntax(root: root, data: newData)
}
public var firstName: TokenSyntax? {
let child = data.cachedChild(at: Cursor.firstName)
if child == nil { return nil }
return makeSyntax(root: _root, data: child!) as? TokenSyntax
}
/// Returns a copy of the receiver with its `firstName` replaced.
/// - param newChild: The new `firstName` to replace the node's
/// current `firstName`, if present.
public func withFirstName(
_ newChild: TokenSyntax?) -> FunctionParameterSyntax {
let raw = newChild?.raw ?? RawSyntax.missingToken(.identifier(""))
let (root, newData) = data.replacingChild(raw,
at: Cursor.firstName)
return FunctionParameterSyntax(root: root, data: newData)
}
public var secondName: TokenSyntax? {
let child = data.cachedChild(at: Cursor.secondName)
if child == nil { return nil }
return makeSyntax(root: _root, data: child!) as? TokenSyntax
}
/// Returns a copy of the receiver with its `secondName` replaced.
/// - param newChild: The new `secondName` to replace the node's
/// current `secondName`, if present.
public func withSecondName(
_ newChild: TokenSyntax?) -> FunctionParameterSyntax {
let raw = newChild?.raw ?? RawSyntax.missingToken(.identifier(""))
let (root, newData) = data.replacingChild(raw,
at: Cursor.secondName)
return FunctionParameterSyntax(root: root, data: newData)
}
public var colon: TokenSyntax? {
let child = data.cachedChild(at: Cursor.colon)
if child == nil { return nil }
return makeSyntax(root: _root, data: child!) as? TokenSyntax
}
/// Returns a copy of the receiver with its `colon` replaced.
/// - param newChild: The new `colon` to replace the node's
/// current `colon`, if present.
public func withColon(
_ newChild: TokenSyntax?) -> FunctionParameterSyntax {
let raw = newChild?.raw ?? RawSyntax.missingToken(.colon)
let (root, newData) = data.replacingChild(raw,
at: Cursor.colon)
return FunctionParameterSyntax(root: root, data: newData)
}
public var type: TypeSyntax? {
let child = data.cachedChild(at: Cursor.type)
if child == nil { return nil }
return makeSyntax(root: _root, data: child!) as? TypeSyntax
}
/// Returns a copy of the receiver with its `type` replaced.
/// - param newChild: The new `type` to replace the node's
/// current `type`, if present.
public func withType(
_ newChild: TypeSyntax?) -> FunctionParameterSyntax {
let raw = newChild?.raw ?? RawSyntax.missing(.type)
let (root, newData) = data.replacingChild(raw,
at: Cursor.type)
return FunctionParameterSyntax(root: root, data: newData)
}
public var ellipsis: TokenSyntax? {
let child = data.cachedChild(at: Cursor.ellipsis)
if child == nil { return nil }
return makeSyntax(root: _root, data: child!) as? TokenSyntax
}
/// Returns a copy of the receiver with its `ellipsis` replaced.
/// - param newChild: The new `ellipsis` to replace the node's
/// current `ellipsis`, if present.
public func withEllipsis(
_ newChild: TokenSyntax?) -> FunctionParameterSyntax {
let raw = newChild?.raw ?? RawSyntax.missingToken(.unknown(""))
let (root, newData) = data.replacingChild(raw,
at: Cursor.ellipsis)
return FunctionParameterSyntax(root: root, data: newData)
}
public var defaultArgument: InitializerClauseSyntax? {
let child = data.cachedChild(at: Cursor.defaultArgument)
if child == nil { return nil }
return makeSyntax(root: _root, data: child!) as? InitializerClauseSyntax
}
/// Returns a copy of the receiver with its `defaultArgument` replaced.
/// - param newChild: The new `defaultArgument` to replace the node's
/// current `defaultArgument`, if present.
public func withDefaultArgument(
_ newChild: InitializerClauseSyntax?) -> FunctionParameterSyntax {
let raw = newChild?.raw ?? RawSyntax.missing(.initializerClause)
let (root, newData) = data.replacingChild(raw,
at: Cursor.defaultArgument)
return FunctionParameterSyntax(root: root, data: newData)
}
public var trailingComma: TokenSyntax? {
let child = data.cachedChild(at: Cursor.trailingComma)
if child == nil { return nil }
return makeSyntax(root: _root, data: child!) as? TokenSyntax
}
/// Returns a copy of the receiver with its `trailingComma` replaced.
/// - param newChild: The new `trailingComma` to replace the node's
/// current `trailingComma`, if present.
public func withTrailingComma(
_ newChild: TokenSyntax?) -> FunctionParameterSyntax {
let raw = newChild?.raw ?? RawSyntax.missingToken(.comma)
let (root, newData) = data.replacingChild(raw,
at: Cursor.trailingComma)
return FunctionParameterSyntax(root: root, data: newData)
}
/// Determines if two `FunctionParameterSyntax` nodes are equal to each other.
public static func ==(lhs: FunctionParameterSyntax, rhs: FunctionParameterSyntax) -> Bool {
return lhs._data === rhs._data
}
/// A unique hash value for this node.
public var hashValue: Int {
return ObjectIdentifier(_data).hashValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment