@@ -35,8 +35,9 @@ import Language.Rust.Syntax.Ident
35
35
import Language.Rust.Pretty.Util
36
36
import Data.Text.Prettyprint.Doc hiding ((<+>) , hsep , indent , vsep )
37
37
38
- import Data.Maybe (maybeToList )
38
+ import Data.Maybe (maybeToList , fromMaybe )
39
39
import Data.Foldable (toList )
40
+ import Data.List (unfoldr )
40
41
import Data.List.NonEmpty (NonEmpty (.. ))
41
42
import qualified Data.List.NonEmpty as N
42
43
@@ -88,24 +89,53 @@ printMac (Mac path ts x) d = annotate x (printPath path False <> "!" <> body)
88
89
where body = block d True mempty mempty [ printTokenStream ts ]
89
90
90
91
-- | Given two positions, find out how to print appropriate amounts of space between them
91
- printSpaceBetween :: Span -> Span -> Doc a
92
- printSpaceBetween (Span _ (Position _ y1 x1)) (Span (Position _ y2 x2) _)
93
- | y2 == y1 = hcat (replicate (x2 - x1) space)
94
- | y2 > y1 = hcat (replicate (y2 - y1) line) <> column (\ x1' -> hcat (replicate (x2 - x1') space))
95
- | otherwise = space
96
- printSpaceBetween _ _ = space
92
+ printSpaceBetween :: Bool -> Span -> Span -> Maybe (Doc a )
93
+ printSpaceBetween spaceNeeded (Span _ (Position _ y1 x1)) (Span (Position _ y2 x2) _)
94
+ | y2 == y1 && x2 > x1 = Just $ hcat (replicate (x2 - x1) space)
95
+ | y2 > y1 = Just $ hcat (replicate (y2 - y1) line) <> column (\ x1' -> hcat (replicate (x2 - x1') space))
96
+ | spaceNeeded = Just space
97
+ | otherwise = Just mempty
98
+ printSpaceBetween _ _ _ = Nothing
97
99
98
100
-- | Print a token tree (@print_tt@)
99
101
printTt :: TokenTree -> Doc a
100
102
printTt (Token _ t) = printToken t
101
103
printTt (Delimited _ d ts) = block d True mempty mempty [ printTokenStream ts ]
102
104
103
- -- | Print a list of token streams, with the right amount of space between successive elements
104
- printTokenStreams :: [TokenStream ] -> Doc a
105
- printTokenStreams [] = mempty
106
- printTokenStreams [ts] = printTokenStream ts
107
- printTokenStreams (ts1: ts2: tss) = printTokenStream ts1 <> sp <> printTokenStreams (ts2: tss)
108
- where sp = printSpaceBetween (spanOf ts1) (spanOf ts2)
105
+ -- | Print a list of token trees, with the right amount of space between successive elements
106
+ printTokenTrees :: [TokenTree ] -> Doc a
107
+ printTokenTrees [] = mempty
108
+ printTokenTrees [tt] = printTt tt
109
+ printTokenTrees (tt1: tt2: tts) = printTt tt1 <> sp <> printTokenTrees (tt2: tts)
110
+ where
111
+ extremities :: TokenTree -> (Token , Token )
112
+ extremities (Token _ t ) = (t,t)
113
+ extremities (Delimited _ d _) = (OpenDelim d, CloseDelim d)
114
+
115
+ -- extremities in this particular situation
116
+ (lastTt1, firstTt2) = (snd $ extremities tt1, fst $ extremities tt2)
117
+ spNeeded = lastTt1 `spaceNeeded` firstTt2
118
+
119
+ -- Spacing of tokens, as informed by positional information on tokens
120
+ spPos = printSpaceBetween spNeeded (spanOf tt1) (spanOf tt2)
121
+
122
+ -- Spacing of tokens, as informed by 'spaceNeeded' and special cases
123
+ spTok = case (snd $ extremities tt1, fst $ extremities tt2) of
124
+ (Comma , _) -> space
125
+ (Colon , _) -> space
126
+ (Semicolon , _) -> space
127
+ (_, OpenDelim Brace ) -> space
128
+ (CloseDelim Brace , _) -> space
129
+ (t1, t2) | t1 `elem` toksRequiringSp || t2 `elem` toksRequiringSp -> space
130
+ | otherwise -> if spNeeded then space else mempty
131
+
132
+ -- List of tokens that want to have space on either side of them
133
+ toksRequiringSp = [ Equal , GreaterEqual , GreaterGreaterEqual , EqualEqual , NotEqual , LessEqual ,
134
+ LessLessEqual , MinusEqual , AmpersandEqual , PipeEqual , PlusEqual , StarEqual ,
135
+ SlashEqual , CaretEqual , PercentEqual , RArrow , LArrow , FatArrow ]
136
+
137
+ -- Use 'spPos' with 'spTok' as a fallback
138
+ sp = fromMaybe spTok spPos
109
139
110
140
-- | Print the space between a token stream and the mod-path before it
111
141
printTokenStreamSp :: TokenStream -> Doc a
@@ -116,8 +146,7 @@ printTokenStreamSp (Stream (t:_)) = printTokenStreamSp t
116
146
117
147
-- | Print a token stream
118
148
printTokenStream :: TokenStream -> Doc a
119
- printTokenStream (Tree tt) = printTt tt
120
- printTokenStream (Stream ts) = printTokenStreams ts
149
+ printTokenStream = printTokenTrees . unfoldr unconsTokenStream
121
150
122
151
-- | Print a token (@token_to_string@)
123
152
-- Single character expression-operator symbols.
0 commit comments