Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add applyRefactorings' #100

Merged
merged 1 commit into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/Refact/Apply.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

module Refact.Apply
( applyRefactorings
, applyRefactorings'
, runRefactoring
, parseExtensions
) where

import Control.Monad (unless)
import Data.List (intercalate)
import Refact.Fixity
import Language.Haskell.GHC.ExactPrint.Types (Anns)
import Refact.Fixity (applyFixities)
import Refact.Internal
import Refact.Types
import Refact.Types (Refactoring, SrcSpan)
import Refact.Utils (Module)

-- | Apply a set of refactorings as supplied by hlint
-- | Apply a set of refactorings as supplied by HLint
applyRefactorings
:: Maybe (Int, Int)
-- ^ Apply hints relevant to a specific position
Expand All @@ -39,4 +42,16 @@ applyRefactorings optionsPos inp file exts = do
unless (null invalid) . fail $ "Unsupported extensions: " ++ intercalate ", " invalid
(as, m) <- either (onError "apply") (uncurry applyFixities)
=<< parseModuleWithArgs (enabled, disabled) file
apply optionsPos False ((mempty,) <$> inp) file Silent as m
apply optionsPos False ((mempty,) <$> inp) (Just file) Silent as m

-- | Like 'applyRefactorings', but takes a parsed module rather than a file path to parse.
applyRefactorings'
:: Maybe (Int, Int)
-> [[Refactoring SrcSpan]]
-> Anns
-- ^ ghc-exactprint AST annotations. This can be obtained from
-- 'Language.Haskell.GHC.ExactPrint.Parsers.postParseTransform'.
-> Module
-- ^ Parsed module
-> IO String
applyRefactorings' optionsPos inp = apply optionsPos False ((mempty,) <$> inp) Nothing Silent
17 changes: 13 additions & 4 deletions src/Refact/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import Data.Maybe
import Data.List
import Data.Ord
import DynFlags hiding (initDynFlags)
import FastString (unpackFS)
import HeaderInfo (getOptions)
import HscTypes (handleSourceError)
import GHC.IO.Exception (IOErrorType(..))
Expand Down Expand Up @@ -82,7 +83,7 @@ import qualified RdrName as GHC
import Refact.Types hiding (SrcSpan)
import qualified Refact.Types as R
import Refact.Utils (Stmt, Pat, Name, Decl, M, Module, Expr, Type, FunBind
, modifyAnnKey, replaceAnnKey, Import, toGhcSrcSpan, setSrcSpanFile)
, modifyAnnKey, replaceAnnKey, Import, toGhcSrcSpan, toGhcSrcSpan', setSrcSpanFile)

#if __GLASGOW_HASKELL__ >= 810
type Errors = ErrorMessages
Expand Down Expand Up @@ -117,14 +118,22 @@ apply
:: Maybe (Int, Int)
-> Bool
-> [(String, [Refactoring R.SrcSpan])]
-> FilePath
-> Maybe FilePath
-> Verbosity
-> Anns
-> Module
-> IO String
apply mpos step inp file verb as0 m0 = do
apply mpos step inp mbfile verb as0 m0 = do
toGhcSS <-
maybe
( case getLoc m0 of
UnhelpfulSpan s -> fail $ "Module has UnhelpfulSpan: " ++ unpackFS s
RealSrcSpan s -> pure $ toGhcSrcSpan' (srcSpanFile s)
)
(pure . toGhcSrcSpan)
mbfile
let noOverlapInp = removeOverlap verb inp
allRefacts = (fmap . fmap . fmap) (toGhcSrcSpan file) <$> noOverlapInp
allRefacts = (fmap . fmap . fmap) toGhcSS <$> noOverlapInp

posFilter (_, rs) =
case mpos of
Expand Down
2 changes: 1 addition & 1 deletion src/Refact/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ runPipe Options{..} file = do
(as, m) <- either (onError "runPipe") (uncurry applyFixities)
=<< parseModuleWithArgs (enabledExts, disabledExts) file
when optionsDebug (putStrLn (showAnnData as 0 m))
apply optionsPos optionsStep inp file verb as m
apply optionsPos optionsStep inp (Just file) verb as m

if optionsInplace && isJust optionsTarget
then writeFileUTF8 file output
Expand Down
10 changes: 7 additions & 3 deletions src/Refact/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Refact.Utils ( -- * Synonyms
, modifyAnnKey
, replaceAnnKey
, toGhcSrcSpan
, toGhcSrcSpan'
, setSrcSpanFile
, findParent

Expand Down Expand Up @@ -189,12 +190,15 @@ replaceAnnKey :: AnnKey -> AnnKey -> AnnKey -> AnnKey -> Anns -> Anns
replaceAnnKey old new inp deltainfo a =
fromMaybe a (replace old new inp deltainfo a)


-- | Convert a @Refact.Types.SrcSpan@ to a @SrcLoc.SrcSpan@
toGhcSrcSpan :: FilePath -> R.SrcSpan -> SrcSpan
toGhcSrcSpan file R.SrcSpan{..} = mkSrcSpan (f startLine startCol) (f endLine endCol)
toGhcSrcSpan = toGhcSrcSpan' . GHC.mkFastString

-- | Convert a @Refact.Types.SrcSpan@ to a @SrcLoc.SrcSpan@
toGhcSrcSpan' :: FastString -> R.SrcSpan -> SrcSpan
toGhcSrcSpan' file R.SrcSpan{..} = mkSrcSpan (f startLine startCol) (f endLine endCol)
where
f = mkSrcLoc (GHC.mkFastString file)
f = mkSrcLoc file

setSrcSpanFile :: FastString -> SrcSpan -> SrcSpan
setSrcSpanFile file s
Expand Down