{-# LANGUAGE ViewPatterns, PatternGuards, FlexibleContexts #-}
module Hint.List(listHint) where
import Control.Applicative
import Data.Generics.Uniplate.Operations
import Data.List.Extra
import Data.Maybe
import Prelude
import Hint.Type(DeclHint',Idea,suggest',toSS')
import Refact.Types hiding (SrcSpan)
import qualified Refact.Types as R
import HsSyn
import SrcLoc
import BasicTypes
import RdrName
import OccName
import Name
import FastString
import TysWiredIn
import GHC.Util
import Language.Haskell.GhclibParserEx.GHC.Hs.Expr
import Language.Haskell.GhclibParserEx.GHC.Hs.ExtendInstances
listHint :: DeclHint'
listHint :: DeclHint'
listHint _ _ = LHsDecl GhcPs -> [Idea]
listDecl
listDecl :: LHsDecl GhcPs -> [Idea]
listDecl :: LHsDecl GhcPs -> [Idea]
listDecl x :: LHsDecl GhcPs
x =
(LHsExpr GhcPs -> [Idea]) -> [LHsExpr GhcPs] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Bool -> LHsExpr GhcPs -> [Idea]
listExp Bool
False) (LHsDecl GhcPs -> [LHsExpr GhcPs]
forall from to. Biplate from to => from -> [to]
childrenBi LHsDecl GhcPs
x) [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++
LHsDecl GhcPs -> [Idea]
stringType LHsDecl GhcPs
x [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++
(Pat GhcPs -> [Idea]) -> [Pat GhcPs] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Pat GhcPs -> [Idea]
listPat (LHsDecl GhcPs -> [Pat GhcPs]
forall from to. Biplate from to => from -> [to]
childrenBi LHsDecl GhcPs
x) [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++
(LHsExpr GhcPs -> [Idea]) -> [LHsExpr GhcPs] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LHsExpr GhcPs -> [Idea]
listComp (LHsDecl GhcPs -> [LHsExpr GhcPs]
forall from to. Biplate from to => from -> [to]
universeBi LHsDecl GhcPs
x)
listComp :: LHsExpr GhcPs -> [Idea]
listComp :: LHsExpr GhcPs -> [Idea]
listComp o :: LHsExpr GhcPs
o@(LL _ (HsDo _ ListComp (L _ stmts))) =
LHsExpr GhcPs -> HsStmtContext Name -> [ExprLStmt GhcPs] -> [Idea]
listCompCheckGuards LHsExpr GhcPs
o HsStmtContext Name
forall id. HsStmtContext id
ListComp [ExprLStmt GhcPs]
stmts
listComp o :: LHsExpr GhcPs
o@(LL _ (HsDo _ MonadComp (L _ stmts))) =
LHsExpr GhcPs -> HsStmtContext Name -> [ExprLStmt GhcPs] -> [Idea]
listCompCheckGuards LHsExpr GhcPs
o HsStmtContext Name
forall id. HsStmtContext id
MonadComp [ExprLStmt GhcPs]
stmts
listComp o :: LHsExpr GhcPs
o@(LHsExpr GhcPs -> App2'
forall a b. View' a b => a -> b
view' -> App2' mp :: LHsExpr GhcPs
mp f :: LHsExpr GhcPs
f (LL _ (HsDo _ ListComp (L _ stmts)))) =
LHsExpr GhcPs
-> LHsExpr GhcPs
-> LHsExpr GhcPs
-> HsStmtContext Name
-> [ExprLStmt GhcPs]
-> [Idea]
listCompCheckMap LHsExpr GhcPs
o LHsExpr GhcPs
mp LHsExpr GhcPs
f HsStmtContext Name
forall id. HsStmtContext id
ListComp [ExprLStmt GhcPs]
stmts
listComp o :: LHsExpr GhcPs
o@(LHsExpr GhcPs -> App2'
forall a b. View' a b => a -> b
view' -> App2' mp :: LHsExpr GhcPs
mp f :: LHsExpr GhcPs
f (LL _ (HsDo _ MonadComp (L _ stmts)))) =
LHsExpr GhcPs
-> LHsExpr GhcPs
-> LHsExpr GhcPs
-> HsStmtContext Name
-> [ExprLStmt GhcPs]
-> [Idea]
listCompCheckMap LHsExpr GhcPs
o LHsExpr GhcPs
mp LHsExpr GhcPs
f HsStmtContext Name
forall id. HsStmtContext id
MonadComp [ExprLStmt GhcPs]
stmts
listComp _ = []
listCompCheckGuards :: LHsExpr GhcPs -> HsStmtContext Name -> [ExprLStmt GhcPs] -> [Idea]
listCompCheckGuards :: LHsExpr GhcPs -> HsStmtContext Name -> [ExprLStmt GhcPs] -> [Idea]
listCompCheckGuards o :: LHsExpr GhcPs
o ctx :: HsStmtContext Name
ctx stmts :: [ExprLStmt GhcPs]
stmts =
let revs :: [ExprLStmt GhcPs]
revs = [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a]
reverse [ExprLStmt GhcPs]
stmts
e :: ExprLStmt GhcPs
e@(LL _ LastStmt{}) = [ExprLStmt GhcPs] -> ExprLStmt GhcPs
forall a. [a] -> a
head [ExprLStmt GhcPs]
revs
xs :: [ExprLStmt GhcPs]
xs = [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a]
reverse ([ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a]
tail [ExprLStmt GhcPs]
revs) in
ExprLStmt GhcPs -> [ExprLStmt GhcPs] -> [Idea]
list_comp_aux ExprLStmt GhcPs
e [ExprLStmt GhcPs]
xs
where
list_comp_aux :: ExprLStmt GhcPs -> [ExprLStmt GhcPs] -> [Idea]
list_comp_aux e :: ExprLStmt GhcPs
e xs :: [ExprLStmt GhcPs]
xs
| "False" [Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
cons = [[Char]
-> LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
[Char] -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest' "Short-circuited list comprehension" LHsExpr GhcPs
o LHsExpr GhcPs
o' (LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan]
suggestExpr LHsExpr GhcPs
o LHsExpr GhcPs
o')]
| "True" [Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [[Char]]
cons = [[Char]
-> LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
[Char] -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest' "Redundant True guards" LHsExpr GhcPs
o LHsExpr GhcPs
o2 (LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan]
suggestExpr LHsExpr GhcPs
o LHsExpr GhcPs
o2)]
| Bool -> Bool
not ([ExprLStmt GhcPs] -> [ExprLStmt GhcPs] -> Bool
forall a. Data a => [a] -> [a] -> Bool
astListEq [ExprLStmt GhcPs]
xs [ExprLStmt GhcPs]
ys) = [[Char]
-> LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
[Char] -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest' "Move guards forward" LHsExpr GhcPs
o LHsExpr GhcPs
o3 (LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan]
suggestExpr LHsExpr GhcPs
o LHsExpr GhcPs
o3)]
| Bool
otherwise = []
where
ys :: [ExprLStmt GhcPs]
ys = [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
moveGuardsForward [ExprLStmt GhcPs]
xs
o' :: LHsExpr GhcPs
o' = SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs)
-> SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ XExplicitList GhcPs
-> Maybe (SyntaxExpr GhcPs) -> [LHsExpr GhcPs] -> HsExpr GhcPs
forall p.
XExplicitList p -> Maybe (SyntaxExpr p) -> [LHsExpr p] -> HsExpr p
ExplicitList NoExt
XExplicitList GhcPs
noExt Maybe (SyntaxExpr GhcPs)
forall a. Maybe a
Nothing []
o2 :: LHsExpr GhcPs
o2 = SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs)
-> SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ XDo GhcPs
-> HsStmtContext Name -> Located [ExprLStmt GhcPs] -> HsExpr GhcPs
forall p.
XDo p -> HsStmtContext Name -> Located [ExprLStmt p] -> HsExpr p
HsDo NoExt
XDo GhcPs
noExt HsStmtContext Name
ctx (SrcSpanLess (Located [ExprLStmt GhcPs])
-> Located [ExprLStmt GhcPs]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc ((ExprLStmt GhcPs -> Bool) -> [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Maybe [Char] -> Maybe [Char] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just "True") (Maybe [Char] -> Bool)
-> (ExprLStmt GhcPs -> Maybe [Char]) -> ExprLStmt GhcPs -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExprLStmt GhcPs -> Maybe [Char]
qualCon) [ExprLStmt GhcPs]
xs [ExprLStmt GhcPs] -> [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a] -> [a]
++ [ExprLStmt GhcPs
e]))
o3 :: LHsExpr GhcPs
o3 = SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs)
-> SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ XDo GhcPs
-> HsStmtContext Name -> Located [ExprLStmt GhcPs] -> HsExpr GhcPs
forall p.
XDo p -> HsStmtContext Name -> Located [ExprLStmt p] -> HsExpr p
HsDo NoExt
XDo GhcPs
noExt HsStmtContext Name
ctx (SrcSpanLess (Located [ExprLStmt GhcPs])
-> Located [ExprLStmt GhcPs]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located [ExprLStmt GhcPs])
-> Located [ExprLStmt GhcPs])
-> SrcSpanLess (Located [ExprLStmt GhcPs])
-> Located [ExprLStmt GhcPs]
forall a b. (a -> b) -> a -> b
$ [ExprLStmt GhcPs]
ys [ExprLStmt GhcPs] -> [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a] -> [a]
++ [ExprLStmt GhcPs
e])
cons :: [[Char]]
cons = (ExprLStmt GhcPs -> Maybe [Char]) -> [ExprLStmt GhcPs] -> [[Char]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe ExprLStmt GhcPs -> Maybe [Char]
qualCon [ExprLStmt GhcPs]
xs
qualCon :: ExprLStmt GhcPs -> Maybe String
qualCon :: ExprLStmt GhcPs -> Maybe [Char]
qualCon (L _ (BodyStmt _ (LL _ (HsVar _ (L _ x))) _ _)) = [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just (OccName -> [Char]
occNameString (OccName -> [Char]) -> (RdrName -> OccName) -> RdrName -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RdrName -> OccName
rdrNameOcc (RdrName -> [Char]) -> RdrName -> [Char]
forall a b. (a -> b) -> a -> b
$ RdrName
IdP GhcPs
x)
qualCon _ = Maybe [Char]
forall a. Maybe a
Nothing
listCompCheckMap ::
LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsStmtContext Name -> [ExprLStmt GhcPs] -> [Idea]
listCompCheckMap :: LHsExpr GhcPs
-> LHsExpr GhcPs
-> LHsExpr GhcPs
-> HsStmtContext Name
-> [ExprLStmt GhcPs]
-> [Idea]
listCompCheckMap o :: LHsExpr GhcPs
o mp :: LHsExpr GhcPs
mp f :: LHsExpr GhcPs
f ctx :: HsStmtContext Name
ctx stmts :: [ExprLStmt GhcPs]
stmts | LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
mp [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== "map" =
[[Char]
-> LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
[Char] -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest' "Move map inside list comprehension" LHsExpr GhcPs
o LHsExpr GhcPs
o2 (LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan]
suggestExpr LHsExpr GhcPs
o LHsExpr GhcPs
o2)]
where
revs :: [ExprLStmt GhcPs]
revs = [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a]
reverse [ExprLStmt GhcPs]
stmts
LL _ (LastStmt _ body b s) = [ExprLStmt GhcPs] -> ExprLStmt GhcPs
forall a. [a] -> a
head [ExprLStmt GhcPs]
revs
last :: ExprLStmt GhcPs
last = SrcSpanLess (ExprLStmt GhcPs) -> ExprLStmt GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (ExprLStmt GhcPs) -> ExprLStmt GhcPs)
-> SrcSpanLess (ExprLStmt GhcPs) -> ExprLStmt GhcPs
forall a b. (a -> b) -> a -> b
$ XLastStmt GhcPs GhcPs (LHsExpr GhcPs)
-> LHsExpr GhcPs
-> Bool
-> SyntaxExpr GhcPs
-> StmtLR GhcPs GhcPs (LHsExpr GhcPs)
forall idL idR body.
XLastStmt idL idR body
-> body -> Bool -> SyntaxExpr idR -> StmtLR idL idR body
LastStmt NoExt
XLastStmt GhcPs GhcPs (LHsExpr GhcPs)
noExt (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs)
-> SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ XApp GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp NoExt
XApp GhcPs
noExt (LHsExpr GhcPs -> LHsExpr GhcPs
paren' LHsExpr GhcPs
f) (LHsExpr GhcPs -> LHsExpr GhcPs
paren' LHsExpr GhcPs
body)) Bool
b SyntaxExpr GhcPs
s
o2 :: LHsExpr GhcPs
o2 =SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs)
-> SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ XDo GhcPs
-> HsStmtContext Name -> Located [ExprLStmt GhcPs] -> HsExpr GhcPs
forall p.
XDo p -> HsStmtContext Name -> Located [ExprLStmt p] -> HsExpr p
HsDo NoExt
XDo GhcPs
noExt HsStmtContext Name
ctx (SrcSpanLess (Located [ExprLStmt GhcPs])
-> Located [ExprLStmt GhcPs]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located [ExprLStmt GhcPs])
-> Located [ExprLStmt GhcPs])
-> SrcSpanLess (Located [ExprLStmt GhcPs])
-> Located [ExprLStmt GhcPs]
forall a b. (a -> b) -> a -> b
$ [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a]
reverse ([ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a]
tail [ExprLStmt GhcPs]
revs) [ExprLStmt GhcPs] -> [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a] -> [a]
++ [ExprLStmt GhcPs
last])
listCompCheckMap _ _ _ _ _ = []
suggestExpr :: LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring R.SrcSpan]
suggestExpr :: LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan]
suggestExpr o :: LHsExpr GhcPs
o o2 :: LHsExpr GhcPs
o2 = [RType
-> SrcSpan -> [([Char], SrcSpan)] -> [Char] -> Refactoring SrcSpan
forall a. RType -> a -> [([Char], a)] -> [Char] -> Refactoring a
Replace RType
Expr (LHsExpr GhcPs -> SrcSpan
forall e. HasSrcSpan e => e -> SrcSpan
toSS' LHsExpr GhcPs
o) [] (LHsExpr GhcPs -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint LHsExpr GhcPs
o2)]
moveGuardsForward :: [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
moveGuardsForward :: [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
moveGuardsForward = [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a]
reverse ([ExprLStmt GhcPs] -> [ExprLStmt GhcPs])
-> ([ExprLStmt GhcPs] -> [ExprLStmt GhcPs])
-> [ExprLStmt GhcPs]
-> [ExprLStmt GhcPs]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ExprLStmt GhcPs] -> [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall idL l idR body.
(AllVars' (LPat idL), Data l, Data (StmtLR idL idR body)) =>
[GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
f [] ([ExprLStmt GhcPs] -> [ExprLStmt GhcPs])
-> ([ExprLStmt GhcPs] -> [ExprLStmt GhcPs])
-> [ExprLStmt GhcPs]
-> [ExprLStmt GhcPs]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a]
reverse
where
f :: [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
f guards :: [GenLocated l (StmtLR idL idR body)]
guards (x :: GenLocated l (StmtLR idL idR body)
x@(L _ (BindStmt _ p :: LPat idL
p _ _ _)) : xs :: [GenLocated l (StmtLR idL idR body)]
xs) = [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
forall a. [a] -> [a]
reverse [GenLocated l (StmtLR idL idR body)]
stop [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
forall a. [a] -> [a] -> [a]
++ GenLocated l (StmtLR idL idR body)
x GenLocated l (StmtLR idL idR body)
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
forall a. a -> [a] -> [a]
: [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
f [GenLocated l (StmtLR idL idR body)]
move [GenLocated l (StmtLR idL idR body)]
xs
where (move :: [GenLocated l (StmtLR idL idR body)]
move, stop :: [GenLocated l (StmtLR idL idR body)]
stop) =
(GenLocated l (StmtLR idL idR body) -> Bool)
-> [GenLocated l (StmtLR idL idR body)]
-> ([GenLocated l (StmtLR idL idR body)],
[GenLocated l (StmtLR idL idR body)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (if (HsRecFields GhcPs (Pat GhcPs) -> Bool)
-> [HsRecFields GhcPs (Pat GhcPs)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any HsRecFields GhcPs (Pat GhcPs) -> Bool
hasPFieldsDotDot' (GenLocated l (StmtLR idL idR body)
-> [HsRecFields GhcPs (Pat GhcPs)]
forall from to. Biplate from to => from -> [to]
universeBi GenLocated l (StmtLR idL idR body)
x)
Bool -> Bool -> Bool
|| (LHsRecField GhcPs (Pat GhcPs) -> Bool)
-> [LHsRecField GhcPs (Pat GhcPs)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any LHsRecField GhcPs (Pat GhcPs) -> Bool
isPFieldWildcard' (GenLocated l (StmtLR idL idR body)
-> [LHsRecField GhcPs (Pat GhcPs)]
forall from to. Biplate from to => from -> [to]
universeBi GenLocated l (StmtLR idL idR body)
x)
then Bool -> GenLocated l (StmtLR idL idR body) -> Bool
forall a b. a -> b -> a
const Bool
False
else \x :: GenLocated l (StmtLR idL idR body)
x -> LPat idL -> [[Char]]
forall a. AllVars' a => a -> [[Char]]
pvars' LPat idL
p [[Char]] -> [[Char]] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`disjoint` GenLocated l (StmtLR idL idR body) -> [[Char]]
forall from. Data from => from -> [[Char]]
vars_ GenLocated l (StmtLR idL idR body)
x) [GenLocated l (StmtLR idL idR body)]
guards
f guards :: [GenLocated l (StmtLR idL idR body)]
guards (x :: GenLocated l (StmtLR idL idR body)
x@(L _ BodyStmt{}):xs :: [GenLocated l (StmtLR idL idR body)]
xs) = [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
f (GenLocated l (StmtLR idL idR body)
xGenLocated l (StmtLR idL idR body)
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
forall a. a -> [a] -> [a]
:[GenLocated l (StmtLR idL idR body)]
guards) [GenLocated l (StmtLR idL idR body)]
xs
f guards :: [GenLocated l (StmtLR idL idR body)]
guards (x :: GenLocated l (StmtLR idL idR body)
x@(L _ LetStmt{}):xs :: [GenLocated l (StmtLR idL idR body)]
xs) = [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
f (GenLocated l (StmtLR idL idR body)
xGenLocated l (StmtLR idL idR body)
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
forall a. a -> [a] -> [a]
:[GenLocated l (StmtLR idL idR body)]
guards) [GenLocated l (StmtLR idL idR body)]
xs
f guards :: [GenLocated l (StmtLR idL idR body)]
guards xs :: [GenLocated l (StmtLR idL idR body)]
xs = [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
forall a. [a] -> [a]
reverse [GenLocated l (StmtLR idL idR body)]
guards [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
-> [GenLocated l (StmtLR idL idR body)]
forall a. [a] -> [a] -> [a]
++ [GenLocated l (StmtLR idL idR body)]
xs
vars_ :: from -> [[Char]]
vars_ x :: from
x = [RdrName -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint RdrName
SrcSpanLess (Located RdrName)
a | HsVar _ (LL _ a) <- from -> [HsExpr GhcPs]
forall from to. Biplate from to => from -> [to]
universeBi from
x :: [HsExpr GhcPs]]
listExp :: Bool -> LHsExpr GhcPs -> [Idea]
listExp :: Bool -> LHsExpr GhcPs -> [Idea]
listExp b :: Bool
b (LHsExpr GhcPs -> LHsExpr GhcPs
fromParen' -> LHsExpr GhcPs
x) =
if [Idea] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Idea]
res then (LHsExpr GhcPs -> [Idea]) -> [LHsExpr GhcPs] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Bool -> LHsExpr GhcPs -> [Idea]
listExp (Bool -> LHsExpr GhcPs -> [Idea])
-> Bool -> LHsExpr GhcPs -> [Idea]
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcPs -> Bool
forall a. View' a App2' => a -> Bool
isAppend LHsExpr GhcPs
x) ([LHsExpr GhcPs] -> [Idea]) -> [LHsExpr GhcPs] -> [Idea]
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcPs -> [LHsExpr GhcPs]
forall on. Uniplate on => on -> [on]
children LHsExpr GhcPs
x else [[Idea] -> Idea
forall a. [a] -> a
head [Idea]
res]
where
res :: [Idea]
res = [[Char]
-> LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
[Char] -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest' [Char]
name LHsExpr GhcPs
x LHsExpr GhcPs
x2 [Refactoring SrcSpan
r]
| (name :: [Char]
name, f :: Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
f) <- [([Char],
Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))]
checks
, Just (x2 :: LHsExpr GhcPs
x2, subts :: [([Char], SrcSpan)]
subts, temp :: [Char]
temp) <- [Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
f Bool
b LHsExpr GhcPs
x]
, let r :: Refactoring SrcSpan
r = RType
-> SrcSpan -> [([Char], SrcSpan)] -> [Char] -> Refactoring SrcSpan
forall a. RType -> a -> [([Char], a)] -> [Char] -> Refactoring a
Replace RType
Expr (LHsExpr GhcPs -> SrcSpan
forall e. HasSrcSpan e => e -> SrcSpan
toSS' LHsExpr GhcPs
x) [([Char], SrcSpan)]
subts [Char]
temp ]
listPat :: Pat GhcPs -> [Idea]
listPat :: Pat GhcPs -> [Idea]
listPat x :: Pat GhcPs
x = if [Idea] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Idea]
res then (Pat GhcPs -> [Idea]) -> [Pat GhcPs] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Pat GhcPs -> [Idea]
listPat ([Pat GhcPs] -> [Idea]) -> [Pat GhcPs] -> [Idea]
forall a b. (a -> b) -> a -> b
$ Pat GhcPs -> [Pat GhcPs]
forall on. Uniplate on => on -> [on]
children Pat GhcPs
x else [[Idea] -> Idea
forall a. [a] -> a
head [Idea]
res]
where res :: [Idea]
res = [[Char] -> Pat GhcPs -> Pat GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
[Char] -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest' [Char]
name Pat GhcPs
x Pat GhcPs
x2 [Refactoring SrcSpan
r]
| (name :: [Char]
name, f :: Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char])
f) <- [([Char],
Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char]))]
pchecks
, Just (x2 :: Pat GhcPs
x2, subts :: [([Char], SrcSpan)]
subts, temp :: [Char]
temp) <- [Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char])
f Pat GhcPs
x]
, let r :: Refactoring SrcSpan
r = RType
-> SrcSpan -> [([Char], SrcSpan)] -> [Char] -> Refactoring SrcSpan
forall a. RType -> a -> [([Char], a)] -> [Char] -> Refactoring a
Replace RType
Pattern (Pat GhcPs -> SrcSpan
forall e. HasSrcSpan e => e -> SrcSpan
toSS' Pat GhcPs
x) [([Char], SrcSpan)]
subts [Char]
temp ]
isAppend :: View' a App2' => a -> Bool
isAppend :: a -> Bool
isAppend (a -> App2'
forall a b. View' a b => a -> b
view' -> App2' op :: LHsExpr GhcPs
op _ _) = LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
op [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== "++"
isAppend _ = Bool
False
checks ::[(String, Bool -> LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [(String, R.SrcSpan)], String))]
checks :: [([Char],
Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))]
checks = let * :: a -> b -> (a, b)
(*) = (,) in Int
-> [([Char],
Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))]
-> [([Char],
Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))]
forall a. Int -> [a] -> [a]
drop 1
[ "Use string literal" [Char]
-> (Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))
-> ([Char],
Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))
forall a b. a -> b -> (a, b)
* Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
forall p a.
p -> LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [a], [Char])
useString
, "Use list literal" [Char]
-> (Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))
-> ([Char],
Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))
forall a b. a -> b -> (a, b)
* Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
forall p.
p
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
useList
, "Use :" [Char]
-> (Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))
-> ([Char],
Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))
forall a b. a -> b -> (a, b)
* Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
forall a.
View' a App2' =>
Bool -> a -> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
useCons
]
pchecks :: [(String, Pat GhcPs -> Maybe (Pat GhcPs, [(String, R.SrcSpan)], String))]
pchecks :: [([Char],
Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char]))]
pchecks = let * :: a -> b -> (a, b)
(*) = (,) in Int
-> [([Char],
Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char]))]
-> [([Char],
Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char]))]
forall a. Int -> [a] -> [a]
drop 1
[ "Use string literal pattern" [Char]
-> (Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char]))
-> ([Char],
Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char]))
forall a b. a -> b -> (a, b)
* Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char])
forall a. Pat GhcPs -> Maybe (Pat GhcPs, [a], [Char])
usePString
, "Use list literal pattern" [Char]
-> (Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char]))
-> ([Char],
Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char]))
forall a b. a -> b -> (a, b)
* Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char])
usePList
]
usePString :: Pat GhcPs -> Maybe (Pat GhcPs, [a], String)
usePString :: Pat GhcPs -> Maybe (Pat GhcPs, [a], [Char])
usePString (LL _ (ListPat _ xs)) | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Pat GhcPs] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Pat GhcPs]
xs, Just s :: [Char]
s <- (Pat GhcPs -> Maybe Char) -> [Pat GhcPs] -> Maybe [Char]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Pat GhcPs -> Maybe Char
fromPChar' [Pat GhcPs]
xs =
let literal :: Pat GhcPs
literal = SrcSpanLess (Pat GhcPs) -> Pat GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Pat GhcPs) -> Pat GhcPs)
-> SrcSpanLess (Pat GhcPs) -> Pat GhcPs
forall a b. (a -> b) -> a -> b
$ XLitPat GhcPs -> HsLit GhcPs -> Pat GhcPs
forall p. XLitPat p -> HsLit p -> Pat p
LitPat NoExt
XLitPat GhcPs
noExt (XHsString GhcPs -> FastString -> HsLit GhcPs
forall x. XHsString x -> FastString -> HsLit x
HsString XHsString GhcPs
SourceText
NoSourceText ([Char] -> FastString
fsLit ([Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
s)))
in (Pat GhcPs, [a], [Char]) -> Maybe (Pat GhcPs, [a], [Char])
forall a. a -> Maybe a
Just (Pat GhcPs
literal, [], Pat GhcPs -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint Pat GhcPs
literal)
usePString _ = Maybe (Pat GhcPs, [a], [Char])
forall a. Maybe a
Nothing
usePList :: Pat GhcPs -> Maybe (Pat GhcPs, [(String, R.SrcSpan)], String)
usePList :: Pat GhcPs -> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char])
usePList =
([(Pat GhcPs, ([Char], Pat GhcPs))]
-> (Pat GhcPs, [([Char], SrcSpan)], [Char]))
-> Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
-> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ( (\(e :: [Pat GhcPs]
e, s :: [([Char], Pat GhcPs)]
s) ->
(SrcSpanLess (Pat GhcPs) -> Pat GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XListPat GhcPs -> [Pat GhcPs] -> Pat GhcPs
forall p. XListPat p -> [Pat p] -> Pat p
ListPat NoExt
XListPat GhcPs
noExt [Pat GhcPs]
e)
, (([Char], Pat GhcPs) -> ([Char], SrcSpan))
-> [([Char], Pat GhcPs)] -> [([Char], SrcSpan)]
forall a b. (a -> b) -> [a] -> [b]
map ((Pat GhcPs -> SrcSpan) -> ([Char], Pat GhcPs) -> ([Char], SrcSpan)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pat GhcPs -> SrcSpan
forall e. HasSrcSpan e => e -> SrcSpan
toSS') [([Char], Pat GhcPs)]
s
, Pat GhcPs -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint (SrcSpanLess (Pat GhcPs) -> Pat GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Pat GhcPs) -> Pat GhcPs)
-> SrcSpanLess (Pat GhcPs) -> Pat GhcPs
forall a b. (a -> b) -> a -> b
$ XListPat GhcPs -> [Pat GhcPs] -> Pat GhcPs
forall p. XListPat p -> [Pat p] -> Pat p
ListPat NoExt
XListPat GhcPs
noExt ((([Char], Pat GhcPs) -> Pat GhcPs)
-> [([Char], Pat GhcPs)] -> [Pat GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map ([Char], Pat GhcPs) -> Pat GhcPs
forall a b. (a, b) -> b
snd [([Char], Pat GhcPs)]
s) :: Pat GhcPs))
)
(([Pat GhcPs], [([Char], Pat GhcPs)])
-> (Pat GhcPs, [([Char], SrcSpan)], [Char]))
-> ([(Pat GhcPs, ([Char], Pat GhcPs))]
-> ([Pat GhcPs], [([Char], Pat GhcPs)]))
-> [(Pat GhcPs, ([Char], Pat GhcPs))]
-> (Pat GhcPs, [([Char], SrcSpan)], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Pat GhcPs, ([Char], Pat GhcPs))]
-> ([Pat GhcPs], [([Char], Pat GhcPs)])
forall a b. [(a, b)] -> ([a], [b])
unzip
)
(Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
-> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char]))
-> (Pat GhcPs -> Maybe [(Pat GhcPs, ([Char], Pat GhcPs))])
-> Pat GhcPs
-> Maybe (Pat GhcPs, [([Char], SrcSpan)], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> [Char] -> Pat GhcPs -> Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
f Bool
True ['a'..'z']
where
f :: Bool
-> [Char] -> Pat GhcPs -> Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
f first :: Bool
first _ x :: Pat GhcPs
x | Pat GhcPs -> [Char]
patToStr' Pat GhcPs
x [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== "[]" = if Bool
first then Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
forall a. Maybe a
Nothing else [(Pat GhcPs, ([Char], Pat GhcPs))]
-> Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
forall a. a -> Maybe a
Just []
f first :: Bool
first (ident :: Char
ident:cs :: [Char]
cs) (Pat GhcPs -> PApp_'
forall a b. View' a b => a -> b
view' -> PApp_' ":" [a :: Pat GhcPs
a, b :: Pat GhcPs
b]) = ((Pat GhcPs
a, Char -> Pat GhcPs -> ([Char], Pat GhcPs)
g Char
ident Pat GhcPs
a) (Pat GhcPs, ([Char], Pat GhcPs))
-> [(Pat GhcPs, ([Char], Pat GhcPs))]
-> [(Pat GhcPs, ([Char], Pat GhcPs))]
forall a. a -> [a] -> [a]
:) ([(Pat GhcPs, ([Char], Pat GhcPs))]
-> [(Pat GhcPs, ([Char], Pat GhcPs))])
-> Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
-> Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool
-> [Char] -> Pat GhcPs -> Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
f Bool
False [Char]
cs Pat GhcPs
b
f first :: Bool
first _ _ = Maybe [(Pat GhcPs, ([Char], Pat GhcPs))]
forall a. Maybe a
Nothing
g :: Char -> Pat GhcPs -> (String, Pat GhcPs)
g :: Char -> Pat GhcPs -> ([Char], Pat GhcPs)
g c :: Char
c p :: Pat GhcPs
p = ([Char
c], XVarPat GhcPs -> Located (IdP GhcPs) -> Pat GhcPs
forall p. XVarPat p -> Located (IdP p) -> Pat p
VarPat NoExt
XVarPat GhcPs
noExt (SrcSpanLess (Located RdrName) -> Located (IdP GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located RdrName) -> Located (IdP GhcPs))
-> SrcSpanLess (Located RdrName) -> Located (IdP GhcPs)
forall a b. (a -> b) -> a -> b
$ FastString -> RdrName
mkVarUnqual ([Char] -> FastString
fsLit [Char
c])))
useString :: p -> LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [a], String)
useString :: p -> LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [a], [Char])
useString b :: p
b (LL _ (ExplicitList _ _ xs)) | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [LHsExpr GhcPs] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsExpr GhcPs]
xs, Just s :: [Char]
s <- (LHsExpr GhcPs -> Maybe Char) -> [LHsExpr GhcPs] -> Maybe [Char]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LHsExpr GhcPs -> Maybe Char
fromChar [LHsExpr GhcPs]
xs =
let literal :: LHsExpr GhcPs
literal = SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XLitE GhcPs -> HsLit GhcPs -> HsExpr GhcPs
forall p. XLitE p -> HsLit p -> HsExpr p
HsLit NoExt
XLitE GhcPs
noExt (XHsString GhcPs -> FastString -> HsLit GhcPs
forall x. XHsString x -> FastString -> HsLit x
HsString XHsString GhcPs
SourceText
NoSourceText ([Char] -> FastString
fsLit ([Char] -> [Char]
forall a. Show a => a -> [Char]
show [Char]
s))))
in (LHsExpr GhcPs, [a], [Char]) -> Maybe (LHsExpr GhcPs, [a], [Char])
forall a. a -> Maybe a
Just (LHsExpr GhcPs
literal, [], LHsExpr GhcPs -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint LHsExpr GhcPs
literal)
useString _ _ = Maybe (LHsExpr GhcPs, [a], [Char])
forall a. Maybe a
Nothing
useList :: p -> LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, [(String, R.SrcSpan)], String)
useList :: p
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
useList b :: p
b =
([(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))
-> Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ( (\(e :: [LHsExpr GhcPs]
e, s :: [([Char], LHsExpr GhcPs)]
s) ->
(SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XExplicitList GhcPs
-> Maybe (SyntaxExpr GhcPs) -> [LHsExpr GhcPs] -> HsExpr GhcPs
forall p.
XExplicitList p -> Maybe (SyntaxExpr p) -> [LHsExpr p] -> HsExpr p
ExplicitList NoExt
XExplicitList GhcPs
noExt Maybe (SyntaxExpr GhcPs)
forall a. Maybe a
Nothing [LHsExpr GhcPs]
e)
, (([Char], LHsExpr GhcPs) -> ([Char], SrcSpan))
-> [([Char], LHsExpr GhcPs)] -> [([Char], SrcSpan)]
forall a b. (a -> b) -> [a] -> [b]
map ((LHsExpr GhcPs -> SrcSpan)
-> ([Char], LHsExpr GhcPs) -> ([Char], SrcSpan)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap LHsExpr GhcPs -> SrcSpan
forall e. HasSrcSpan e => e -> SrcSpan
toSS') [([Char], LHsExpr GhcPs)]
s
, LHsExpr GhcPs -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs)
-> SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ XExplicitList GhcPs
-> Maybe (SyntaxExpr GhcPs) -> [LHsExpr GhcPs] -> HsExpr GhcPs
forall p.
XExplicitList p -> Maybe (SyntaxExpr p) -> [LHsExpr p] -> HsExpr p
ExplicitList NoExt
XExplicitList GhcPs
noExt Maybe (SyntaxExpr GhcPs)
forall a. Maybe a
Nothing ((([Char], LHsExpr GhcPs) -> LHsExpr GhcPs)
-> [([Char], LHsExpr GhcPs)] -> [LHsExpr GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map ([Char], LHsExpr GhcPs) -> LHsExpr GhcPs
forall a b. (a, b) -> b
snd [([Char], LHsExpr GhcPs)]
s) :: LHsExpr GhcPs))
)
(([LHsExpr GhcPs], [([Char], LHsExpr GhcPs)])
-> (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))
-> ([(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> ([LHsExpr GhcPs], [([Char], LHsExpr GhcPs)]))
-> [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> ([LHsExpr GhcPs], [([Char], LHsExpr GhcPs)])
forall a b. [(a, b)] -> ([a], [b])
unzip
)
(Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))
-> (LHsExpr GhcPs
-> Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))])
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> [Char]
-> LHsExpr GhcPs
-> Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
f Bool
True ['a'..'z']
where
f :: Bool
-> [Char]
-> LHsExpr GhcPs
-> Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
f first :: Bool
first _ x :: LHsExpr GhcPs
x | LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
x [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== "[]" = if Bool
first then Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
forall a. Maybe a
Nothing else [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
forall a. a -> Maybe a
Just []
f first :: Bool
first (ident :: Char
ident:cs :: [Char]
cs) (LHsExpr GhcPs -> App2'
forall a b. View' a b => a -> b
view' -> App2' c :: LHsExpr GhcPs
c a :: LHsExpr GhcPs
a b :: LHsExpr GhcPs
b) | LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
c [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== ":" =
((LHsExpr GhcPs
a, Char -> LHsExpr GhcPs -> ([Char], LHsExpr GhcPs)
g Char
ident LHsExpr GhcPs
a) (LHsExpr GhcPs, ([Char], LHsExpr GhcPs))
-> [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
forall a. a -> [a] -> [a]
:) ([(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))])
-> Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
-> Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool
-> [Char]
-> LHsExpr GhcPs
-> Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
f Bool
False [Char]
cs LHsExpr GhcPs
b
f first :: Bool
first _ _ = Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
forall a. Maybe a
Nothing
g :: Char -> LHsExpr GhcPs -> (String, LHsExpr GhcPs)
g :: Char -> LHsExpr GhcPs -> ([Char], LHsExpr GhcPs)
g c :: Char
c p :: LHsExpr GhcPs
p = ([Char
c], [Char] -> LHsExpr GhcPs
strToVar [Char
c])
useCons :: View' a App2' => Bool -> a -> Maybe (LHsExpr GhcPs, [(String, R.SrcSpan)], String)
useCons :: Bool -> a -> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
useCons False (a -> App2'
forall a b. View' a b => a -> b
view' -> App2' op :: LHsExpr GhcPs
op x :: LHsExpr GhcPs
x y :: LHsExpr GhcPs
y) | LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
op [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== "++"
, Just (x2 :: LHsExpr GhcPs
x2, build :: LHsExpr GhcPs -> LHsExpr GhcPs
build) <- LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, LHsExpr GhcPs -> LHsExpr GhcPs)
f LHsExpr GhcPs
x
, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcPs -> Bool
forall a. View' a App2' => a -> Bool
isAppend LHsExpr GhcPs
y =
(LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
forall a. a -> Maybe a
Just (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
gen (LHsExpr GhcPs -> LHsExpr GhcPs
build LHsExpr GhcPs
x2) LHsExpr GhcPs
y
, [("x", LHsExpr GhcPs -> SrcSpan
forall e. HasSrcSpan e => e -> SrcSpan
toSS' LHsExpr GhcPs
x2), ("xs", LHsExpr GhcPs -> SrcSpan
forall e. HasSrcSpan e => e -> SrcSpan
toSS' LHsExpr GhcPs
y)]
, LHsExpr GhcPs -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint (LHsExpr GhcPs -> [Char]) -> LHsExpr GhcPs -> [Char]
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
gen (LHsExpr GhcPs -> LHsExpr GhcPs
build (LHsExpr GhcPs -> LHsExpr GhcPs) -> LHsExpr GhcPs -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ [Char] -> LHsExpr GhcPs
strToVar "x") ([Char] -> LHsExpr GhcPs
strToVar "xs")
)
where
f :: LHsExpr GhcPs ->
Maybe (LHsExpr GhcPs, LHsExpr GhcPs -> LHsExpr GhcPs)
f :: LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, LHsExpr GhcPs -> LHsExpr GhcPs)
f (LL _ (ExplicitList _ _ [x]))=
(LHsExpr GhcPs, LHsExpr GhcPs -> LHsExpr GhcPs)
-> Maybe (LHsExpr GhcPs, LHsExpr GhcPs -> LHsExpr GhcPs)
forall a. a -> Maybe a
Just (LHsExpr GhcPs
x, \v :: LHsExpr GhcPs
v -> if LHsExpr GhcPs -> Bool
isApp LHsExpr GhcPs
x then LHsExpr GhcPs
v else LHsExpr GhcPs -> LHsExpr GhcPs
paren' LHsExpr GhcPs
v)
f _ = Maybe (LHsExpr GhcPs, LHsExpr GhcPs -> LHsExpr GhcPs)
forall a. Maybe a
Nothing
gen :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
gen :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
gen x :: LHsExpr GhcPs
x = HsExpr GhcPs -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (HsExpr GhcPs -> LHsExpr GhcPs)
-> (LHsExpr GhcPs -> HsExpr GhcPs)
-> LHsExpr GhcPs
-> LHsExpr GhcPs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. XOpApp GhcPs
-> LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p.
XOpApp p -> LHsExpr p -> LHsExpr p -> LHsExpr p -> HsExpr p
OpApp NoExt
XOpApp GhcPs
noExt LHsExpr GhcPs
x (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XVar GhcPs -> Located (IdP GhcPs) -> HsExpr GhcPs
forall p. XVar p -> Located (IdP p) -> HsExpr p
HsVar NoExt
XVar GhcPs
noExt (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc RdrName
SrcSpanLess (Located RdrName)
consDataCon_RDR)))
useCons _ _ = Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
forall a. Maybe a
Nothing
typeListChar :: LHsType GhcPs
typeListChar :: LHsType GhcPs
typeListChar =
SrcSpanLess (LHsType GhcPs) -> LHsType GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsType GhcPs) -> LHsType GhcPs)
-> SrcSpanLess (LHsType GhcPs) -> LHsType GhcPs
forall a b. (a -> b) -> a -> b
$ XListTy GhcPs -> LHsType GhcPs -> HsType GhcPs
forall pass. XListTy pass -> LHsType pass -> HsType pass
HsListTy NoExt
XListTy GhcPs
noExt
(SrcSpanLess (LHsType GhcPs) -> LHsType GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XTyVar GhcPs
-> PromotionFlag -> Located (IdP GhcPs) -> HsType GhcPs
forall pass.
XTyVar pass -> PromotionFlag -> Located (IdP pass) -> HsType pass
HsTyVar NoExt
XTyVar GhcPs
noExt PromotionFlag
NotPromoted (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (FastString -> RdrName
mkVarUnqual ([Char] -> FastString
fsLit "Char")))))
typeString :: LHsType GhcPs
typeString :: LHsType GhcPs
typeString =
SrcSpanLess (LHsType GhcPs) -> LHsType GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsType GhcPs) -> LHsType GhcPs)
-> SrcSpanLess (LHsType GhcPs) -> LHsType GhcPs
forall a b. (a -> b) -> a -> b
$ XTyVar GhcPs
-> PromotionFlag -> Located (IdP GhcPs) -> HsType GhcPs
forall pass.
XTyVar pass -> PromotionFlag -> Located (IdP pass) -> HsType pass
HsTyVar NoExt
XTyVar GhcPs
noExt PromotionFlag
NotPromoted (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (FastString -> RdrName
mkVarUnqual ([Char] -> FastString
fsLit "String")))
stringType :: LHsDecl GhcPs -> [Idea]
stringType :: LHsDecl GhcPs -> [Idea]
stringType (LL _ x :: SrcSpanLess (LHsDecl GhcPs)
x) = case SrcSpanLess (LHsDecl GhcPs)
x of
InstD _ ClsInstD{
cid_inst=
ClsInstDecl{cid_binds=x, cid_tyfam_insts=y, cid_datafam_insts=z}} ->
LHsBinds GhcPs -> [Idea]
forall from. Data from => from -> [Idea]
f LHsBinds GhcPs
x [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++ [LTyFamInstDecl GhcPs] -> [Idea]
forall from. Data from => from -> [Idea]
f [LTyFamInstDecl GhcPs]
y [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++ [LDataFamInstDecl GhcPs] -> [Idea]
forall from. Data from => from -> [Idea]
f [LDataFamInstDecl GhcPs]
z
_ -> HsDecl GhcPs -> [Idea]
forall from. Data from => from -> [Idea]
f SrcSpanLess (LHsDecl GhcPs)
HsDecl GhcPs
x
where
f :: from -> [Idea]
f x :: from
x = (LHsType GhcPs -> [Idea]) -> [LHsType GhcPs] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LHsType GhcPs -> [Idea]
g ([LHsType GhcPs] -> [Idea]) -> [LHsType GhcPs] -> [Idea]
forall a b. (a -> b) -> a -> b
$ from -> [LHsType GhcPs]
forall from to. Biplate from to => from -> [to]
childrenBi from
x
g :: LHsType GhcPs -> [Idea]
g :: LHsType GhcPs -> [Idea]
g e :: LHsType GhcPs
e@(LHsType GhcPs -> LHsType GhcPs
fromTyParen' -> LHsType GhcPs
x) = [[Char]
-> LHsType GhcPs -> LHsType GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
[Char] -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest' "Use String" LHsType GhcPs
x ((LHsType GhcPs -> LHsType GhcPs) -> LHsType GhcPs -> LHsType GhcPs
forall on. Uniplate on => (on -> on) -> on -> on
transform LHsType GhcPs -> LHsType GhcPs
f LHsType GhcPs
x)
[Refactoring SrcSpan]
rs | Bool -> Bool
not (Bool -> Bool)
-> ([Refactoring SrcSpan] -> Bool) -> [Refactoring SrcSpan] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Refactoring SrcSpan] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Refactoring SrcSpan] -> Bool) -> [Refactoring SrcSpan] -> Bool
forall a b. (a -> b) -> a -> b
$ [Refactoring SrcSpan]
rs]
where f :: LHsType GhcPs -> LHsType GhcPs
f x :: LHsType GhcPs
x = if LHsType GhcPs -> LHsType GhcPs -> Bool
forall a. Data a => a -> a -> Bool
astEq LHsType GhcPs
x LHsType GhcPs
typeListChar then LHsType GhcPs
typeString else LHsType GhcPs
x
rs :: [Refactoring SrcSpan]
rs = [RType
-> SrcSpan -> [([Char], SrcSpan)] -> [Char] -> Refactoring SrcSpan
forall a. RType -> a -> [([Char], a)] -> [Char] -> Refactoring a
Replace RType
Type (LHsType GhcPs -> SrcSpan
forall e. HasSrcSpan e => e -> SrcSpan
toSS' LHsType GhcPs
t) [] (LHsType GhcPs -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint LHsType GhcPs
typeString) | LHsType GhcPs
t <- LHsType GhcPs -> [LHsType GhcPs]
forall on. Uniplate on => on -> [on]
universe LHsType GhcPs
x, LHsType GhcPs -> LHsType GhcPs -> Bool
forall a. Data a => a -> a -> Bool
astEq LHsType GhcPs
t LHsType GhcPs
typeListChar]
stringType _ = []