Begin adding very basic source line printing

This commit is contained in:
John Wiegley 2019-03-03 14:48:49 -08:00
parent 269faf88a8
commit 624ec11234
No known key found for this signature in database
GPG key ID: C144D8F4F19FE630

View file

@ -7,6 +7,7 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ViewPatterns #-}
module Nix.Render where
@ -16,8 +17,11 @@ import Control.Monad.Trans
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Data.Text.Prettyprint.Doc
import Data.Void
import Debug.Trace
import Nix.Expr.Types.Annotated
import qualified System.Directory as S
import qualified System.Posix.Files as S
@ -69,15 +73,19 @@ posAndMsg (SourcePos _ lineNo _) msg =
FancyError (unPos lineNo)
(Set.fromList [ErrorFail (show msg) :: ErrorFancy Void])
renderLocation :: Monad m => SrcSpan -> Doc a -> m (Doc a)
renderLocation (SrcSpan beg@(SourcePos file begLine begCol)
renderLocation :: MonadFile m => SrcSpan -> Doc a -> m (Doc a)
renderLocation (SrcSpan (SourcePos file begLine begCol)
(SourcePos file' endLine endCol)) msg
| file == file' = do
return $ vsep
[ "In file " <> errorContext file begLine begCol endLine endCol <> ":"
, pretty (parseErrorTextPretty @String (posAndMsg beg msg))
]
| file /= "<string>" && file == file' = do
exist <- doesFileExist file
if exist
then do
txt <- sourceContext file begLine begCol endLine endCol msg
return $ vsep
[ "In file " <> errorContext file begLine begCol endLine endCol <> ":"
, txt
]
else return msg
renderLocation (SrcSpan beg end) msg =
fail $ "Don't know how to render range from " ++ show beg ++ " to " ++ show end
++ " for error: " ++ show msg
@ -85,3 +93,19 @@ renderLocation (SrcSpan beg end) msg =
errorContext :: FilePath -> Pos -> Pos -> Pos -> Pos -> Doc a
errorContext path bl bc _el _ec =
pretty path <> ":" <> pretty (unPos bl) <> ":" <> pretty (unPos bc)
sourceContext :: MonadFile m => FilePath -> Pos -> Pos -> Pos -> Pos -> Doc a -> m (Doc a)
sourceContext path (unPos -> begLine) (unPos -> begCol)
(unPos -> endLine) (unPos -> endCol) msg = do
traceM $ "Taking lines from " ++ path
traceM $ "begLine = " ++ show begLine
traceM $ "begCol = " ++ show begCol
traceM $ "endLine = " ++ show endLine
traceM $ "endCol = " ++ show endCol
traceM $ "msg = " ++ show msg
ls <- take (endLine - begLine)
. drop (pred begLine)
. T.lines
. T.decodeUtf8
<$> readFile path
pure $ vsep $ map pretty ls