11module Nix.Comments
2- ( annotateWithComments
3- , Comment
4- , NExprCommentsF
5- , NExprComments
6- ) where
7-
8- import Data.Text ( Text )
9- import Data.Vector ( (!?)
10- , Vector
11- )
12- import Data.Fix
13- import Nix.Expr
14- import qualified Data.Text as T
15- import Data.Char ( isSpace )
2+ ( annotateWithComments ,
3+ Comment ,
4+ NExprCommentsF ,
5+ NExprComments ,
6+ )
7+ where
8+
9+ import Data.Char (isSpace )
10+ import Data.Fix
11+ import Data.Text (Text )
12+ import qualified Data.Text as T
13+ import Data.Vector
14+ ( Vector ,
15+ (!?) ,
16+ )
17+ import Nix.Expr
1618
1719type Comment = Text
1820
@@ -38,22 +40,23 @@ type NExprComments = Fix NExprCommentsF
3840-- [(1 + { a = 2; },Just "baz"),(1,Just "foo"),({ a = 2; },Just "baz"),(2,Just "bar")]
3941annotateWithComments :: Vector Text -> NExprLoc -> NExprComments
4042annotateWithComments sourceLines = go
41- where
42- go :: NExprLoc -> NExprComments
43- go = Fix . go' . fmap go . unFix
44-
45- go' :: NExprLocF f -> NExprCommentsF f
46- go' e =
47- let
48- comment = case spanEnd . annotation . getCompose $ e of
49- SourcePos _ line col -> do
50- theLine <- sourceLines !? (unPos line - 1 )
51- theLineAfterExpression <- dropMaybe (unPos col - 1 ) theLine
52- let theLineAfterCruft = T. dropWhile (\ c -> isSpace c || (c == ' ;' ))
53- theLineAfterExpression
54- (' #' , theComment) <- T. uncons theLineAfterCruft
55- pure (T. strip theComment)
56- in Compose (Ann comment e)
43+ where
44+ go :: NExprLoc -> NExprComments
45+ go = Fix . go' . fmap go . unFix
46+
47+ go' :: NExprLocF f -> NExprCommentsF f
48+ go' e =
49+ let comment = case spanEnd . annotation . getCompose $ e of
50+ SourcePos _ line col -> do
51+ theLine <- sourceLines !? (unPos line - 1 )
52+ theLineAfterExpression <- dropMaybe (unPos col - 1 ) theLine
53+ let theLineAfterCruft =
54+ T. dropWhile
55+ (\ c -> isSpace c || (c == ' ;' ))
56+ theLineAfterExpression
57+ (' #' , theComment) <- T. uncons theLineAfterCruft
58+ pure (T. strip theComment)
59+ in Compose (AnnUnit comment e)
5760
5861----------------------------------------------------------------
5962-- Utils
0 commit comments