wordplaydev / wordplay

An accessible, language-inclusive programming language and IDE for creating interactive typography on the web.
Other
55 stars 16 forks source link

Space formatting doesn't properly insight newlines #463

Closed amyjko closed 1 month ago

amyjko commented 1 month ago

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

data: [
    1 
    2 
    [3 4 5]
    6 
    12345678910111213141522343243243243
]

Actual behavior

It actually produces this:

data: [
1 
2 
[3 4 5]
6 
12345678910111213141522343243243243
]

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:

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