What are the actual formatting rules for Swift Snippets?

i’ve been examining the snippet specification from SE-0356 and i’m having trouble understanding what the actual snippet formatting rules are, based on:

Writing a snippet
A snippet file might look like the following:

// The first contiguous line comments
// serve as the snippet's short description.

func someCodeToShow() {
    print("Hello, world!")
}

// snippet.hide

func someCodeToHide() {
    print("Some demo message")
}

// Still hidden
someCodeToHide()

// snippet.show

someCodeToShow()
  1. do doccomments (///) count as valid line comments?
  2. is any non-comment code (like an import statement) allowed before the line comments?
  3. is leading whitespace allowed for the snippet control comments? can control comments appear inside an indented code block?
  4. does // snippet.hide also delete the newline before the comment?

FYI, the //snippet. commands seem to now be //MARK: comments

By experimentation:
snippetFile.swft

import Foundation

//! A Very simple demo of a snippets file

//
//  SnippetFile.swift
//
//
//  Created by Diggory Laycock on 09/02/2024.
//


import Snippets

//	This code will be shown
test1()

// MARK: Hide
// Demo-ing MARK Hide/Show.

//	Nothing in this section (until the 'Show' mark below) will be shown to the user in the snippets UI

test1()
test2()

aFunctionThatTheEndUserShouldNotSee()

// MARK: Show

//	This code will be shown

///	Runs both simple test functions
func runBothTests() {
	test1()
	test2()
}

//	Here comes the last func in the file
// MARK: Hide
//	You can't see me!
// MARK: Show
///	The last func in this snippet file
func lastFunc() {
	print("This is the last func in the snippet file")
	// MARK: Hide
	//	You still can't see me!
	// MARK: Show
}

and as viewed in swift package learn:

# SnippetFile

// A Very simple demo of a snippets file

import Foundation

//
//  SnippetFile.swift
//
//
//  Created by Diggory Laycock on 09/02/2024.
//

import Snippets

//	This code will be shown
test1()

//	This code will be shown

///	Runs both simple test functions
func runBothTests() {
	test1()
	test2()
}

//	Here comes the last func in the file
///	The last func in this snippet file
func lastFunc() {
	print("This is the last func in the snippet file")
}


Not sure if that helps you in any way, but your post lead me to try snippets out.

1 Like