技术细节
该打印机是 recast 打印机的一个分支,其算法被 Wadler 在“Prettier 打印机”中描述的算法所取代。仍然可能有需要清理的重铸旧版代码。
¥This printer is a fork of recast’s printer with its algorithm replaced by the one described by Wadler in "A prettier printer". There still may be leftover code from recast that needs to be cleaned up.
基本思想是打印机采用 AST 并返回输出的中间表示,打印机使用它来生成字符串。优点是打印机可以 "measure" IR 并查看输出是否适合一行,如果不适合则中断。
¥The basic idea is that the printer takes an AST and returns an intermediate representation of the output, and the printer uses that to generate a string. The advantage is that the printer can "measure" the IR and see if the output is going to fit on a line, and break if not.
这意味着打印 AST 的大部分逻辑都涉及生成涉及特定命令的输出的抽象表示。例如,["(", line, arg, line, ")"]
表示左括号、参数和右括号的串联。但如果一行不适合,打印机可能会在指定 line
的地方中断。
¥This means that most of the logic of printing an AST involves generating an abstract representation of the output involving certain commands. For example, ["(", line, arg, line, ")"]
would represent a concatenation of opening parens, an argument, and closing parens. But if that doesn’t fit on one line, the printer can break where line
is specified.
在线运行 有一个特殊的模式来探索 Prettier 的中间表示是如何打印的。为此,打开侧边栏("显示选项" 按钮)并将 parser
选项设置为特殊值 doc-explorer
。
¥The Playground has a special mode for exploring how Prettier’s intermediate representation is printed. To get there, open the sidebar (the "Show options" button) and set the parser
option to the special value doc-explorer
.
更多(粗略的)细节可以在 commands.md 中找到。
¥More (rough) details can be found in commands.md.