module Language.Haskell.Tools.Refactor.Representation where
import Control.Exception
import Control.Reference
import Data.List
import Data.List.Split
import Data.Typeable
import System.FilePath
import Bag (bagToList)
import ErrUtils as GHC
import Outputable
import Language.Haskell.Tools.AST as AST
type UnnamedModule = Ann AST.UModule IdDom SrcTemplateStage
type ModuleDom = (SourceFileKey, UnnamedModule)
data SourceFileKey = SourceFileKey { _sfkFileName :: FilePath
, _sfkModuleName :: String
}
deriving (Eq, Ord, Show)
data RefactorChange = ContentChanged { fromContentChanged :: ModuleDom }
| ModuleRemoved { removedModuleName :: String }
| ModuleCreated { createdModuleName :: String
, createdModuleContent :: UnnamedModule
, sameLocation :: SourceFileKey
}
instance Show RefactorChange where
show (ContentChanged (n, _)) = "ContentChanged (" ++ show n ++ ")"
show (ModuleRemoved n) = "ModuleRemoved " ++ n
show (ModuleCreated n _ other) = "ModuleCreated " ++ n ++ " (" ++ show other ++ ")"
data RefactorException = IllegalExtensions [String]
| SourceCodeProblem ErrorMessages
| UnknownException String
deriving (Show, Typeable)
instance Show ErrorMessages where
show = show . bagToList
instance Exception RefactorException where
displayException (SourceCodeProblem prob)
= "Source code problem: " ++ showSDocUnsafe (vcat (pprErrMsgBagWithLoc prob))
displayException (IllegalExtensions exts)
= "The following extensions are not allowed: " ++ (concat $ intersperse ", " exts) ++ "."
displayException (UnknownException ex) = "An unexpected problem appeared: " ++ ex ++ "."
moduleSourceFile :: String -> FilePath
moduleSourceFile m = (foldl1 (</>) (splitOn "." m)) <.> "hs"
sourceFileModule :: FilePath -> String
sourceFileModule fp = intercalate "." $ splitDirectories $ dropExtension fp
makeReferences ''SourceFileKey