2014-07-01 15:43:13 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2014-06-27 16:38:15 +00:00
|
|
|
module Language.Haskell.GhcMod.CaseSplit (
|
2014-07-15 03:41:10 +00:00
|
|
|
splits
|
2014-06-21 09:38:44 +00:00
|
|
|
) where
|
|
|
|
|
2014-08-01 15:08:23 +00:00
|
|
|
import Data.List (find, intercalate)
|
2014-07-24 18:21:05 +00:00
|
|
|
import Data.Maybe (isJust)
|
2015-08-31 05:33:36 +00:00
|
|
|
import Data.Functor
|
2014-06-25 16:09:24 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.IO as T (readFile)
|
2015-03-03 20:12:43 +00:00
|
|
|
import System.FilePath
|
2015-08-31 05:33:36 +00:00
|
|
|
import Prelude
|
2015-03-03 20:12:43 +00:00
|
|
|
|
2014-07-15 03:41:10 +00:00
|
|
|
import qualified DataCon as Ty
|
2014-07-24 18:21:05 +00:00
|
|
|
import GHC (GhcMonad, LPat, Id, ParsedModule(..), TypecheckedModule(..), DynFlags, SrcSpan, Type, GenLocated(L))
|
2014-06-21 09:38:44 +00:00
|
|
|
import qualified GHC as G
|
2015-03-03 20:12:43 +00:00
|
|
|
import Outputable (PprStyle)
|
|
|
|
import qualified TyCon as Ty
|
|
|
|
import qualified Type as Ty
|
|
|
|
import Exception
|
|
|
|
|
2014-07-15 03:41:10 +00:00
|
|
|
import Language.Haskell.GhcMod.Convert
|
2015-03-03 20:12:43 +00:00
|
|
|
import Language.Haskell.GhcMod.DynFlags
|
2014-06-21 09:38:44 +00:00
|
|
|
import qualified Language.Haskell.GhcMod.Gap as Gap
|
2014-06-28 19:43:51 +00:00
|
|
|
import Language.Haskell.GhcMod.Monad
|
2014-06-21 09:38:44 +00:00
|
|
|
import Language.Haskell.GhcMod.SrcUtils
|
2015-03-03 20:12:43 +00:00
|
|
|
import Language.Haskell.GhcMod.Doc
|
|
|
|
import Language.Haskell.GhcMod.Logging
|
|
|
|
import Language.Haskell.GhcMod.Types
|
2015-07-03 19:31:52 +00:00
|
|
|
import Language.Haskell.GhcMod.FileMapping (fileModSummaryWithMapping)
|
2014-06-22 16:03:34 +00:00
|
|
|
|
2014-06-27 16:38:15 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
-- CASE SPLITTING
|
2014-06-21 09:38:44 +00:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
2014-07-24 18:21:05 +00:00
|
|
|
data SplitInfo = SplitInfo G.Name SrcSpan (SrcSpan, Type) [SrcSpan]
|
|
|
|
| TySplitInfo G.Name SrcSpan (SrcSpan, Ty.Kind)
|
2014-06-25 16:09:24 +00:00
|
|
|
data SplitToTextInfo = SplitToTextInfo { sVarName :: String
|
|
|
|
, sBindingSpan :: SrcSpan
|
|
|
|
, sVarSpan :: SrcSpan
|
|
|
|
, sTycons :: [String]
|
|
|
|
}
|
2014-06-21 09:38:44 +00:00
|
|
|
|
|
|
|
-- | Splitting a variable in a equation.
|
2014-07-12 09:16:16 +00:00
|
|
|
splits :: IOish m
|
|
|
|
=> FilePath -- ^ A target file.
|
2014-06-21 09:38:44 +00:00
|
|
|
-> Int -- ^ Line number.
|
|
|
|
-> Int -- ^ Column number.
|
2014-07-12 09:16:16 +00:00
|
|
|
-> GhcModT m String
|
2015-03-03 20:12:43 +00:00
|
|
|
splits file lineNo colNo =
|
2015-08-14 16:18:26 +00:00
|
|
|
ghandle handler $ runGmlT' [Left file] deferErrors $ do
|
2015-08-31 05:33:36 +00:00
|
|
|
oopts <- outputOpts <$> options
|
2015-03-03 20:12:43 +00:00
|
|
|
crdl <- cradle
|
|
|
|
style <- getStyle
|
|
|
|
dflag <- G.getSessionDynFlags
|
2015-07-03 19:31:52 +00:00
|
|
|
modSum <- fileModSummaryWithMapping (cradleCurrentDir crdl </> file)
|
2015-08-31 05:33:36 +00:00
|
|
|
whenFound' oopts (getSrcSpanTypeForSplit modSum lineNo colNo) $ \x -> case x of
|
2015-03-03 20:12:43 +00:00
|
|
|
(SplitInfo varName bndLoc (varLoc,varT) _matches) -> do
|
|
|
|
let varName' = showName dflag style varName -- Convert name to string
|
|
|
|
t <- genCaseSplitTextFile file (SplitToTextInfo varName' bndLoc varLoc $
|
|
|
|
getTyCons dflag style varName varT)
|
|
|
|
return (fourInts bndLoc, t)
|
|
|
|
(TySplitInfo varName bndLoc (varLoc,varT)) -> do
|
|
|
|
let varName' = showName dflag style varName -- Convert name to string
|
|
|
|
t <- genCaseSplitTextFile file (SplitToTextInfo varName' bndLoc varLoc $
|
|
|
|
getTyCons dflag style varName varT)
|
|
|
|
return (fourInts bndLoc, t)
|
|
|
|
where
|
|
|
|
handler (SomeException ex) = do
|
2015-08-17 02:58:33 +00:00
|
|
|
gmLog GmException "splits" $
|
2015-03-03 20:12:43 +00:00
|
|
|
text "" $$ nest 4 (showDoc ex)
|
2015-08-31 05:33:36 +00:00
|
|
|
emptyResult =<< outputOpts <$> options
|
2014-06-27 17:06:20 +00:00
|
|
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
-- a. Code for getting the information of the variable
|
2014-06-21 09:38:44 +00:00
|
|
|
|
2014-07-27 10:20:46 +00:00
|
|
|
getSrcSpanTypeForSplit :: GhcMonad m => G.ModSummary -> Int -> Int -> m (Maybe SplitInfo)
|
|
|
|
getSrcSpanTypeForSplit modSum lineNo colNo = do
|
2014-07-24 18:21:05 +00:00
|
|
|
fn <- getSrcSpanTypeForFnSplit modSum lineNo colNo
|
|
|
|
if isJust fn
|
|
|
|
then return fn
|
2014-07-27 10:20:46 +00:00
|
|
|
else getSrcSpanTypeForTypeSplit modSum lineNo colNo
|
2014-07-24 18:21:05 +00:00
|
|
|
|
|
|
|
-- Information for a function case split
|
|
|
|
getSrcSpanTypeForFnSplit :: GhcMonad m => G.ModSummary -> Int -> Int -> m (Maybe SplitInfo)
|
|
|
|
getSrcSpanTypeForFnSplit modSum lineNo colNo = do
|
2014-08-23 12:06:26 +00:00
|
|
|
p@ParsedModule{pm_parsed_source = _pms} <- G.parseModule modSum
|
2014-06-21 09:38:44 +00:00
|
|
|
tcm@TypecheckedModule{tm_typechecked_source = tcs} <- G.typecheckModule p
|
2014-11-01 13:06:34 +00:00
|
|
|
let varPat = find isPatternVar $ listifySpans tcs (lineNo, colNo) :: Maybe (LPat Id)
|
|
|
|
match = last $ listifySpans tcs (lineNo, colNo) :: Gap.GLMatchI
|
2014-06-21 09:38:44 +00:00
|
|
|
case varPat of
|
|
|
|
Nothing -> return Nothing
|
|
|
|
Just varPat' -> do
|
2014-07-15 03:35:45 +00:00
|
|
|
varT <- Gap.getType tcm varPat' -- Finally we get the type of the var
|
2014-07-24 18:21:05 +00:00
|
|
|
case varT of
|
|
|
|
Just varT' ->
|
2015-03-28 18:54:10 +00:00
|
|
|
#if __GLASGOW_HASKELL__ >= 710
|
2015-01-29 08:43:31 +00:00
|
|
|
let (L matchL (G.Match _ _ _ (G.GRHSs rhsLs _))) = match
|
2015-03-28 18:54:10 +00:00
|
|
|
#else
|
|
|
|
let (L matchL (G.Match _ _ (G.GRHSs rhsLs _))) = match
|
|
|
|
#endif
|
2014-07-24 18:21:05 +00:00
|
|
|
in return $ Just (SplitInfo (getPatternVarName varPat') matchL varT' (map G.getLoc rhsLs) )
|
2014-06-21 09:38:44 +00:00
|
|
|
_ -> return Nothing
|
|
|
|
|
|
|
|
isPatternVar :: LPat Id -> Bool
|
|
|
|
isPatternVar (L _ (G.VarPat _)) = True
|
|
|
|
isPatternVar _ = False
|
|
|
|
|
|
|
|
getPatternVarName :: LPat Id -> G.Name
|
|
|
|
getPatternVarName (L _ (G.VarPat vName)) = G.getName vName
|
2014-07-24 18:21:05 +00:00
|
|
|
getPatternVarName _ = error "This should never happened"
|
|
|
|
|
2014-07-27 10:20:46 +00:00
|
|
|
-- TODO: Information for a type family case split
|
|
|
|
getSrcSpanTypeForTypeSplit :: GhcMonad m => G.ModSummary -> Int -> Int -> m (Maybe SplitInfo)
|
|
|
|
getSrcSpanTypeForTypeSplit _modSum _lineNo _colNo = return Nothing
|
2014-07-24 18:21:05 +00:00
|
|
|
|
2014-06-25 16:09:24 +00:00
|
|
|
----------------------------------------------------------------
|
2014-06-27 17:06:20 +00:00
|
|
|
-- b. Code for getting the possible constructors
|
2014-06-25 16:09:24 +00:00
|
|
|
|
2014-06-21 09:38:44 +00:00
|
|
|
getTyCons :: DynFlags -> PprStyle -> G.Name -> G.Type -> [String]
|
|
|
|
getTyCons dflag style name ty | Just (tyCon, _) <- Ty.splitTyConApp_maybe ty =
|
|
|
|
let name' = showName dflag style name -- Convert name to string
|
|
|
|
in getTyCon dflag style name' tyCon
|
|
|
|
getTyCons dflag style name _ = [showName dflag style name]
|
|
|
|
|
|
|
|
-- Write cases for one type
|
|
|
|
getTyCon :: DynFlags -> PprStyle -> String -> Ty.TyCon -> [String]
|
|
|
|
-- 1. Non-matcheable type constructors
|
|
|
|
getTyCon _ _ name tyCon | isNotMatcheableTyCon tyCon = [name]
|
|
|
|
-- 2. Special cases
|
|
|
|
-- 2.1. Tuples
|
|
|
|
getTyCon _ _ name tyCon | Ty.isTupleTyCon tyCon =
|
|
|
|
let [uniqueDataCon] = Ty.tyConDataCons tyCon
|
|
|
|
tupleArity = Ty.dataConSourceArity uniqueDataCon
|
|
|
|
-- Deal with both boxed and unboxed tuples
|
|
|
|
isUnboxed = Ty.isUnboxedTupleTyCon tyCon
|
|
|
|
startSign = if isUnboxed then "(#" else "("
|
|
|
|
endSign = if isUnboxed then "#)" else ")"
|
|
|
|
in [ startSign ++ intercalate "," (map (\n -> name ++ show n) [1 .. tupleArity]) ++ endSign ]
|
|
|
|
-- 3. General case
|
|
|
|
getTyCon dflag style name tyCon = map (getDataCon dflag style name) (Ty.tyConDataCons tyCon)
|
|
|
|
|
|
|
|
-- These type constructors should not be matched against
|
|
|
|
isNotMatcheableTyCon :: Ty.TyCon -> Bool
|
|
|
|
isNotMatcheableTyCon ty = Ty.isPrimTyCon ty -- Primitive types, such as Int#
|
|
|
|
|| Ty.isFunTyCon ty -- Function types
|
|
|
|
|
|
|
|
-- Write case for one constructor
|
|
|
|
getDataCon :: DynFlags -> PprStyle -> String -> Ty.DataCon -> String
|
|
|
|
-- 1. Infix constructors
|
|
|
|
getDataCon dflag style vName dcon | Ty.dataConIsInfix dcon =
|
|
|
|
let dName = showName dflag style $ Ty.dataConName dcon
|
|
|
|
in case Ty.dataConSourceArity dcon of
|
|
|
|
0 -> dName
|
|
|
|
1 -> vName ++ dName
|
|
|
|
n -> if dName == ":" -- Special case for lists
|
|
|
|
then vName ++ ":" ++ vName ++ "s"
|
|
|
|
else newVar vName 1 ++ " " ++ dName ++ " " ++ newVars vName 2 (n-1)
|
|
|
|
-- 2. Non-record, non-infix syntax
|
|
|
|
getDataCon dflag style vName dcon | [] <- Ty.dataConFieldLabels dcon =
|
|
|
|
let dName = showName dflag style $ Ty.dataConName dcon
|
|
|
|
in if last dName == '#' -- Special case for I#, C# and so on
|
|
|
|
then vName
|
2014-06-25 16:09:24 +00:00
|
|
|
else case Ty.dataConSourceArity dcon of
|
|
|
|
0 -> dName
|
|
|
|
_ -> dName ++ " " ++ newVarsSpecialSingleton vName 1 (Ty.dataConSourceArity dcon)
|
2014-06-21 09:38:44 +00:00
|
|
|
-- 3. Records
|
|
|
|
getDataCon dflag style vName dcon =
|
|
|
|
let dName = showName dflag style $ Ty.dataConName dcon
|
|
|
|
flds = Ty.dataConFieldLabels dcon
|
|
|
|
in dName ++ " { " ++ showFieldNames dflag style vName flds ++ " }"
|
|
|
|
|
|
|
|
-- Create a new variable by adjoining a number
|
|
|
|
newVar :: String -> Int -> String
|
|
|
|
newVar v n = v ++ show n
|
|
|
|
|
|
|
|
-- Create a list of variables which start with the same prefix
|
|
|
|
newVars :: String -> Int -> Int -> String
|
|
|
|
newVars _ _ 0 = ""
|
|
|
|
newVars v s 1 = newVar v s
|
|
|
|
newVars v s m = newVar v s ++ " " ++ newVars v (s+1) (m-1)
|
|
|
|
|
|
|
|
-- Create a list of variables which start with the same prefix
|
|
|
|
-- Special case for a single variable, in which case no number is adjoint
|
|
|
|
newVarsSpecialSingleton :: String -> Int -> Int -> String
|
|
|
|
newVarsSpecialSingleton v _ 1 = v
|
|
|
|
newVarsSpecialSingleton v start n = newVars v start n
|
|
|
|
|
|
|
|
showFieldNames :: DynFlags -> PprStyle -> String -> [G.Name] -> String
|
|
|
|
showFieldNames _ _ _ [] = "" -- This should never happen
|
|
|
|
showFieldNames dflag style v (x:xs) = let fName = showName dflag style x
|
|
|
|
fAcc = fName ++ " = " ++ v ++ "_" ++ fName
|
|
|
|
in case xs of
|
|
|
|
[] -> fAcc
|
|
|
|
_ -> fAcc ++ ", " ++ showFieldNames dflag style v xs
|
|
|
|
|
|
|
|
----------------------------------------------------------------
|
2014-06-27 17:06:20 +00:00
|
|
|
-- c. Code for performing the case splitting
|
2014-06-21 09:38:44 +00:00
|
|
|
|
2015-04-02 23:15:12 +00:00
|
|
|
genCaseSplitTextFile :: (MonadIO m, GhcMonad m) =>
|
|
|
|
FilePath -> SplitToTextInfo -> m String
|
2014-06-25 16:09:24 +00:00
|
|
|
genCaseSplitTextFile file info = liftIO $ do
|
2015-03-03 20:12:43 +00:00
|
|
|
t <- T.readFile file
|
|
|
|
return $ getCaseSplitText (T.lines t) info
|
2014-06-25 16:09:24 +00:00
|
|
|
|
|
|
|
getCaseSplitText :: [T.Text] -> SplitToTextInfo -> String
|
2015-03-03 20:12:43 +00:00
|
|
|
getCaseSplitText t (SplitToTextInfo { sVarName = sVN, sBindingSpan = sBS
|
2014-06-27 17:32:05 +00:00
|
|
|
, sVarSpan = sVS, sTycons = sT }) =
|
2015-03-03 20:12:43 +00:00
|
|
|
let bindingText = getBindingText t sBS
|
2014-06-27 17:32:05 +00:00
|
|
|
difference = srcSpanDifference sBS sVS
|
2014-11-01 13:06:34 +00:00
|
|
|
replaced = map (replaceVarWithTyCon bindingText difference sVN) sT
|
|
|
|
-- The newly generated bindings need to be indented to align with the
|
|
|
|
-- original binding.
|
|
|
|
replaced' = head replaced : map (indentBindingTo sBS) (tail replaced)
|
|
|
|
in T.unpack $ T.intercalate (T.pack "\n") (concat replaced')
|
2014-06-25 16:09:24 +00:00
|
|
|
|
|
|
|
getBindingText :: [T.Text] -> SrcSpan -> [T.Text]
|
2015-03-03 20:12:43 +00:00
|
|
|
getBindingText t srcSpan =
|
2014-06-25 16:09:24 +00:00
|
|
|
let Just (sl,sc,el,ec) = Gap.getSrcSpan srcSpan
|
2015-03-03 20:12:43 +00:00
|
|
|
lines_ = drop (sl - 1) $ take el t
|
2014-06-25 16:09:24 +00:00
|
|
|
in if sl == el
|
|
|
|
then -- only one line
|
|
|
|
[T.drop (sc - 1) $ T.take ec $ head lines_]
|
|
|
|
else -- several lines
|
|
|
|
let (first,rest,last_) = (head lines_, tail $ init lines_, last lines_)
|
2014-07-17 08:16:44 +00:00
|
|
|
in T.drop (sc - 1) first : rest ++ [T.take ec last_]
|
2014-06-25 16:09:24 +00:00
|
|
|
|
|
|
|
srcSpanDifference :: SrcSpan -> SrcSpan -> (Int,Int,Int,Int)
|
|
|
|
srcSpanDifference b v =
|
|
|
|
let Just (bsl,bsc,_ ,_) = Gap.getSrcSpan b
|
|
|
|
Just (vsl,vsc,vel,vec) = Gap.getSrcSpan v
|
|
|
|
in (vsl - bsl, vsc - bsc, vel - bsl, vec - bsc) -- assume variable in one line
|
|
|
|
|
|
|
|
replaceVarWithTyCon :: [T.Text] -> (Int,Int,Int,Int) -> String -> String -> [T.Text]
|
2015-03-03 20:12:43 +00:00
|
|
|
replaceVarWithTyCon t (vsl,vsc,_,vec) varname tycon =
|
2014-06-25 16:09:24 +00:00
|
|
|
let tycon' = if ' ' `elem` tycon || ':' `elem` tycon then "(" ++ tycon ++ ")" else tycon
|
|
|
|
lengthDiff = length tycon' - length varname
|
|
|
|
tycon'' = T.pack $ if lengthDiff < 0 then tycon' ++ replicate (-lengthDiff) ' ' else tycon'
|
|
|
|
spacesToAdd = if lengthDiff < 0 then 0 else lengthDiff
|
|
|
|
in zipWith (\n line -> if n < vsl
|
|
|
|
then line -- before variable starts
|
|
|
|
else if n == vsl
|
|
|
|
then T.take vsc line `T.append` tycon'' `T.append` T.drop vec line
|
|
|
|
else T.replicate spacesToAdd (T.pack " ") `T.append` line)
|
2015-03-03 20:12:43 +00:00
|
|
|
[0 ..] t
|
2014-11-01 13:06:34 +00:00
|
|
|
|
|
|
|
indentBindingTo :: SrcSpan -> [T.Text] -> [T.Text]
|
|
|
|
indentBindingTo bndLoc binds =
|
|
|
|
let Just (_,sl,_,_) = Gap.getSrcSpan bndLoc
|
|
|
|
indent = (T.replicate (sl - 1) (T.pack " ") `T.append`)
|
|
|
|
in indent (head binds) : tail binds
|