-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathLeijen.hs
More file actions
172 lines (131 loc) · 5.73 KB
/
Leijen.hs
File metadata and controls
172 lines (131 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{-# LANGUAGE CPP #-}
#include "version-compatibility-macros.h"
module Text.PrettyPrint.Annotated.Leijen {-# DEPRECATED "Compatibility module for users of annotated-wl-pprint - use \"Prettyprinter\" instead" #-} (
Doc, SimpleDoc, SpanList, putDoc, hPutDoc, empty, char, text, (<>), nest,
line, linebreak, group, softline, softbreak, align, hang, indent,
encloseSep, list, tupled, semiBraces, (<+>), (<$>), (</>), (<$$>), (<//>),
hsep, vsep, fillSep, sep, hcat, vcat, fillCat, cat, punctuate, fill,
fillBreak, enclose, squotes, dquotes, parens, angles, braces, brackets,
lparen, rparen, langle, rangle, lbrace, rbrace, lbracket, rbracket, squote,
dquote, semi, colon, comma, space, dot, backslash, equals, pipe, string,
int, integer, float, double, rational, bool, annotate, noAnnotate,
renderPretty, renderCompact, displayDecorated, displayDecoratedA, display,
displayS, displayIO, displaySpans, column, nesting, width
) where
import Prelude hiding ((<$>))
#if !(LIFTA2_IN_PRELUDE)
import Control.Applicative (liftA2)
#endif
import qualified Data.Text as T
import qualified Data.Text.IO as T
import System.IO
import Prettyprinter
import qualified Prettyprinter.Render.String as New
import qualified Prettyprinter.Render.Text as New
import Prettyprinter.Render.Util.Panic
type SimpleDoc = SimpleDocStream
putDoc :: Doc () -> IO ()
putDoc = New.putDoc
hPutDoc :: Handle -> Doc () -> IO ()
hPutDoc = New.hPutDoc
displayS :: SimpleDoc ann -> ShowS
displayS = New.renderShowS
renderPretty :: Float -> Int -> Doc ann -> SimpleDoc ann
renderPretty ribbonFraction pWidth
= layoutPretty LayoutOptions
{ layoutPageWidth = AvailablePerLine pWidth (realToFrac ribbonFraction) }
renderCompact :: Doc ann -> SimpleDoc ann
renderCompact = layoutCompact
display :: SimpleDoc ann -> String
display = flip displayS ""
noAnnotate :: Doc ann -> Doc xxx
noAnnotate = unAnnotate
linebreak :: Doc ann
linebreak = line'
softbreak :: Doc ann
softbreak = softline'
semiBraces :: [Doc ann] -> Doc ann
semiBraces = encloseSep lbrace rbrace semi
(<$>), (</>), (<$$>), (<//>) :: Doc ann -> Doc ann -> Doc ann
(<$>) = \x y -> x <> line <> y
(</>) = \x y -> x <> softline <> y
(<$$>) = \x y -> x <> line' <> y
(<//>) = \x y -> x <> softline' <> y
empty :: Doc ann
empty = emptyDoc
char :: Char -> Doc ann
char = pretty
bool :: Bool -> Doc ann
bool = pretty
text, string :: String -> Doc ann
text = pretty
string = pretty
int :: Int -> Doc ann
int = pretty
integer :: Integer -> Doc ann
integer = pretty
float :: Float -> Doc ann
float = pretty
double :: Double -> Doc ann
double = pretty
rational :: Rational -> Doc ann
rational = pretty . show
displayDecorated :: (a -> String -> String) -> SimpleDoc a -> String
displayDecorated decor sd = go id id [] sd ""
where
go s d [] SEmpty = d . s
go s d stk (SChar c x) = go (s . showChar c) d stk x
go s d stk (SText _ str x) = go (s . showString (T.unpack str)) d stk x
go s d stk (SLine ind x) = go (s . showString ('\n':replicate ind ' ')) d stk x
go s d stk (SAnnPush ann x) = go id (decor ann) ((s, d):stk) x
go s d ((sf', d'):stk) (SAnnPop x) = let formatted = d (s "")
in go (sf' . showString formatted) d' stk x
go _ _ [] (SAnnPop _) = error "stack underflow"
go _ _ _ SEmpty = error "stack not consumed by rendering"
go _ _ _ SFail = panicUncaughtFail
displayDecoratedA :: (Applicative f, Monoid b)
=> (String -> f b) -> (a -> f b) -> (a -> f b)
-> SimpleDoc a -> f b
displayDecoratedA str start end sd = go [] sd
where
go [] SEmpty = pure mempty
go stk (SChar c x) = str [c] <++> go stk x
go stk (SText _ s x) = str (T.unpack s) <++> go stk x
go stk (SLine ind x) = str ('\n' : replicate ind ' ') <++> go stk x
go stk (SAnnPush ann x) = start ann <++> go (ann:stk) x
go (ann:stk) (SAnnPop x) = end ann <++> go stk x
-- malformed documents
go [] (SAnnPop _) = error "stack underflow"
go _ SEmpty = error "stack not consumed by rendering"
go _ SFail = panicUncaughtFail
(<++>) = liftA2 mappend
type SpanList a = [(Int, Int, a)]
displaySpans :: SimpleDoc a -> (String, SpanList a)
displaySpans sd = go 0 [] sd
where
go :: Int -> [(Int, a)] -> SimpleDoc a -> (String, SpanList a)
go _ [] SEmpty = ("", [])
go i stk (SChar c x) = let (str, spans) = go (i+1) stk x
in (c:str, spans)
go i stk (SText l s x) = mapFst (T.unpack s ++) (go (i + l) stk x)
go i stk (SLine ind x) = mapFst (('\n':replicate ind ' ') ++) (go (1+i+ind) stk x)
go i stk (SAnnPush ann x) = go i ((i, ann):stk) x
go i ((start, ann):stk) (SAnnPop x) = mapSnd ((start, i-start, ann) :) (go i stk x)
-- malformed documents
go _ [] (SAnnPop _) = error "stack underflow"
go _ _ SEmpty = error "Stack not consumed by rendering"
go _ _ SFail = panicUncaughtFail
mapFst :: (a -> b) -> (a, c) -> (b, c)
mapFst f (x, y) = (f x, y)
mapSnd :: (a -> b) -> (c, a) -> (c, b)
mapSnd f (x, y) = (x, f y)
displayIO :: Handle -> SimpleDoc a -> IO ()
displayIO h simpleDoc = go simpleDoc
where
go SFail = panicUncaughtFail
go SEmpty = pure ()
go (SChar c x) = hPutChar h c >> go x
go (SText _ s x) = T.hPutStr h s >> go x
go (SLine i x) = hPutStr h ('\n':replicate i ' ') >> go x
go (SAnnPush _ x) = go x
go (SAnnPop x) = go x