Skip to content

Conversation

@amyworrall
Copy link
Contributor

[RFC] Swift Macro support for Node boilerplate

@amyworrall
Copy link
Contributor Author

I'd like to get people's feedback on this! Obviously we can't land it until Xcode 15 is out of beta, but this can be a taste of things to come.

What I've done here is generated all the boilerplate for node initialisation, cloning, encoding, property access etc using a Swift macro. You might use it like this:

public enum ListType: String, Codable {
  case bullet
  case number
  case check
}

extension NodeType {
  public static let list = NodeType(rawValue: "list")
}

@LexicalNode(.list)
public class ListNode: ElementNode {
  private var _listType: ListType = .bullet
  private var _start: Int = 1
}

and the macro would fill in all the extra boilerplate that you would usually have to write yourself, like so:

open override class var type: NodeType {
  .list
}

enum CodingKeys: String, CodingKey {
    case listType
    case start

}

public convenience init(listType: ListType , start: Int ) {
  self.init(listType: listType, start: start, key: nil)
}

public required init(listType: ListType , start: Int , key: NodeKey?) {
  self._listType = listType
  self._start = start

  super.init(key)
}

override public func clone() -> Self {
  Self (listType: _listType, start: _start, key: key)
}

override public func encode(to encoder: Encoder) throws {
  try super.encode(to: encoder)
  var container = encoder.container(keyedBy: CodingKeys.self)
  try container.encode(_listType, forKey: .listType)
  try container.encode(_start, forKey: .start)

}

public required init(from decoder: Decoder) throws {
  let container = try decoder.container(keyedBy: CodingKeys.self)
  self._listType = try container.decode(ListType .self, forKey: .listType)
  self._start = try container.decode(Int .self, forKey: .start)

  try super.init(from: decoder)
}

public var listType: ListType  {
  return getLatest()._listType
}

public func getListType() -> ListType  {
  return getLatest()._listType
}

public func setListType(_ val: ListType ) throws {
  try getWritable()._listType = val
}

public var start: Int  {
  return getLatest()._start
}

public func getStart() -> Int  {
  return getLatest()._start
}

public func setStart(_ val: Int ) throws {
  try getWritable()._start = val
}

I think you'll agree this is a lot easier!

It won't be compulsory to write Lexical nodes in this way. If you need more control, you can still write nodes by implementing these methods manually.

At the moment, the macro doesn't allow much customisation of the behaviour. It's all or nothing -- for example, you can't implement your own encode method but have the macro do everything else. I'd like to improve this in the future, making the macro ignore methods that have already been written manually.

@iBenjamin
Copy link

@amyworrall Xcode 15 is released can this pr be merged?

@facebook-github-bot
Copy link
Contributor

Hi @amyworrall!

Thank you for your pull request.

We require contributors to sign our Contributor License Agreement, and yours needs attention.

You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants