We have a "pretty printing" feature (ew, I don't like that phrase) that should reformat programs with preferred spacing for readability. For example, if we have this program:
There are many reasons for this, one of which is because the implementation for this is distributed across Spaces.ts and Node.ts, which leads to many inconsistencies. It's also inefficient, and confusing to change, and the various formatting preferences defined in each node's Grammar are also not always respected consistently.
Design specification
Based on how we're currently doing things, the algorithm should:
[ ] Take a Source, and produce a new Source with revised Spaces
[ ] Rid of indent flag on Grammar, as indentation is due more to depth isBlockFor and whether a newline is preferred
[ ] Create static Spaces.getPreferredSpaces(node: Node): Spaces, which goes through each leaf and formats its preceding space according to field rules, allowing for additional space to be added at the end.
[ ] Revise the reactive block in RootView.svelte to use Spaces.getPreferredSpaces().
[ ] Remove Node.toWordplay()'s call to Node.getPreferredPrecedingSpace(), calling Spaces.getPreferredSpaces(node) if not provided
[ ] Delete Node.getPreferredPrecedingSpace()
[ ] Delete Spaces.getPreferredPrecedingSpace()
[ ] Delete all references to Spaces.getAdditionalSpace(), since we no longer do additional space
The pseudocode for the algorithm is as follows:
for each token in order of appearance
let depth be Root.getDepth()
let current be the current space in the text
let space be ''
if a newline is desired
if not yet there, add '\n', or '\n\n' if so desired to space
append one '\t' to space for each depth
else if space is desired
append one space to space
subtract space from current, then append remainder to space
save space for token
return mappnig from tokens to spaces
Expected behavior
We have a "pretty printing" feature (ew, I don't like that phrase) that should reformat programs with preferred spacing for readability. For example, if we have this program:
data: [1 2 [3 4 5]6 12345678910111213141522343243243243]
The formatting feature should produce this
Actual behavior
It actually produces this:
There are many reasons for this, one of which is because the implementation for this is distributed across
Spaces.ts
andNode.ts
, which leads to many inconsistencies. It's also inefficient, and confusing to change, and the various formatting preferences defined in each node'sGrammar
are also not always respected consistently.Design specification
Based on how we're currently doing things, the algorithm should:
indent
flag onGrammar
, as indentation is due more to depthisBlockFor
and whether a newline is preferredSpaces.getPreferredSpaces(node: Node): Spaces
, which goes through each leaf and formats its preceding space according to field rules, allowing for additional space to be added at the end.Spaces.getPreferredSpaces()
.Node.toWordplay()
's call toNode.getPreferredPrecedingSpace()
, callingSpaces.getPreferredSpaces(node)
if not providedNode.getPreferredPrecedingSpace()
Spaces.getPreferredPrecedingSpace()
Spaces.getAdditionalSpace()
, since we no longer do additional spaceThe pseudocode for the algorithm is as follows: