nimpretty
#113
Replies: 5 comments 11 replies
-
This can be done as a three-part refactor. First we remove nimpretty, it is kind of useless, then make sure parser does not lose information, and finally implement the layout engine. |
Beta Was this translation helpful? Give feedback.
-
I have cleaned up the implementation from hmisc - https://github.com/haxscramper/hack/blob/master/testing/nim/implementation_ideas/blockfmt_generic.nim in conjunction with chunking approach from https://journal.stuffwithstuff.com/2015/09/08/the-hardest-program-ive-ever-written/ it can be used to implement pretty-printer once we have a concrete syntax tree. Example of usage proc lytProc(
args: openarray[LytBlock[Rune]], body: LytBlock[Rune]): LytBlock[Rune] =
let
h = T["proc ("]
t = T[") = "]
hsep = makeHSeparated[Rune](@args, T[", "])
vsep = makeVSeparated[Rune](@args, T[", "])
result = C[
H[h, hsep, t, body],
V[H[h, hsep, t, I[2, body]]],
V[h, I[4, vsep], t, I[2, body]]
]
for args in [1, 2]:
for body in [20, 60]:
echo toString lytProc(
args = mapIt(0 ..< args, T[&"arg{it}: arg{it}_type"]),
body = T[repeat("?", body)]
)
Printed optimized layouts
|
Beta Was this translation helpful? Give feedback.
-
With proper code formatter implemented, we can also remove |
Beta Was this translation helpful? Give feedback.
-
Mass-formatting compiler source certainly will break most of the git history. For local git blame, this can be mitigated with https://www.moxio.com/blog/43/ignoring-bulk-change-commits-with-git-blame - Support for this is requested in GitHub as well community/community#5033 and https://github.community/t/support-ignore-revs-file-in-githubs-blame-view/3256/27 |
Beta Was this translation helpful? Give feedback.
-
One problem with nimpretty rendering is support for macro code - while it is next to impossible to provide good rendering for all known macros (could theoretically be solved by allowing user-provided pattern expressions with custom formatting rules, but I highly doubt this is worth it) the printer must support the unusual AST combinations to some extent |
Beta Was this translation helpful? Give feedback.
-
nimpretty
itself is a fairly simple tool. Implementation leaves a lot to be desired - currently it is hacked intoparser.nim
andlexer.nim
directly, with multiple (total of ~37 and ~23 respectively) conditional compilation checks likeThis makes it really hard to provide a global code layout, leading to bad formatting and sometimes even broken code. Instead, the parser should be refactored to avoid information loss - the best idea so far was to simply keep the underlying tokens and then put them back in the generated code on formatting.
Engine for code layout can use an algorithm from "A New Approach to Optimal Code Formatting" (has a reference python implementation which I rewrote in nim), but tooling like dartfmt (accompanied by the article) or scalafmt (research paper) can be investigated as well.
Beta Was this translation helpful? Give feedback.
All reactions