Swift Package plugins for snippets

This tweet is being shared around which shows a code snippet expending based on the currently defined properties of a Codable type. I've only created static code snippets with placeholders. There are limited docs so I have mostly figured it out by trial and error.

I've shared code snippets via GitHub with a script which copies the files in place so that Xcode can use them. Now what would make more sense would be to bring in collections of snippets via Swift Packages. Static snippets with placeholders would be useful. Allowing for accessing the current context of that type like this Codable example would give the Swift community a powerful tool to enhance what we can do with Xcode.

I created XcodeCodeSnippets over 10 years ago for Objective-C. It includes a script to manage these snippets. Adding snippets is done for the local installation so all Xcode projects would be affected. Having snippets collections defined for a package with SPM could limit the scope to just the packages where those snippets are appropriate. Xcode could also bring in snippet collections for all projects/packages for general use snippets.

Should this become a more formal proposal?

1 Like

I would love to see snippets able to be included in packages, ideally as part of additional documentation for a function or a class. All the boilerplate usage could be both explained and provided via the snippet which could even include inline documentation as part of the snippet. This would be super helpful. Did this ever make it into a formal proposal?

Macros looks like the solution for boilerplate code which is even better than expanding code snippets.

1 Like

Snippets still seem to have their own purpose. For example the "view" snippet expands to:

struct /*@START_MENU_TOKEN@*/MyView/*@END_MENU_TOKEN@*/: View {
  var body: some View {
    /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Hello, world!@*/Text("Hello, world!")/*@END_MENU_TOKEN@*/
  }
}

Which is boilerplate that can't be covered by a macro.