From 8f0da09d7a139903d4dcb0d4ba77460030de5431 Mon Sep 17 00:00:00 2001 From: sorki Date: Sun, 31 Dec 2023 18:28:53 +0100 Subject: [PATCH] Port to hnix-0.17 --- cabal.project | 2 +- src/Nix/Comments.hs | 4 ++-- src/Update/Nix/FetchGit/Utils.hs | 6 +++--- src/Update/Span.hs | 20 ++++++++++++-------- update-nix-fetchgit.cabal | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cabal.project b/cabal.project index 9683d41..624427a 100644 --- a/cabal.project +++ b/cabal.project @@ -1,3 +1,3 @@ packages: ./*.cabal optimization: 2 -allow-newer: all:base, all:ghc-prim, hnix:template-haskell +allow-newer: all:base, all:ghc-prim, hnix:template-haskell, lens-family-th:template-haskell diff --git a/src/Nix/Comments.hs b/src/Nix/Comments.hs index b31983f..808ddfc 100644 --- a/src/Nix/Comments.hs +++ b/src/Nix/Comments.hs @@ -46,8 +46,8 @@ annotateWithComments sourceLines = go go' :: NExprLocF f -> NExprCommentsF f go' e = - let comment = case spanEnd . annotation . getCompose $ e of - SourcePos _ line col -> do + let comment = case getSpanEnd . annotation . getCompose $ e of + NSourcePos _ (NPos line) (NPos col) -> do theLine <- sourceLines !? (unPos line - 1) theLineAfterExpression <- dropMaybe (unPos col - 1) theLine let theLineAfterCruft = diff --git a/src/Update/Nix/FetchGit/Utils.hs b/src/Update/Nix/FetchGit/Utils.hs index c05c946..562ee21 100644 --- a/src/Update/Nix/FetchGit/Utils.hs +++ b/src/Update/Nix/FetchGit/Utils.hs @@ -145,11 +145,11 @@ formatWarning (DuplicateAttrs attrName) = "Error: The \"" <> attrName <> "\" attribute appears twice in a set." formatWarning (NotAString expr) = "Error: The expression at " - <> (T.pack . prettyPrintSourcePos . spanBegin . exprSpan) expr + <> (T.pack . prettyPrintSourcePos . getSpanBegin . exprSpan) expr <> " is not a string literal." formatWarning (NotABool expr) = "Error: The expression at " - <> (T.pack . prettyPrintSourcePos . spanBegin . exprSpan) expr + <> (T.pack . prettyPrintSourcePos . getSpanBegin . exprSpan) expr <> " is not a boolean literal." formatWarning (NixPrefetchGitFailed exitCode errorOutput) = "Error: nix-prefetch-git failed with exit code " @@ -185,7 +185,7 @@ tShow = T.pack . show containsPosition :: NExprLoc -> (Int, Int) -> Bool containsPosition (Fix (Compose (AnnUnit (SrcSpan begin end) _))) p = - let unSourcePos (SourcePos _ l c) = (unPos l, unPos c) + let unSourcePos (NSourcePos _ (NPos l) (NPos c)) = (unPos l, unPos c) in p >= unSourcePos begin && p < unSourcePos end ---------------------------------------------------------------- diff --git a/src/Update/Span.hs b/src/Update/Span.hs index 9eacd67..53a0c33 100644 --- a/src/Update/Span.hs +++ b/src/Update/Span.hs @@ -6,7 +6,7 @@ module Update.Span ( SpanUpdate(..) , SrcSpan(..) - , SourcePos(..) + , NSourcePos(..) , updateSpan , updateSpans , linearizeSourcePos @@ -25,7 +25,11 @@ import Data.Text ( Text , lines , splitAt ) -import Nix.Expr.Types.Annotated +import Nix.Expr.Types ( NPos(..) + , NSourcePos(..) + , unPos + ) +import Nix.Expr.Types.Annotated ( SrcSpan(..) ) import Prelude hiding ( length , lines , splitAt @@ -42,7 +46,7 @@ data SpanUpdate = SpanUpdate -- | Update many spans in a file. They must be non-overlapping. updateSpans :: [SpanUpdate] -> Text -> Text updateSpans us t = - let sortedSpans = sortOn (spanBegin . spanUpdateSpan) us + let sortedSpans = sortOn (getSpanBegin . spanUpdateSpan) us anyOverlap = any (uncurry overlaps) (zip <*> tail $ spanUpdateSpan <$> sortedSpans) in assert (not anyOverlap) (foldr updateSpan t sortedSpans) @@ -60,9 +64,9 @@ overlaps :: SrcSpan -> SrcSpan -> Bool overlaps (SrcSpan b1 e1) (SrcSpan b2 e2) = b2 >= b1 && b2 < e1 || e2 >= b1 && e2 < e1 --- | Split some text at a particular 'SourcePos' -split :: SourcePos -> Text -> (Text, Text) -split (SourcePos _ row col) t = splitAt +-- | Split some text at a particular 'NSourcePos' +split :: NSourcePos -> Text -> (Text, Text) +split (NSourcePos _ (NPos row) (NPos col)) t = splitAt (fromIntegral (linearizeSourcePos t (fromIntegral (unPos row - 1)) @@ -84,6 +88,6 @@ linearizeSourcePos t l c = fromIntegral lineCharOffset + c where lineCharOffset = sum . fmap ((+ 1) . length) . genericTake l . lines $ t -prettyPrintSourcePos :: SourcePos -> String -prettyPrintSourcePos (SourcePos _ row column) = +prettyPrintSourcePos :: NSourcePos -> String +prettyPrintSourcePos (NSourcePos _ (NPos row) (NPos column)) = show (unPos row) <> ":" <> show (unPos column) diff --git a/update-nix-fetchgit.cabal b/update-nix-fetchgit.cabal index 8552cb7..ce0df1a 100644 --- a/update-nix-fetchgit.cabal +++ b/update-nix-fetchgit.cabal @@ -108,7 +108,7 @@ library , bytestring >=0.10 , data-fix , github-rest >=1.1 - , hnix ==0.16.* + , hnix >=0.17 && <0.18 , monad-validate , mtl , process >=1.2