haskell-tools-ast-trf 0.1.3.0 → 0.2.0.0
raw patch · 8 files changed
+33/−12 lines, 8 filesdep −MissingHdep ~haskell-tools-astsetup-changed
Dependencies removed: MissingH
Dependency ranges changed: haskell-tools-ast
Files
- Language/Haskell/Tools/AnnTrf/PlaceComments.hs +1/−2
- Language/Haskell/Tools/AnnTrf/RangeTemplate.hs +10/−2
- Language/Haskell/Tools/AnnTrf/RangeTemplateToSourceTemplate.hs +2/−0
- Language/Haskell/Tools/AnnTrf/RangeToRangeTemplate.hs +10/−3
- Language/Haskell/Tools/AnnTrf/SourceTemplate.hs +5/−0
- Language/Haskell/Tools/AnnTrf/SourceTemplateHelpers.hs +1/−0
- Setup.hs +1/−1
- haskell-tools-ast-trf.cabal +3/−4
Language/Haskell/Tools/AnnTrf/PlaceComments.hs view
@@ -10,7 +10,6 @@ import qualified Data.Set as Set import Data.Maybe import Data.List -import Data.String.Utils import Data.Char (isSpace, isAlphaNum) import Data.Data import Data.Generics.Uniplate.Data @@ -34,7 +33,7 @@ where getPragmaCommand = takeWhile (\c -> isAlphaNum c || c == '_') . dropWhile isSpace . drop 3 isPragma :: AnnotationComment -> Bool -isPragma (AnnBlockComment str) = startswith "{-#" str && endswith "#-}" str +isPragma (AnnBlockComment str) = take 3 str == "{-#" && take 3 (reverse str) == "#-}" isPragma _ = False -- | Puts comments in the nodes they should be attached to. Leaves the AST in a state where parent nodes
Language/Haskell/Tools/AnnTrf/RangeTemplate.hs view
@@ -76,11 +76,19 @@ getRangeElemSpan _ = Nothing instance HasRange (SpanInfo RngTemplateStage) where - getRange = RealSrcSpan . (^. rngTemplateNodeRange) + getRange = RealSrcSpan . (^. rngTemplateNodeRange) + setRange (RealSrcSpan sp) = rngTemplateNodeRange .= sp + setRange _ = id + instance HasRange (ListInfo RngTemplateStage) where - getRange = RealSrcSpan . (^. rngTemplateListRange) + getRange = RealSrcSpan . (^. rngTemplateListRange) + setRange (RealSrcSpan sp) = rngTemplateListRange .= sp + setRange _ = id + instance HasRange (OptionalInfo RngTemplateStage) where getRange = RealSrcSpan . (^. rngTemplateOptRange) + setRange (RealSrcSpan sp) = rngTemplateOptRange .= sp + setRange _ = id instance Show (SpanInfo RngTemplateStage) where show rngNode = concatMap show $ rngNode ^. rngTemplateNodeElems
Language/Haskell/Tools/AnnTrf/RangeTemplateToSourceTemplate.hs view
@@ -1,6 +1,8 @@ {-# LANGUAGE LambdaCase , FlexibleContexts #-} +-- | This module converts range templates into source templates. +-- Basically it reads the source file and attaches parts of the source file to the AST elements that have the range of the given source code fragment. module Language.Haskell.Tools.AnnTrf.RangeTemplateToSourceTemplate where import SrcLoc
Language/Haskell/Tools/AnnTrf/RangeToRangeTemplate.hs view
@@ -16,6 +16,8 @@ import SrcLoc import Language.Haskell.Tools.AnnTrf.RangeTemplate +import FastString as GHC + import Debug.Trace -- | Creates a source template from the ranges and the input file. @@ -53,8 +55,13 @@ => ([SrcSpan] -> x RangeStage -> x NormRangeStage) -> x RangeStage -> State [[SrcSpan]] (x NormRangeStage) trf f ni = do (below : top : xs) <- get let res = f below ni - put ([] : (top ++ [ getRange res ]) : xs) - return res + resRange = getRange res + endOfSiblings = srcSpanEnd (RealSrcSpan $ collectSpanRanges (srcSpanStart resRange) top) + correctedRange = if endOfSiblings > srcSpanStart resRange + then mkSrcSpan endOfSiblings (max endOfSiblings (srcSpanEnd resRange)) + else resRange + put ([] : (top ++ [ correctedRange ]) : xs) + return $ setRange correctedRange res -- | Expand a simple node to contain its children expandToContain :: [SrcSpan] -> SpanInfo RangeStage -> SpanInfo NormRangeStage @@ -76,7 +83,7 @@ -- only continue if the correct place for the child range is not found Just pieces -> pieces ++ rest Nothing -> elem : breakFirstHit rest sp - breakFirstHit [] sp = error ("breakFirstHit: didn't find correct place for " ++ show sp ++ " in " ++ show sp ++ " with " ++ show sps) + breakFirstHit [] sp = error ("breakFirstHit: " ++ maybe "" unpackFS (srcSpanFileName_maybe sp) ++ " didn't find correct place for " ++ shortShowSpan sp ++ " in " ++ shortShowSpan sp ++ " with [" ++ concat (intersperse "," (map shortShowSpan sps)) ++ "]") cutOutElemList :: [SrcSpan] -> ListInfo NormRangeStage -> ListInfo RngTemplateStage cutOutElemList sps lp@(NormListInfo bef aft sep indented sp)
Language/Haskell/Tools/AnnTrf/SourceTemplate.hs view
@@ -77,10 +77,15 @@ instance HasRange (SpanInfo SrcTemplateStage) where getRange = (^. sourceTemplateNodeRange) + setRange = (sourceTemplateNodeRange .=) + instance HasRange (ListInfo SrcTemplateStage) where getRange = (^. sourceTemplateListRange) + setRange = (sourceTemplateListRange .=) + instance HasRange (OptionalInfo SrcTemplateStage) where getRange = (^. sourceTemplateOptRange) + setRange = (sourceTemplateOptRange .=) instance Show (SpanInfo SrcTemplateStage) where show (SourceTemplateNode rng sp) = concatMap show sp
Language/Haskell/Tools/AnnTrf/SourceTemplateHelpers.hs view
@@ -14,6 +14,7 @@ import Language.Haskell.Tools.AnnTrf.SourceTemplate filterList :: (Ann e dom SrcTemplateStage -> Bool) -> AnnList e dom SrcTemplateStage -> AnnList e dom SrcTemplateStage +-- QUESTION: is it OK? No problem from losing separators? filterList pred ls = replaceList (filter pred (ls ^. annListElems)) ls -- | Replaces the list with a new one with the given elements, keeping the most common separator as the new one.
Setup.hs view
@@ -1,2 +1,2 @@-import Distribution.Simple+import Distribution.Simple main = defaultMain
haskell-tools-ast-trf.cabal view
@@ -1,7 +1,7 @@ name: haskell-tools-ast-trf -version: 0.1.3.0 +version: 0.2.0.0 synopsis: Conversions on Haskell-Tools AST to prepare for refactorings -description: Converts the Haskell-Tools AST between different versions to have source annotations that help refactorings. Have transformations that convert from ranges to range templates and then to source templates. Also have a transformation to put comments to their places. +description: Converts the Haskell-Tools AST between different versions to have source annotations that help refactorings. Have transformations that convert from ranges to range templates and then to source templates. The AST can be modified correctly when it has source templates. Every syntactically correct program can be transformed into source template form. Also have a transformation to put comments to their places. homepage: https://github.com/nboldi/haskell-tools license: BSD3 license-file: LICENSE @@ -20,10 +20,9 @@ , Language.Haskell.Tools.AnnTrf.PlaceComments build-depends: base >=4.9 && <5.0 , ghc >=8.0 && <8.1 - , haskell-tools-ast >=0.1.3 && <1.0 + , haskell-tools-ast >=0.2 && <0.3 , uniplate >=1.6 && <2.0 , references >=0.3.2 && <1.0 , mtl >=2.2 && <2.3 , containers >=0.5 && <0.6 - , MissingH >=1.3 && <2.0 default-language: Haskell2010