experiment: A non-line breaking pretty printer implementation based on Wadler-Lindig#12465
experiment: A non-line breaking pretty printer implementation based on Wadler-Lindig#12465gwydd12 wants to merge 144 commits into
Conversation
- add wadler based `Doc` structure for the pretty printer implementation
- pretty printer just takes a syntax tree and transforms it into a Doc and prints it without any formatting applied
- implement logic to filter the spaces and line breaks from the original source code to preserve it in the Doc structure - make pretty function non-recursive due to possible stackoverflow
- refactor gapDoc function to make it more readable and maintainable
- implement alphabetical import ordering as a formatting rule
- this operator allows for syntactic sugar to avoid writing `doc <> space <> doc` in the styling rules
- based on partialfunction now have ad-hoc rules adding of different formatting rules - reimplement all currently existing rules in the formatter as formatter modules aka formatting rules
I agree. This is fine. |
|
Hi Din, Just getting up to speed, but already a couple of questions:
|
|
Hey Magnus, Of course we can further discuss these in the meeting, but here some thoughts to your questions.
I would like to keep the abstraction or at least have the
Currently not no. I've rebuilt some structures in some local commits which I will show you in the meeting. However, currently
Out of the box with only Wadler itself it would not work. However, if I remember correctly |
Not sure yet, but perhaps:
Roger 👍 |
- rule engine has some major flaws implemented nevertheless it would work to some degree
- implement the most basic traversal of the tree - implement the most basic wadler lindig doc
- add space after comma and colon
- each examples is compileable after formatting
- be aware this formatter still has many hiccups and can break functional code
|
As discussed with Magnus, we simply try to pattern match instead of already planning a big ruling engine (we incrementally try to refactor and improve). Please note that this progress currently has many hiccups and might break functional code. Example of such breaking: import java.lang.Runnable
import java.lang.Thread
def newRunnable(): Runnable \ IO = new Runnable {
// Note: In Flix `run` is a keyword so we must escape it with $.
def run(_this: Runnable): Unit \ IO = {
println("Running...")
}
}
def main(): Unit \ IO =
new Thread(newRunnable()).start()As stated in the comment |
- remove redundancy as much as possible
- simply refactor the code to be more readable and go into the direction of ormolu and futhark
- align with Wadler paper
|
!!!jar |
|
Building JAR... |
|
The JAR is available HERE. |
|
@magnus-madsen You might want to invoke the |
|
!!!jar |
|
Building JAR... |
|
The JAR is available HERE. |
- rename Formatter to FormatterLsp as it is not the formatting implementation - add correctness and aesthetic property tests to test flixfmt (not included in this PR)
- one test for correctness properties - one test for aesthetic properties - improve finding flix files code - improve comments
- only allow valid syntax to be formatted and do nothing when an `ErrorTree` occurs
- add helper functions - rename variables
- remove useless indexing for all the fixes
|
!!!jar |
|
Building JAR... |
|
The JAR is available HERE. |
|
First of all, thanks again. After all you might want to test the actual formatter and see, how it looks and feels. Therefore, I requested Magnus to invoke the I'll provide two short instructions on how you can use this
$ cd <flix-project>
# Formatting the whole project.
$ java -jar <path-to-jar>/out.jar format
# Formatting a specific file (e.g. Main.flix)
$ java -jar <path-to-jar>/out.jar format src/Main.flix
$ cd <flix-project>
# Option 1: copy out.jar
$ cp <path-to-jar>/out.jar .
# Option 2: move out.jar
$ mv <path-to-jar>/out.jar .
# Open Visual Studio code
$ code .It is to note all this can be done graphically as well of course. Aside from that the And for Visual Studio Code you might need this extension, https://marketplace.visualstudio.com/items?itemName=flix.flix . It can happen that there might some conflicts with which compiler it takes. In case you have some issues and are not able to play around with it, feel free to contact me any time and I'll try to help with finding a solution. And of course if you find any issues with formatting, feel free to open an issue or just note them down if possible, hopefully you don't find to many^^ PS: The jar file can be found in the comment before this one. Cheers gwydd :) |
This implementation is not a full implementation of the Wadler-Lindig pretty-printing algorithm. Nevertheless, it is a baseline to further proceed working with a more structured approach than currently done with the
lsp/Formatter.In this approach there are a few missing pieces:
TextEditOn the other hand, we need to discuss the current location of the pretty printer, I've added it into
tools/, which might not be suitable. However, it makes sense to have the base of the pretty printer intools/as it should be treated as an additional tool to the compiler.Please note as well, this will be rewritten many times to play around with the algorithm. Maybe I'll even try different algorithms. In addition, it also has an experimental
ordering of imports.Cheers :)