简体   繁体   English

Haskell 中的漂亮打印:打印嵌套元组时打破外部组

[英]Pretty printing in Haskell: Break outer groups when printing nested tuples

I want to pretty print an AST using Haskell and (currently) wl-pprint-annotated (willing to switch to a different library).我想使用 Haskell 和(当前) wl-pprint-annotated (愿意切换到不同的库)漂亮地打印 AST。

How can I make the renderer prefer breaking the softlines of the outer group over the softlines of the inner group?我怎样才能让渲染器更喜欢打破外部组的软线而不是内部组的软线?

Minimal Example最小的例子

Take for example the tuple ((1234, 5678), (abcd, efgh)) .以元组((1234, 5678), (abcd, efgh))为例。

The output I want:我想要的 output:

// line width: 10
(
  (
    1234,
    5678
  ),
  (
    abcd,
    efgh
  )
)

// line width: 16
(
  (1234, 5678),
  (abcd, efgh)
)

// line width: 32
((1234, 5678), (abcd, efgh))

The output I get: output 我得到:

// line width: 10
((1234,
    5678),
  (abcd,
    efgh))

// line width: 16
((1234, 5678), (
    abcd, efgh))

// line width: 32
((1234, 5678), (abcd, efgh))

Code:代码:

module Main where
import qualified Prelude
import Prelude hiding((<>))

import Text.PrettyPrint.Annotated.WL 

main :: IO ()
main = do
  putStrLn $ pp 10
  putStrLn $ pp 16
  putStrLn $ pp 32

pp w = "// line width: " ++ show w ++ "\n" ++
       display (renderPretty 1.0 w doc) ++ "\n"

doc = pair (pair (text "1234") (text "5678"))
           (pair (text "abcd") (text "efgh"))

pair x y = group (nest 2 (lparen <//> x <> comma </> y) <//> rparen)
pair x y = group (nest 2 (lparen <##> x <> comma <#> y) <##> rparen)

As ekim found out, I've mixed up </> with <#>正如 ekim 发现的那样,我把</><#>混在一起了

I found the documentation to be confusing, so let me clear it up a little.我发现文档令人困惑,所以让我稍微澄清一下。

First of all the operators </> and <#> are just sugar for line and softline .首先,运算符</><#>只是linesoftline的糖。 See definitions :定义

x </> y = x <> softline <> y
x <#> y = x <> line <> y

My problem was that I was using softline when what I wanted was line .我的问题是当我想要的是line时我正在使用softline

Commonalities between line and softline线与软linesoftline

Both are printed as space when the whole line fits the page.当整行适合页面时,两者都打印为空格。 Both are replaced with a line break when the line does not fit the page.当行不适合页面时,两者都替换为换行符。

Difference between line and softline linesoftline线的区别

When a group foes not fit the page, all line s of the whole group are replaced with line breaks.当一组敌人不适合页面时,整个组的所有line都被换行符替换。 That's the behavior I've wanted.这就是我想要的行为。

When the line does not fit the page, only the last softline still fitting the page is replaced.当线条不适合页面时,替换仍然适合页面的最后一条线。 Not the whole group.不是整个组。 It's like the word wrapping in our text editors: Just breaking after the last word that fits to the page.这就像我们的文本编辑器中的自动换行:在适合页面的最后一个单词之后中断。

For example例如

doc = paragraph p1
paragraph = foldr (</>) mempty . map text . words
p1 = "I want to pretty print an AST using Haskell and (currently) wl-pprint-annotated (willing to switch to a different library)."

is printed as打印为

I want to pretty print an AST using Haskell and
(currently) wl-pprint-annotated (willing to
switch to a different library).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM