{-# LANGUAGE ViewPatterns, PatternGuards, FlexibleContexts #-}
{-
    Find and match:

<TEST>
yes = 1:2:[] -- [1,2]
yes = ['h','e','l','l','o']
yes (1:2:[]) = 1 -- [1,2]
yes ['h','e'] = 1

-- [a]++b -> a : b, but only if not in a chain of ++'s
yes = [x] ++ xs -- x : xs
no = "x" ++ xs
no = [x] ++ xs ++ ys
no = xs ++ [x] ++ ys
yes = [if a then b else c] ++ xs -- (if a then b else c) : xs
yes = [1] : [2] : [3] : [4] : [5] : [] -- [[1], [2], [3], [4], [5]]
yes = if x == e then l2 ++ xs else [x] ++ check_elem xs -- x : check_elem xs
data Yes = Yes (Maybe [Char]) -- Maybe String
yes = y :: [Char] -> a -- String -> a
instance C [Char]
foo = [a b] ++ xs -- a b : xs
foo = [myexpr | True, a] -- [myexpr | a]
foo = [myexpr | False] -- []
foo = map f [x + 1 | x <- [1..10]] -- [f (x + 1) | x <- [1..10]]
foo = [x + 1 | x <- [1..10], feature] -- [x + 1 | feature, x <- [1..10]]
foo = [x + 1 | x <- [1..10], even x]
foo = [x + 1 | x <- [1..10], even x, dont_reoder_guards]
foo = [x + 1 | x <- [1..10], let y = even x, y]
foo = [x + 1 | x <- [1..10], let q = even 1, q] -- [x + 1 | let q = even 1, q, x <- [1..10]]
foo = [fooValue | Foo{..} <- y, fooField]
issue619 = [pkgJobs | Pkg{pkgGpd, pkgJobs} <- pkgs, not $ null $ C.condTestSuites pkgGpd]
{-# LANGUAGE MonadComprehensions #-}\
foo = [x | False, x <- [1 .. 10]] -- []
foo = [_ | x <- _, let _ = A{x}]
issue1039 = foo (map f [1 | _ <- []]) -- [f 1 | _ <- []]
</TEST>
-}

module Hint.List(listHint) where

import Control.Applicative
import Data.Generics.Uniplate.DataOnly
import Data.List.Extra
import Data.Maybe
import Prelude

import Hint.Type(DeclHint,Idea,suggest,ignore,toRefactSrcSpan,toSS)

import Refact.Types hiding (SrcSpan)
import qualified Refact.Types as R

import GHC.Hs
import SrcLoc
import BasicTypes
import RdrName
import Name
import FastString
import TysWiredIn

import GHC.Util
import Language.Haskell.GhclibParserEx.GHC.Hs.Pat
import Language.Haskell.GhclibParserEx.GHC.Hs.Expr
import Language.Haskell.GhclibParserEx.GHC.Hs.Types
import Language.Haskell.GhclibParserEx.GHC.Hs.ExtendInstances
import Language.Haskell.GhclibParserEx.GHC.Utils.Outputable
import Language.Haskell.GhclibParserEx.GHC.Types.Name.Reader

listHint :: DeclHint
listHint :: DeclHint
listHint Scope
_ ModuleEx
_ = LHsDecl GhcPs -> [Idea]
listDecl

listDecl :: LHsDecl GhcPs -> [Idea]
listDecl :: LHsDecl GhcPs -> [Idea]
listDecl 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]
++
  (Located (Pat GhcPs) -> [Idea]) -> [Located (Pat GhcPs)] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LPat GhcPs -> [Idea]
Located (Pat GhcPs) -> [Idea]
listPat (LHsDecl GhcPs -> [Located (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)

-- Refer to https://github.com/ndmitchell/hlint/issues/775 for the
-- structure of 'listComp'.

listComp :: LHsExpr GhcPs -> [Idea]
listComp :: LHsExpr GhcPs -> [Idea]
listComp o :: LHsExpr GhcPs
o@(L SrcSpan
_ (HsDo XDo GhcPs
_ HsStmtContext Name
ListComp (L SrcSpan
_ [ExprLStmt GhcPs]
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@(L SrcSpan
_ (HsDo XDo GhcPs
_ HsStmtContext Name
MonadComp (L SrcSpan
_ [ExprLStmt GhcPs]
stmts))) =
  LHsExpr GhcPs -> HsStmtContext Name -> [ExprLStmt GhcPs] -> [Idea]
listCompCheckGuards LHsExpr GhcPs
o HsStmtContext Name
forall id. HsStmtContext id
MonadComp [ExprLStmt GhcPs]
stmts

listComp (L SrcSpan
_ HsPar{}) = [] -- App2 "sees through" paren, which causes duplicate hints with universeBi
listComp o :: LHsExpr GhcPs
o@(LHsExpr GhcPs -> App2
forall a b. View a b => a -> b
view -> App2 LHsExpr GhcPs
mp LHsExpr GhcPs
f (L SrcSpan
_ (HsDo XDo GhcPs
_ HsStmtContext Name
ListComp (L SrcSpan
_ [ExprLStmt GhcPs]
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 LHsExpr GhcPs
mp LHsExpr GhcPs
f (L SrcSpan
_ (HsDo XDo GhcPs
_ HsStmtContext Name
MonadComp (L SrcSpan
_ [ExprLStmt GhcPs]
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 LHsExpr GhcPs
_ = []

listCompCheckGuards :: LHsExpr GhcPs -> HsStmtContext Name -> [ExprLStmt GhcPs] -> [Idea]
listCompCheckGuards :: LHsExpr GhcPs -> HsStmtContext Name -> [ExprLStmt GhcPs] -> [Idea]
listCompCheckGuards LHsExpr GhcPs
o HsStmtContext Name
ctx [ExprLStmt GhcPs]
stmts =
  let revs :: [ExprLStmt GhcPs]
revs = [ExprLStmt GhcPs] -> [ExprLStmt GhcPs]
forall a. [a] -> [a]
reverse [ExprLStmt GhcPs]
stmts
      e :: ExprLStmt GhcPs
e@(L SrcSpan
_ LastStmt{}) = [ExprLStmt GhcPs] -> ExprLStmt GhcPs
forall a. [a] -> a
head [ExprLStmt GhcPs]
revs -- In a ListComp, this is always last.
      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 ExprLStmt GhcPs
e [ExprLStmt GhcPs]
xs
      | [Char]
"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 [Char]
"Short-circuited list comprehension" LHsExpr GhcPs
o LHsExpr GhcPs
o' (LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan]
suggestExpr LHsExpr GhcPs
o LHsExpr GhcPs
o')]
      | [Char]
"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 [Char]
"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 [Char]
"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 NoExtField
XExplicitList GhcPs
noExtField 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
-> GenLocated SrcSpan [ExprLStmt GhcPs]
-> HsExpr GhcPs
forall p.
XDo p -> HsStmtContext Name -> Located [ExprLStmt p] -> HsExpr p
HsDo NoExtField
XDo GhcPs
noExtField HsStmtContext Name
ctx (SrcSpanLess (GenLocated SrcSpan [ExprLStmt GhcPs])
-> GenLocated SrcSpan [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 [Char]
"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
-> GenLocated SrcSpan [ExprLStmt GhcPs]
-> HsExpr GhcPs
forall p.
XDo p -> HsStmtContext Name -> Located [ExprLStmt p] -> HsExpr p
HsDo NoExtField
XDo GhcPs
noExtField HsStmtContext Name
ctx (SrcSpanLess (GenLocated SrcSpan [ExprLStmt GhcPs])
-> GenLocated SrcSpan [ExprLStmt GhcPs]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (GenLocated SrcSpan [ExprLStmt GhcPs])
 -> GenLocated SrcSpan [ExprLStmt GhcPs])
-> SrcSpanLess (GenLocated SrcSpan [ExprLStmt GhcPs])
-> GenLocated SrcSpan [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 SrcSpan
_ (BodyStmt XBodyStmt GhcPs GhcPs (LHsExpr GhcPs)
_ (L SrcSpan
_ (HsVar XVar GhcPs
_ (L SrcSpan
_ IdP GhcPs
x))) SyntaxExpr GhcPs
_ SyntaxExpr GhcPs
_)) = [Char] -> Maybe [Char]
forall a. a -> Maybe a
Just (RdrName -> [Char]
occNameStr IdP GhcPs
RdrName
x)
        qualCon ExprLStmt GhcPs
_ = 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 LHsExpr GhcPs
o LHsExpr GhcPs
mp LHsExpr GhcPs
f HsStmtContext Name
ctx [ExprLStmt GhcPs]
stmts  | LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
mp [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"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 [Char]
"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
      L SrcSpan
_ (LastStmt XLastStmt GhcPs GhcPs (LHsExpr GhcPs)
_ LHsExpr GhcPs
body Bool
b SyntaxExpr GhcPs
s) = [ExprLStmt GhcPs] -> ExprLStmt GhcPs
forall a. [a] -> a
head [ExprLStmt GhcPs]
revs -- In a ListComp, this is always last.
      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 NoExtField
XLastStmt GhcPs GhcPs (LHsExpr GhcPs)
noExtField (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 NoExtField
XApp GhcPs
noExtField (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
-> GenLocated SrcSpan [ExprLStmt GhcPs]
-> HsExpr GhcPs
forall p.
XDo p -> HsStmtContext Name -> Located [ExprLStmt p] -> HsExpr p
HsDo NoExtField
XDo GhcPs
noExtField HsStmtContext Name
ctx (SrcSpanLess (GenLocated SrcSpan [ExprLStmt GhcPs])
-> GenLocated SrcSpan [ExprLStmt GhcPs]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (GenLocated SrcSpan [ExprLStmt GhcPs])
 -> GenLocated SrcSpan [ExprLStmt GhcPs])
-> SrcSpanLess (GenLocated SrcSpan [ExprLStmt GhcPs])
-> GenLocated SrcSpan [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 LHsExpr GhcPs
_ LHsExpr GhcPs
_ LHsExpr GhcPs
_ HsStmtContext Name
_ [ExprLStmt GhcPs]
_ = []

suggestExpr :: LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring R.SrcSpan]
suggestExpr :: LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan]
suggestExpr LHsExpr GhcPs
o 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 a. HasSrcSpan a => a -> 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 l idL idR body.
(Data l, Data (StmtLR idL idR body),
 AllVars (GenLocated l (StmtLR idL idR body)),
 AllVars (XRec idL Pat)) =>
[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 [GenLocated l (StmtLR idL idR body)]
guards (x :: GenLocated l (StmtLR idL idR body)
x@(L l
_ (BindStmt XBindStmt idL idR body
_ XRec idL Pat
p body
_ SyntaxExpr idR
_ SyntaxExpr idR
_)) : [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 ([GenLocated l (StmtLR idL idR body)]
move, [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 (Located (Pat GhcPs)) -> Bool)
-> [HsRecFields GhcPs (Located (Pat GhcPs))] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any HsRecFields GhcPs (LPat GhcPs) -> Bool
HsRecFields GhcPs (Located (Pat GhcPs)) -> Bool
hasPFieldsDotDot (GenLocated l (StmtLR idL idR body)
-> [HsRecFields GhcPs (Located (Pat GhcPs))]
forall from to. Biplate from to => from -> [to]
universeBi GenLocated l (StmtLR idL idR body)
x)
                       Bool -> Bool -> Bool
|| (LHsRecField GhcPs (Located (Pat GhcPs)) -> Bool)
-> [LHsRecField GhcPs (Located (Pat GhcPs))] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any LHsRecField GhcPs (LPat GhcPs) -> Bool
LHsRecField GhcPs (Located (Pat GhcPs)) -> Bool
isPFieldWildcard (GenLocated l (StmtLR idL idR body)
-> [LHsRecField GhcPs (Located (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 \GenLocated l (StmtLR idL idR body)
x ->
                       let pvs :: [[Char]]
pvs = XRec idL Pat -> [[Char]]
forall a. AllVars a => a -> [[Char]]
pvars XRec idL Pat
p in
                       -- See this code from 'RdrHsSyn.hs' (8.10.1):
                       --   plus_RDR, pun_RDR :: RdrName
                       --   plus_RDR = mkUnqual varName (fsLit "+") -- Hack
                       --   pun_RDR  = mkUnqual varName (fsLit "pun-right-hand-side")
                       -- Todo (SF, 2020-03-28): Try to make this better somehow.
                       [[Char]]
pvs [[Char]] -> [[Char]] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`disjoint` GenLocated l (StmtLR idL idR body) -> [[Char]]
forall a. AllVars a => a -> [[Char]]
varss GenLocated l (StmtLR idL idR body)
x Bool -> Bool -> Bool
&& [Char]
"pun-right-hand-side" [Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [[Char]]
pvs
                   ) [GenLocated l (StmtLR idL idR body)]
guards
    f [GenLocated l (StmtLR idL idR body)]
guards (x :: GenLocated l (StmtLR idL idR body)
x@(L l
_ BodyStmt{}):[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 [GenLocated l (StmtLR idL idR body)]
guards (x :: GenLocated l (StmtLR idL idR body)
x@(L l
_ LetStmt{}):[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 [GenLocated l (StmtLR idL idR body)]
guards [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

listExp :: Bool -> LHsExpr GhcPs -> [Idea]
listExp :: Bool -> LHsExpr GhcPs -> [Idea]
listExp 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]
          | ([Char]
name, Bool
-> LHsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char])
f) <- [([Char],
  Bool
  -> LHsExpr GhcPs
  -> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))]
checks
          , Just (LHsExpr GhcPs
x2, [([Char], SrcSpan)]
subts, [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 a. HasSrcSpan a => a -> SrcSpan
toSS LHsExpr GhcPs
x) [([Char], SrcSpan)]
subts [Char]
temp ]

listPat :: LPat GhcPs -> [Idea]
listPat :: LPat GhcPs -> [Idea]
listPat LPat GhcPs
x = if [Idea] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Idea]
res then (Located (Pat GhcPs) -> [Idea]) -> [Located (Pat GhcPs)] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LPat GhcPs -> [Idea]
Located (Pat GhcPs) -> [Idea]
listPat ([Located (Pat GhcPs)] -> [Idea])
-> [Located (Pat GhcPs)] -> [Idea]
forall a b. (a -> b) -> a -> b
$ Located (Pat GhcPs) -> [Located (Pat GhcPs)]
forall on. Uniplate on => on -> [on]
children LPat GhcPs
Located (Pat GhcPs)
x else [[Idea] -> Idea
forall a. [a] -> a
head [Idea]
res]
    where res :: [Idea]
res = [[Char]
-> Located (Pat GhcPs)
-> Located (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 LPat GhcPs
Located (Pat GhcPs)
x Located (Pat GhcPs)
x2 [Refactoring SrcSpan
r]
                  | ([Char]
name, Located (Pat GhcPs)
-> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char])
f) <- [([Char],
  LPat GhcPs -> Maybe (LPat GhcPs, [([Char], SrcSpan)], [Char]))]
[([Char],
  Located (Pat GhcPs)
  -> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))]
pchecks
                  , Just (Located (Pat GhcPs)
x2, [([Char], SrcSpan)]
subts, [Char]
temp) <- [Located (Pat GhcPs)
-> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char])
f LPat GhcPs
Located (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 (Located (Pat GhcPs) -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LPat GhcPs
Located (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 LHsExpr GhcPs
op LHsExpr GhcPs
_ LHsExpr GhcPs
_) = LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
op [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"++"
isAppend a
_ = 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 [([Char],
  Bool
  -> LHsExpr GhcPs
  -> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))]
-> [([Char],
     Bool
     -> LHsExpr GhcPs
     -> Maybe (LHsExpr GhcPs, [([Char], SrcSpan)], [Char]))]
forall a. [a] -> [a]
drop1 -- see #174
  [ [Char]
"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
  , [Char]
"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
  , [Char]
"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, LPat GhcPs -> Maybe (LPat GhcPs, [(String, R.SrcSpan)], String))]
pchecks :: [([Char],
  LPat GhcPs -> Maybe (LPat GhcPs, [([Char], SrcSpan)], [Char]))]
pchecks = let * :: a -> b -> (a, b)
(*) = (,) in [([Char],
  Located (Pat GhcPs)
  -> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))]
-> [([Char],
     Located (Pat GhcPs)
     -> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))]
forall a. [a] -> [a]
drop1 -- see #174
    [ [Char]
"Use string literal pattern" [Char]
-> (Located (Pat GhcPs)
    -> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))
-> ([Char],
    Located (Pat GhcPs)
    -> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))
forall a b. a -> b -> (a, b)
* Located (Pat GhcPs)
-> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char])
forall a. LPat GhcPs -> Maybe (LPat GhcPs, [a], [Char])
usePString
    , [Char]
"Use list literal pattern" [Char]
-> (Located (Pat GhcPs)
    -> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))
-> ([Char],
    Located (Pat GhcPs)
    -> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))
forall a b. a -> b -> (a, b)
* LPat GhcPs -> Maybe (LPat GhcPs, [([Char], SrcSpan)], [Char])
Located (Pat GhcPs)
-> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char])
usePList
    ]

usePString :: LPat GhcPs -> Maybe (LPat GhcPs, [a], String)
usePString :: LPat GhcPs -> Maybe (LPat GhcPs, [a], [Char])
usePString (L _ (ListPat _ xs)) | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [Located (Pat GhcPs)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LPat GhcPs]
[Located (Pat GhcPs)]
xs, Just [Char]
s <- (Located (Pat GhcPs) -> Maybe Char)
-> [Located (Pat GhcPs)] -> Maybe [Char]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LPat GhcPs -> Maybe Char
Located (Pat GhcPs) -> Maybe Char
fromPChar [LPat GhcPs]
[Located (Pat GhcPs)]
xs =
  let literal :: Located (Pat GhcPs)
literal = SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs))
-> SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XLitPat GhcPs -> HsLit GhcPs -> Pat GhcPs
forall p. XLitPat p -> HsLit p -> Pat p
LitPat NoExtField
XLitPat GhcPs
noExtField (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 (Located (Pat GhcPs), [a], [Char])
-> Maybe (Located (Pat GhcPs), [a], [Char])
forall a. a -> Maybe a
Just (Located (Pat GhcPs)
literal, [], Located (Pat GhcPs) -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint Located (Pat GhcPs)
literal)
usePString LPat GhcPs
_ = Maybe (LPat GhcPs, [a], [Char])
forall a. Maybe a
Nothing

usePList :: LPat GhcPs -> Maybe (LPat GhcPs, [(String, R.SrcSpan)], String)
usePList :: LPat GhcPs -> Maybe (LPat GhcPs, [([Char], SrcSpan)], [Char])
usePList =
  ([(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
 -> (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))
-> Maybe
     [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
-> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap  ( (\([Located (Pat GhcPs)]
e, [(([Char], SrcSpan), Located (Pat GhcPs))]
s) ->
             (SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XListPat GhcPs -> [LPat GhcPs] -> Pat GhcPs
forall p. XListPat p -> [LPat p] -> Pat p
ListPat NoExtField
XListPat GhcPs
noExtField [LPat GhcPs]
[Located (Pat GhcPs)]
e)
             , ((([Char], SrcSpan), Located (Pat GhcPs)) -> ([Char], SrcSpan))
-> [(([Char], SrcSpan), Located (Pat GhcPs))]
-> [([Char], SrcSpan)]
forall a b. (a -> b) -> [a] -> [b]
map ((SrcSpan -> SrcSpan) -> ([Char], SrcSpan) -> ([Char], SrcSpan)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SrcSpan -> SrcSpan
toRefactSrcSpan (([Char], SrcSpan) -> ([Char], SrcSpan))
-> ((([Char], SrcSpan), Located (Pat GhcPs)) -> ([Char], SrcSpan))
-> (([Char], SrcSpan), Located (Pat GhcPs))
-> ([Char], SrcSpan)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Char], SrcSpan), Located (Pat GhcPs)) -> ([Char], SrcSpan)
forall a b. (a, b) -> a
fst) [(([Char], SrcSpan), Located (Pat GhcPs))]
s
             , Located (Pat GhcPs) -> [Char]
forall a. Outputable a => a -> [Char]
unsafePrettyPrint (SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs))
-> SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XListPat GhcPs -> [LPat GhcPs] -> Pat GhcPs
forall p. XListPat p -> [LPat p] -> Pat p
ListPat NoExtField
XListPat GhcPs
noExtField (((([Char], SrcSpan), Located (Pat GhcPs)) -> Located (Pat GhcPs))
-> [(([Char], SrcSpan), Located (Pat GhcPs))]
-> [Located (Pat GhcPs)]
forall a b. (a -> b) -> [a] -> [b]
map (([Char], SrcSpan), Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a b. (a, b) -> b
snd [(([Char], SrcSpan), Located (Pat GhcPs))]
s) :: LPat GhcPs))
          )
          (([Located (Pat GhcPs)],
  [(([Char], SrcSpan), Located (Pat GhcPs))])
 -> (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))
-> ([(Located (Pat GhcPs),
      (([Char], SrcSpan), Located (Pat GhcPs)))]
    -> ([Located (Pat GhcPs)],
        [(([Char], SrcSpan), Located (Pat GhcPs))]))
-> [(Located (Pat GhcPs),
     (([Char], SrcSpan), Located (Pat GhcPs)))]
-> (Located (Pat GhcPs), [([Char], SrcSpan)], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
-> ([Located (Pat GhcPs)],
    [(([Char], SrcSpan), Located (Pat GhcPs))])
forall a b. [(a, b)] -> ([a], [b])
unzip
        )
  (Maybe
   [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
 -> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char]))
-> (Located (Pat GhcPs)
    -> Maybe
         [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))])
-> Located (Pat GhcPs)
-> Maybe (Located (Pat GhcPs), [([Char], SrcSpan)], [Char])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool
-> [Char]
-> Located (Pat GhcPs)
-> Maybe
     [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
f Bool
True [Char
'a'..Char
'z']
  where
    f :: Bool
-> [Char]
-> Located (Pat GhcPs)
-> Maybe
     [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
f Bool
first [Char]
_ Located (Pat GhcPs)
x | LPat GhcPs -> [Char]
patToStr LPat GhcPs
Located (Pat GhcPs)
x [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"[]" = if Bool
first then Maybe
  [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
forall a. Maybe a
Nothing else [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
-> Maybe
     [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
forall a. a -> Maybe a
Just []
    f Bool
first (Char
ident:[Char]
cs) (Located (Pat GhcPs) -> PApp_
forall a b. View a b => a -> b
view -> PApp_ [Char]
":" [LPat GhcPs
a, LPat GhcPs
b]) = ((LPat GhcPs
Located (Pat GhcPs)
a, Char -> LPat GhcPs -> (([Char], SrcSpan), LPat GhcPs)
g Char
ident LPat GhcPs
a) (Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))
-> [(Located (Pat GhcPs),
     (([Char], SrcSpan), Located (Pat GhcPs)))]
-> [(Located (Pat GhcPs),
     (([Char], SrcSpan), Located (Pat GhcPs)))]
forall a. a -> [a] -> [a]
:) ([(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
 -> [(Located (Pat GhcPs),
      (([Char], SrcSpan), Located (Pat GhcPs)))])
-> Maybe
     [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
-> Maybe
     [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool
-> [Char]
-> Located (Pat GhcPs)
-> Maybe
     [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
f Bool
False [Char]
cs LPat GhcPs
Located (Pat GhcPs)
b
    f Bool
first [Char]
_ Located (Pat GhcPs)
_ = Maybe
  [(Located (Pat GhcPs), (([Char], SrcSpan), Located (Pat GhcPs)))]
forall a. Maybe a
Nothing

    g :: Char -> LPat GhcPs -> ((String, SrcSpan), LPat GhcPs)
    g :: Char -> LPat GhcPs -> (([Char], SrcSpan), LPat GhcPs)
g Char
c (LPat GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
getLoc -> SrcSpan
loc) = (([Char
c], SrcSpan
loc), SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs))
-> SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XVarPat GhcPs -> GenLocated SrcSpan (IdP GhcPs) -> Pat GhcPs
forall p. XVarPat p -> Located (IdP p) -> Pat p
VarPat NoExtField
XVarPat GhcPs
noExtField (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located RdrName) -> Located RdrName)
-> SrcSpanLess (Located RdrName) -> Located RdrName
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 p
b (L SrcSpan
_ (ExplicitList XExplicitList GhcPs
_ Maybe (SyntaxExpr GhcPs)
_ [LHsExpr GhcPs]
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 [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 NoExtField
XLitE GhcPs
noExtField (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 p
_ LHsExpr GhcPs
_ = 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 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  ( (\([LHsExpr GhcPs]
e, [([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 NoExtField
XExplicitList GhcPs
noExtField 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 a. HasSrcSpan a => a -> 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 NoExtField
XExplicitList GhcPs
noExtField 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 [Char
'a'..Char
'z']
  where
    f :: Bool
-> [Char]
-> LHsExpr GhcPs
-> Maybe [(LHsExpr GhcPs, ([Char], LHsExpr GhcPs))]
f Bool
first [Char]
_ LHsExpr GhcPs
x | LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
x [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"[]" = 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 Bool
first (Char
ident:[Char]
cs) (LHsExpr GhcPs -> App2
forall a b. View a b => a -> b
view -> App2 LHsExpr GhcPs
c LHsExpr GhcPs
a LHsExpr GhcPs
b) | LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
c [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
":" =
          ((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 Bool
first [Char]
_ LHsExpr GhcPs
_ = 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 Char
c LHsExpr GhcPs
p = ([Char
c], SrcSpan -> HsExpr GhcPs -> LHsExpr GhcPs
forall l e. l -> e -> GenLocated l e
L (LHsExpr GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
getLoc LHsExpr GhcPs
p) (LHsExpr GhcPs -> SrcSpanLess (LHsExpr GhcPs)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc (LHsExpr GhcPs -> SrcSpanLess (LHsExpr GhcPs))
-> LHsExpr GhcPs -> SrcSpanLess (LHsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ [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 Bool
False (a -> App2
forall a b. View a b => a -> b
view -> App2 LHsExpr GhcPs
op LHsExpr GhcPs
x LHsExpr GhcPs
y) | LHsExpr GhcPs -> [Char]
varToStr LHsExpr GhcPs
op [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"++"
                                    , Just (LHsExpr GhcPs
newX, LHsExpr GhcPs
tplX, SrcSpan
spanX) <- LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, LHsExpr GhcPs, SrcSpan)
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
newX LHsExpr GhcPs
y
         , [([Char]
"x", SrcSpan
spanX), ([Char]
"xs", LHsExpr GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> 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
tplX ([Char] -> LHsExpr GhcPs
strToVar [Char]
"xs")
         )
  where
    f :: LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, LHsExpr GhcPs, R.SrcSpan)
    f :: LHsExpr GhcPs -> Maybe (LHsExpr GhcPs, LHsExpr GhcPs, SrcSpan)
f (L SrcSpan
_ (ExplicitList XExplicitList GhcPs
_ Maybe (SyntaxExpr GhcPs)
_ [LHsExpr GhcPs
x]))
      | LHsExpr GhcPs -> Bool
forall a. Brackets a => a -> Bool
isAtom LHsExpr GhcPs
x Bool -> Bool -> Bool
|| LHsExpr GhcPs -> Bool
isApp LHsExpr GhcPs
x = (LHsExpr GhcPs, LHsExpr GhcPs, SrcSpan)
-> Maybe (LHsExpr GhcPs, LHsExpr GhcPs, SrcSpan)
forall a. a -> Maybe a
Just (LHsExpr GhcPs
x, [Char] -> LHsExpr GhcPs
strToVar [Char]
"x", LHsExpr GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LHsExpr GhcPs
x)
      | Bool
otherwise = (LHsExpr GhcPs, LHsExpr GhcPs, SrcSpan)
-> Maybe (LHsExpr GhcPs, LHsExpr GhcPs, SrcSpan)
forall a. a -> Maybe a
Just (LHsExpr GhcPs -> LHsExpr GhcPs
forall a. Brackets a => a -> a
addParen LHsExpr GhcPs
x, LHsExpr GhcPs -> LHsExpr GhcPs
forall a. Brackets a => a -> a
addParen ([Char] -> LHsExpr GhcPs
strToVar [Char]
"x"), LHsExpr GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LHsExpr GhcPs
x)
    f LHsExpr GhcPs
_ = Maybe (LHsExpr GhcPs, LHsExpr GhcPs, SrcSpan)
forall a. Maybe a
Nothing

    gen :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
    gen :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
gen 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 NoExtField
XOpApp GhcPs
noExtField LHsExpr GhcPs
x (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XVar GhcPs -> GenLocated SrcSpan (IdP GhcPs) -> HsExpr GhcPs
forall p. XVar p -> Located (IdP p) -> HsExpr p
HsVar NoExtField
XVar GhcPs
noExtField  (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc RdrName
SrcSpanLess (Located RdrName)
consDataCon_RDR)))
useCons Bool
_ a
_ = 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 NoExtField
XListTy GhcPs
noExtField
    (SrcSpanLess (LHsType GhcPs) -> LHsType GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XTyVar GhcPs
-> PromotionFlag -> GenLocated SrcSpan (IdP GhcPs) -> HsType GhcPs
forall pass.
XTyVar pass -> PromotionFlag -> Located (IdP pass) -> HsType pass
HsTyVar NoExtField
XTyVar GhcPs
noExtField PromotionFlag
NotPromoted (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (FastString -> RdrName
mkVarUnqual ([Char] -> FastString
fsLit [Char]
"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 -> GenLocated SrcSpan (IdP GhcPs) -> HsType GhcPs
forall pass.
XTyVar pass -> PromotionFlag -> Located (IdP pass) -> HsType pass
HsTyVar NoExtField
XTyVar GhcPs
noExtField PromotionFlag
NotPromoted (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (FastString -> RdrName
mkVarUnqual ([Char] -> FastString
fsLit [Char]
"String")))

stringType :: LHsDecl GhcPs  -> [Idea]
stringType :: LHsDecl GhcPs -> [Idea]
stringType (L SrcSpan
_ HsDecl GhcPs
x) = case HsDecl GhcPs
x of
  InstD XInstD GhcPs
_ ClsInstD{
    cid_inst :: forall pass. InstDecl pass -> ClsInstDecl pass
cid_inst=
        ClsInstDecl{cid_binds :: forall pass. ClsInstDecl pass -> LHsBinds pass
cid_binds=LHsBinds GhcPs
x, cid_tyfam_insts :: forall pass. ClsInstDecl pass -> [LTyFamInstDecl pass]
cid_tyfam_insts=[LTyFamInstDecl GhcPs]
y, cid_datafam_insts :: forall pass. ClsInstDecl pass -> [LDataFamInstDecl pass]
cid_datafam_insts=[LDataFamInstDecl GhcPs]
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 -- Pretty much everthing but the instance type.
  HsDecl GhcPs
_ -> HsDecl GhcPs -> [Idea]
forall from. Data from => from -> [Idea]
f HsDecl GhcPs
x
  where
    f :: from -> [Idea]
f 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.
(HasSrcSpan a, Outputable a) =>
[Char] -> a -> a -> [Refactoring SrcSpan] -> Idea
ignore [Char]
"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 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 a. HasSrcSpan a => a -> 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]