Skip to content

Commit 163ec14

Browse files
authored
Merge pull request swiftlang#167 from dabelknap/documentation
Add verbatim tokens to documentation
2 parents bb4a191 + c66d773 commit 163ec14

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

Documentation/PrettyPrinter.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ tokens and end with *close* tokens. These tokens must always be paired.
2525

2626
The different types of tokens are represented as a Token `enum` within the code.
2727
The available cases are: `syntax`, `break`, `open`, `close`, `newlines`,
28-
`comment`, and `reset`. The behavior of each of them is described below with
29-
pseudocode examples.
28+
`comment`, `reset`, and `verbatim`. The behavior of each of them is described
29+
below with pseudocode examples.
3030

3131
See: [`Token.swift`](../Sources/SwiftFormatPrettyPrint/Token.swift)
3232

@@ -239,6 +239,38 @@ Tokens = [block(" Block comment\n Second Line ")]
239239
Tokens = [docBlock(" Doc Block comment\n * Second line *")]
240240
```
241241

242+
#### Verbatim
243+
244+
Verbatim tokens are used to print text verbatim without any formatting apart
245+
from applying a global indentation. They have a length set to the maximum line
246+
width. They are typically used to handle syntax types that are classed as
247+
"unknown" by SwiftSyntax. In these cases, we don't have access to the
248+
substructure of the syntax node a manner useful for formatting, so we print them
249+
verbatim. The indentation for verbatim tokens is applied to the first line of
250+
the text. The relative indentation of subsequent lines is preserved unless they
251+
have less indentation than the first line, in which case we set the indentation
252+
of those lines equal to the first.
253+
254+
```
255+
// Consider "ifnt", an unknown syntax structure:
256+
257+
if someCondition {
258+
ifnt anotherCondition {
259+
let a = 123
260+
let b = 456
261+
}
262+
}
263+
264+
// The pretty-printer will transform this into:
265+
266+
if someCondition {
267+
ifnt anotherCondition {
268+
let a = 123
269+
let b = 456
270+
}
271+
}
272+
```
273+
242274
### Token Generation
243275

244276
Token generation begins with the abstract syntax tree (AST) of the Swift source

0 commit comments

Comments
 (0)