Skip to content

Commit

Permalink
Replace syb with uniplate for supported operations
Browse files Browse the repository at this point in the history
  • Loading branch information
zliu41 committed Nov 22, 2020
1 parent 4fbd3a3 commit e11ebc2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 3 additions & 0 deletions apply-refact.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ library
, filemanip >= 0.3.6.3 && < 0.4
, unix-compat >= 0.5.2
, directory >= 1.3
, uniplate >= 1.6.13
hs-source-dirs: src
default-language: Haskell2010

Expand Down Expand Up @@ -77,6 +78,7 @@ executable refactor
, unix-compat
, filepath
, transformers
, uniplate

Test-Suite test
type: exitcode-stdio-1.0
Expand Down Expand Up @@ -111,3 +113,4 @@ Test-Suite test
, filepath
, silently
, transformers
, uniplate
13 changes: 7 additions & 6 deletions src/Refact/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import Control.Monad.Trans.State
import Data.Char (isAlphaNum)
import Data.Data
import Data.Functor.Identity (Identity(..))
import Data.Generics hiding (GT)
import Data.Generics (everywhereM, extM, listify, mkM, mkQ, something)
import Data.Generics.Uniplate.Data (transformBi, transformBiM)
import qualified Data.Map as Map
import Data.Maybe
import Data.List
Expand Down Expand Up @@ -281,7 +282,7 @@ runRefactoring as m InsertComment{..} = do
runRefactoring as m RemoveAsKeyword{..} =
pure (as, removeAsKeyword m)
where
removeAsKeyword = everywhere (mkT go)
removeAsKeyword = transformBi go
go :: LImportDecl GHC.GhcPs -> LImportDecl GHC.GhcPs
go imp@(GHC.L l i) | l == pos = GHC.L l (i { ideclAs = Nothing })
| otherwise = imp
Expand Down Expand Up @@ -443,7 +444,7 @@ doGenReplacement m p new old
o = decomposeSrcSpan old
intAnns <- liftIO $ execStateT (modifyAnnKey m o n) anns
let newFile = srcSpanFile newLocReal
newLocal = everywhere (mkT $ setSrcSpanFile newFile) oldLocal
newLocal = transformBi (setSrcSpanFile newFile) oldLocal
newLocalLoc = getLoc newLocal
ensureLoc = combineSrcSpans newLocalLoc
newMG = fun_matches newBind
Expand Down Expand Up @@ -595,7 +596,7 @@ replaceWorker as m parser seed Replace{..} = do
else ann

replacementPred (GHC.L l _) = l == replExprLocation
transformation = everywhereM (mkM (doGenReplacement m (replacementPred . decomposeSrcSpan) newExpr))
transformation = transformBiM (doGenReplacement m (replacementPred . decomposeSrcSpan) newExpr)
runStateT (transformation m) (newAnns, False) >>= \case
(finalM, (finalAs, True)) -> pure (ensureSpace finalAs, finalM)
-- Failed to find a replacment so don't make any changes
Expand Down Expand Up @@ -651,10 +652,10 @@ findOrError m = either f pure . findInModule m
-- Deletion from a list

doDeleteStmt :: Data a => (Stmt -> Bool) -> a -> a
doDeleteStmt p = everywhere (mkT (filter p))
doDeleteStmt = transformBi . filter

doDeleteImport :: Data a => (Import -> Bool) -> a -> a
doDeleteImport p = everywhere (mkT (filter p))
doDeleteImport = transformBi . filter

{-
-- Renaming
Expand Down

0 comments on commit e11ebc2

Please sign in to comment.