@@ -178,6 +178,13 @@ Tokens = ["one", break(1), "two", break(1), open, "three", newline, "four", clos
178178Lengths = [3, 4, 3, 60, 59, 5, 50, 4, 0]
179179```
180180
181+ #### Space
182+
183+ * Space* tokens are used to insert whitespace between tokens, as you might do
184+ with a * break* token. However, line-breaks may not occur at * space* tokens. They
185+ have a size assigned to them, corresponding to the number of spaces you wish to
186+ print.
187+
181188#### Reset
182189
183190Reset tokens are used to reset the state created by break tokens if needed, and
@@ -341,3 +348,85 @@ When we have visited all nodes in the AST, the array of printing tokens is then
341348passed on to the * scan* phase of the pretty-printer.
342349
343350See: [ ` TokenStreamCreator.swift ` ] ( ../Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift )
351+
352+ ## Scan
353+
354+ The purpose of the scan phase is to calculate the lengths of all tokens;
355+ primarily the ` break ` and ` open ` tokens. It takes as input the array of tokens
356+ produced by ` TokenStreamCreator ` .
357+
358+ There are three main variables used in the scan phase: an index stack
359+ (` delimIndexStack ` ), a running total of the lengths (` total ` ), and an array of
360+ lengths (` lengths ` ). The index stack is used to store the locations of ` open `
361+ and ` break ` tokens, since we need to look back to fill in the lengths. The
362+ running total adds the lengths of each token as we encounter it. The length
363+ array is the same size as the token array, and stores the computed lengths of
364+ the tokens.
365+
366+ After having iterated over the entire list of tokens and calculated their
367+ lengths, we then loop over the tokens and call ` print ` for each token with its
368+ corresponding length.
369+
370+ See: [ ` PrettyPrint.swift:prettyPrint() ` ] ( ../Sources/SwiftFormatPrettyPrint/PrettyPrint.swift )
371+
372+ ### Syntax Tokens
373+
374+ The length of a ` syntax ` token is the number of columns needed to print it. This
375+ value goes directly into the length array, and ` total ` is incremented by the
376+ same amount.
377+
378+ ### Open Tokens
379+
380+ If we encounter an ` open ` token, we push its index onto ` delimIndexStack ` ,
381+ initialize its length to ` -total ` , and append this value to the length array.
382+
383+ ### Close Tokens
384+
385+ At a ` close ` token, we pop an index off the top of the stack. This index will
386+ correspond to either an ` open ` or ` break ` token. If it is an ` open ` token, we
387+ add ` total ` to its length. The ` total ` variable will have been accumulating
388+ lengths since encountering the ` open ` token. The ` open ` token's length is
389+ ` total_at_close - total_at_open ` (hence the reason for initializing to
390+ ` -total ` ).
391+
392+ If the index is a ` break ` , we add ` total ` to its length. We pop the stack again
393+ to get the location of the ` open ` token corresponding to this ` close ` . We are
394+ guaranteed for this to be an ` open ` since any other ` break ` tokens will have
395+ been handled by the logic in the next subsection.
396+
397+ ### Break Tokens
398+
399+ If a ` break ` token is encountered, first check the top of the index stack. Only
400+ if the index corresponds to another ` break ` , pop it from the stack, and add
401+ ` total ` to its length. Initialize the length of the current ` break ` to ` -total `
402+ on the length array, push its index onto the stack, and then increment ` total `
403+ by the size of the ` break ` .
404+
405+ ### Newline Tokens
406+
407+ A ` newline ` token executes the same logic as for ` break ` tokens. However, we
408+ assign it a length equal to the maximum allowed line length, and increment
409+ ` total ` by the same amount. We do not push its index onto the stack since we
410+ already know its length and do not need to calculate it at a later time.
411+
412+ ### Space Tokens
413+
414+ A ` space ` token has a length equal to its ` size ` value. This is appended to the
415+ length array and added to ` total ` .
416+
417+ ### Reset Tokens
418+
419+ If a ` reset ` token is encountered, check if the top of the index stack
420+ corresponds to a ` break ` . If it does, pop it from the stack, and add ` total ` to
421+ its length in the length array. Append a length of 0 to the length array for the
422+ ` reset ` token.
423+
424+ ### Comment Tokens
425+
426+ A ` comment ` token has a length equal to the number of characters required to
427+ print it. This value is appended to the length array, and added to ` total ` .
428+
429+ ### Verbatim Tokens
430+
431+ A ` verbatim ` token has a length equal to the maximum allowed line length. This
432+ value is appended to the length array, and added to ` total ` .
0 commit comments