Skip to content

Commit 8954c5d

Browse files
committed
Render: sourceContext: use Text internally to form Doc
1 parent 595bb0a commit 8954c5d

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/Nix/Render.hs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import qualified System.Directory as S
2323
import qualified System.Posix.Files as S
2424
import Text.Megaparsec.Error
2525
import Text.Megaparsec.Pos
26+
import qualified Data.Text as Text
2627

2728
class MonadFail m => MonadFile m where
2829
readFile :: FilePath -> m ByteString
@@ -110,28 +111,32 @@ sourceContext path (unPos -> begLine) (unPos -> _begCol) (unPos -> endLine) (unP
110111
. decodeUtf8
111112
<$> readFile path
112113
let
113-
longest = length $ show @String (beg' + (length ls) - 1)
114+
longest = Text.length $ show $ beg' + length ls - 1
115+
pad :: Int -> Text
114116
pad n =
115117
let
118+
ns :: Text
116119
ns = show n
117-
nsp = replicate (longest - length ns) ' ' <> ns
120+
nsp = Text.replicate (longest - Text.length ns) " " <> ns
118121
in
119122
if
120123
| n == begLine && n == endLine -> "==> " <> nsp <> " | "
121124
| n >= begLine && n <= endLine -> " > " <> nsp <> " | "
122125
| otherwise -> " " <> nsp <> " | "
123126
composeLine n l =
124127
[pretty (pad n) <> l]
125-
++ [ pretty
126-
$ replicate (length (pad n) - 3) ' '
127-
<> "| "
128-
<> replicate (_begCol - 1) ' '
129-
<> replicate (_endCol - _begCol) '^'
130-
| begLine == endLine && n == endLine ]
128+
<> bool mempty
129+
[ pretty $
130+
Text.replicate (Text.length (pad n) - 3) " "
131+
<> "|"
132+
<> Text.replicate (_begCol + 1) " "
133+
<> Text.replicate (_endCol - _begCol) "^"
134+
]
135+
(begLine == endLine && n == endLine)
131136
-- XXX: Consider inserting the message here when it is small enough.
132137
-- ATM some messages are so huge that they take prevalence over the source listing.
133138
-- ++ [ indent (length $ pad n) msg | n == endLine ]
134139

135140
ls' = concat $ zipWith composeLine [beg' ..] ls
136141

137-
pure $ vsep $ ls' ++ [ indent (length $ pad begLine) msg ]
142+
pure $ vsep $ ls' <> [ indent (Text.length $ pad begLine) msg ]

0 commit comments

Comments
 (0)