Skip to content

Instantly share code, notes, and snippets.

@fwcd
Last active March 17, 2023 12:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwcd/b70cc91a27a4d00baf869785a3bf8a6d to your computer and use it in GitHub Desktop.
Save fwcd/b70cc91a27a4d00baf869785a3bf8a6d to your computer and use it in GitHub Desktop.
Google Summer of Code 2022

Overview

My Google Summer of Code 2022 project mainly focused on rewriting parts of the SwiftSyntax library using its own SwiftSyntaxBuilder DSL, more specifically, porting the gyb templates in SwiftSyntaxBuilder to type-safe Swift code, written using the library itself. During this bootstrapping process, many improvements to the DSL were added, inconveniences in the (quite large) API surface fixed and thoroughly tested. The result is a robust and ergonomic API for generating Swift code that has proven to be useful in the context of a medium-sized codebase.

The Project: Bootstrapping SwiftSyntaxBuilder

The first step was to set up the build infrastructure. In particular this required adding a new target that, when executed, produced the generated Swift files in SwiftSyntaxBuilder/generated:

flowchart LR
  subgraph Sources
    SwiftSyntax
    SwiftSyntaxBuilder
    generate-swift-syntax-builder
  end
  subgraph Products
    executable(generate-swift-syntax-builder)
  end
  SwiftSyntax -- is used by --> SwiftSyntaxBuilder
  SwiftSyntax -- is used by --> generate-swift-syntax-builder
  SwiftSyntaxBuilder -- is used by --> generate-swift-syntax-builder
  generate-swift-syntax-builder -- compiles to --> executable
  executable -- generates --> SwiftSyntaxBuilder

In the second phase, the project mainly focused on improving the DSL, for example by adding convenience initializers and making sure that trailing closures and result builders make SwiftSyntaxBuilder code both easy to read and write. A specific example of this is the nice duality between trailing closures and generated blocks, which makes use of Swift's expressiveness to model the language itself concisely in the language. For example, consider the following snippet of Swift code:

struct Point {
  let x: Int
  let y: Int
}

In SwiftSyntaxBuilder this would be expressed as:

StructDecl(identifier: "Point") {
  VariableDecl(.let, name: "x", type: "Int")
  VariableDecl(.let, name: "y", type: "Int")
}

The final step consisted of translating the gyb templates to Swift. During this process, many more improvements to the DSL naturally came up and made their way into the project. A detailed breakdown of the PRs to swift-syntax can be found below.

Week 1: 2022/05/30 - 2022/06/06

Week 2: 2022/06/06 - 2022/06/13

Week 3: 2022/06/13 - 2022/06/20

Week 4: 2022/06/20 - 2022/06/27

Week 5: 2022/06/27 - 2022/07/04

Week 6: 2022/07/04 - 2022/07/11

Week 7: 2022/07/11 - 2022/07/18

Week 8: 2022/07/18 - 2022/07/25

Week 9: 2022/07/25 - 2022/08/01

Week 10: 2022/08/01 - 2022/08/08

Week 11: 2022/08/08 - 2022/08/15

Week 12: 2022/08/15 - 2022/08/22

Week 13: 2022/08/22 - 2022/08/29

Week 14: 2022/08/29 - 2022/09/05

Week 15: 2022/09/05 - 2022/09/12

Side Quest: Extending SourceKit-LSP

In addition to the aforementioned improvements to SwiftSyntax, I also worked on SourceKit-LSP, the language server for Swift: In addition to migrating the inlay hints from my GSoC project last year to upstream LSP inlay hints, I have added two major features to SourceKit-LSP, namely Call Hierarchy and Type Hierarchy. These let the user query caller/callee and subtypes/supertype hierarchies for Swift, thereby making code navigation more convenient.

Closing Thoughts

As last year, it has been a pleasure working on Swift and its surrounding projects! Robust tooling for Swift source code is essential to many real-world applications and having a production-ready, type-safe DSL for generating Swift code comprises a large part of this goal.

Special thanks to my mentor Alex and the amazing community for the great collaboration, quick reviews, intriguing ideas surrounding this project and SwiftSyntax in general!

@shindodkar
Copy link

Hii, I'm new to open source contribution program so, help to figure out were to start . I'm so eger to work on these projects please, get some guidelines.

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