77{-# LANGUAGE TypeFamilies #-}
88{-# LANGUAGE TypeOperators #-}
99{-# LANGUAGE ViewPatterns #-}
10+ {-# LANGUAGE MultiWayIf #-}
1011
1112module Nix.Render where
1213
1314import Prelude hiding ( readFile )
1415
15- -- Please reduce Unsafe
16- import Relude.Unsafe ( read )
1716import qualified Data.ByteString as BS
1817import qualified Data.Set as Set
19- import Data.List ( maximum )
2018import Nix.Utils.Fix1 ( Fix1T
2119 , MonadFix1T )
2220import Nix.Expr.Types.Annotated
@@ -102,8 +100,8 @@ sourceContext
102100 :: MonadFile m => FilePath -> Pos -> Pos -> Pos -> Pos -> Doc a -> m (Doc a )
103101sourceContext path (unPos -> begLine) (unPos -> _begCol) (unPos -> endLine) (unPos -> _endCol) msg
104102 = do
105- let beg' = max 1 $ min begLine $ begLine - 3
106- end' = max endLine $ endLine + 3
103+ let beg' = max 1 $ begLine - 3
104+ end' = endLine + 3
107105 ls <-
108106 fmap pretty
109107 . take (end' - beg')
@@ -112,14 +110,28 @@ sourceContext path (unPos -> begLine) (unPos -> _begCol) (unPos -> endLine) (unP
112110 . decodeUtf8
113111 <$> readFile path
114112 let
115- nums = zipWith (curry (show . fst )) [beg' .. ] ls
116- longest = maximum $ length <$> nums
117- nums' = (\ n -> replicate (longest - length n) ' ' <> n) <$> nums
118- pad n | read n == begLine = " ==> " <> n
119- | otherwise = " " <> n
120- ls' =
121- zipWith
122- (\ a b -> a <> space <> b)
123- (pretty . pad <$> nums')
124- ((" | " <> ) <$> ls)
125- pure $ vsep $ ls' <> [msg]
113+ longest = length $ show @ String (beg' + (length ls) - 1 )
114+ pad n =
115+ let
116+ ns = show n
117+ nsp = replicate (longest - length ns) ' ' <> ns
118+ in
119+ if
120+ | n == begLine && n == endLine -> " ==> " <> nsp <> " | "
121+ | n >= begLine && n <= endLine -> " > " <> nsp <> " | "
122+ | otherwise -> " " <> nsp <> " | "
123+ composeLine n l =
124+ [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 ]
131+ -- XXX: Consider inserting the message here when it is small enough.
132+ -- ATM some messages are so huge that they take prevalence over the source listing.
133+ -- ++ [ indent (length $ pad n) msg | n == endLine ]
134+
135+ ls' = concat $ zipWith composeLine [beg' .. ] ls
136+
137+ pure $ vsep $ ls' ++ [ indent (length $ pad begLine) msg ]
0 commit comments