Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better indentation & layout for pretty-printing #11

Closed
byorgey opened this issue Sep 20, 2021 · 7 comments · Fixed by #1464
Closed

Better indentation & layout for pretty-printing #11

byorgey opened this issue Sep 20, 2021 · 7 comments · Fixed by #1464
Assignees
Labels
C-Low Hanging Fruit Ideal issue for new contributors. L-Pretty-printing Pretty-printing ASTs or values into a string representation. S-Moderate The fix or feature would substantially improve user experience. Z-User Experience This issue seeks to make the game more enjoyable to play.

Comments

@byorgey
Copy link
Member

byorgey commented Sep 20, 2021

I kind of threw the pretty-printer together without much regard for how the layout and indentation works. The prettyprinter library has lots of nice tools for specifying this kind of thing; someone just needs to sit down and figure out how they work and how to apply them to the Swarm language to get good results.

This is very related to #7 , which will depend on this to get nice-looking save files that users can actually edit without tearing their hair out. Another semi-related issue is #644 .

At the moment, we use pretty-printing quite a bit for types, but we only pretty-print terms in certain error messages (e.g. when a command throws an exception).

@byorgey byorgey added C-Low Hanging Fruit Ideal issue for new contributors. S-Moderate The fix or feature would substantially improve user experience. L-Pretty-printing Pretty-printing ASTs or values into a string representation. labels Sep 20, 2021
@xsebek xsebek self-assigned this Oct 3, 2021
@byorgey
Copy link
Member Author

byorgey commented Feb 24, 2022

Some ideas for a nice pretty-printing style:

  • Put consecutive lambdas on one line, then indent the body starting on the line beneath
  • Put commands separated by ; on separate lines (with semicolons at end)

However, can we make the above somehow optional, so very short lambdas or very short command sequences just go on a single line? Some examples of the kind of thing I have in mind:

def id : a -> a = \x. x end

def repeat : int -> {cmd ()} -> cmd () =
  \n. \c.
    if (n == 0)
      { return () }
      { force c; 
        repeat (n-1) c
      }
end

@xsebek
Copy link
Member

xsebek commented Jun 26, 2022

Thanks for the resources @byorgey. If you plan to work on this please unassign me or I might just pick this up after 6 months. 😉

@byorgey
Copy link
Member Author

byorgey commented Jun 26, 2022

OK! I wasn't necessarily planning to work on this. I just have a goal to "freshen" two github issues per day, which means picking the two least recently updated issues and improving them or moving them along in some (even small) way, like curating the tags, improving the title, adding a comment with additional thoughts or resources, etc. 😄

@byorgey byorgey added the Z-User Experience This issue seeks to make the game more enjoyable to play. label Feb 10, 2023
@xsebek
Copy link
Member

xsebek commented Feb 20, 2023

I wonder what the rule should be for binds (;) I think it would be best to store whether the player explicitly used line break. Then we could collect those that were on the same line and lay them out using fillSep:

move; move; move; move;
turn right;
move; move; move; move;
+--------------------+
| move; move; move;  |
| move;              |
| turn right;        |
| move; move; move;  |
| move;              |
+--------------------+

@xsebek
Copy link
Member

xsebek commented Aug 23, 2023

However, can we make the above somehow optional, so very short lambdas or very short command sequences just go on a single line?

@byorgey Yes, we can! group nicely turns newlines to spaces (or empty in case of line') so this is really nice and easy.

I played with it in 503f38b and got from this:

def forever = \c. c; force forever c end; def encircle = \lDir. \rDir. turn lDir; b <- blocked; if b {turn rDir} {wait 1}; fwBlocked <- blocked; if fwBlocked {turn rDir} {move} end; def patrolCW = force forever (force encircle right left) end; force patrolCW

to this:

def forever = \c. c; force forever c end;
def encircle =
\lDir. \rDir. turn lDir;
 b <- blocked;
 if b {turn rDir} {wait 1};
 fwBlocked <- blocked;
 if fwBlocked {turn rDir} {move}
end;
def patrolCW = force forever (force encircle right left) end;
force patrolCW

@byorgey any idea what will break with multiline pretty Term instance? 😅


As an aside, I think those force are wrong. The result does not type check again because e.g. forever does not have a delayed type. 😕 IMO we should either add the delayed braces or ideally not have force there.

@byorgey
Copy link
Member Author

byorgey commented Aug 24, 2023

Re: force, see my comment here.

@mergify mergify bot closed this as completed in #1464 Aug 25, 2023
mergify bot pushed a commit that referenced this issue Aug 25, 2023
- improve layout of terms:
  - break lines on binds, unless it fits on one line
  - lambdas go on same line, but the body _can_ go to next line
  - def and let can have long body on next line (indented for def)
  - parens and braces have body indented if it does not fit on line
- closes #11

Example using `--format` from #1459:

```
> cabal run swarm -O0 -- format scenarios/Challenges/_blender/patrol-clockwise.sw                          
def forever = \c. c; force forever c end;
def encircle = \lDir. \rDir.
  turn lDir;
  b <- blocked;
  if b {turn rDir} {wait 1};
  fwBlocked <- blocked;
  if fwBlocked {turn rDir} {move}
end;
def patrolCW = force forever (force encircle right left) end;
force patrolCW
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Low Hanging Fruit Ideal issue for new contributors. L-Pretty-printing Pretty-printing ASTs or values into a string representation. S-Moderate The fix or feature would substantially improve user experience. Z-User Experience This issue seeks to make the game more enjoyable to play.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants