Skip to content

Commit 4fbd3a3

Browse files
authored
Merge pull request #100 from zliu41/parsed
Add applyRefactorings'
2 parents f6402ca + dc98cbd commit 4fbd3a3

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

src/Refact/Apply.hs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
module Refact.Apply
44
( applyRefactorings
5+
, applyRefactorings'
56
, runRefactoring
67
, parseExtensions
78
) where
89

910
import Control.Monad (unless)
1011
import Data.List (intercalate)
11-
import Refact.Fixity
12+
import Language.Haskell.GHC.ExactPrint.Types (Anns)
13+
import Refact.Fixity (applyFixities)
1214
import Refact.Internal
13-
import Refact.Types
15+
import Refact.Types (Refactoring, SrcSpan)
16+
import Refact.Utils (Module)
1417

15-
-- | Apply a set of refactorings as supplied by hlint
18+
-- | Apply a set of refactorings as supplied by HLint
1619
applyRefactorings
1720
:: Maybe (Int, Int)
1821
-- ^ Apply hints relevant to a specific position
@@ -39,4 +42,16 @@ applyRefactorings optionsPos inp file exts = do
3942
unless (null invalid) . fail $ "Unsupported extensions: " ++ intercalate ", " invalid
4043
(as, m) <- either (onError "apply") (uncurry applyFixities)
4144
=<< parseModuleWithArgs (enabled, disabled) file
42-
apply optionsPos False ((mempty,) <$> inp) file Silent as m
45+
apply optionsPos False ((mempty,) <$> inp) (Just file) Silent as m
46+
47+
-- | Like 'applyRefactorings', but takes a parsed module rather than a file path to parse.
48+
applyRefactorings'
49+
:: Maybe (Int, Int)
50+
-> [[Refactoring SrcSpan]]
51+
-> Anns
52+
-- ^ ghc-exactprint AST annotations. This can be obtained from
53+
-- 'Language.Haskell.GHC.ExactPrint.Parsers.postParseTransform'.
54+
-> Module
55+
-- ^ Parsed module
56+
-> IO String
57+
applyRefactorings' optionsPos inp = apply optionsPos False ((mempty,) <$> inp) Nothing Silent

src/Refact/Internal.hs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import Data.Maybe
4949
import Data.List
5050
import Data.Ord
5151
import DynFlags hiding (initDynFlags)
52+
import FastString (unpackFS)
5253
import HeaderInfo (getOptions)
5354
import HscTypes (handleSourceError)
5455
import GHC.IO.Exception (IOErrorType(..))
@@ -82,7 +83,7 @@ import qualified RdrName as GHC
8283
import Refact.Types hiding (SrcSpan)
8384
import qualified Refact.Types as R
8485
import Refact.Utils (Stmt, Pat, Name, Decl, M, Module, Expr, Type, FunBind
85-
, modifyAnnKey, replaceAnnKey, Import, toGhcSrcSpan, setSrcSpanFile)
86+
, modifyAnnKey, replaceAnnKey, Import, toGhcSrcSpan, toGhcSrcSpan', setSrcSpanFile)
8687

8788
#if __GLASGOW_HASKELL__ >= 810
8889
type Errors = ErrorMessages
@@ -117,14 +118,22 @@ apply
117118
:: Maybe (Int, Int)
118119
-> Bool
119120
-> [(String, [Refactoring R.SrcSpan])]
120-
-> FilePath
121+
-> Maybe FilePath
121122
-> Verbosity
122123
-> Anns
123124
-> Module
124125
-> IO String
125-
apply mpos step inp file verb as0 m0 = do
126+
apply mpos step inp mbfile verb as0 m0 = do
127+
toGhcSS <-
128+
maybe
129+
( case getLoc m0 of
130+
UnhelpfulSpan s -> fail $ "Module has UnhelpfulSpan: " ++ unpackFS s
131+
RealSrcSpan s -> pure $ toGhcSrcSpan' (srcSpanFile s)
132+
)
133+
(pure . toGhcSrcSpan)
134+
mbfile
126135
let noOverlapInp = removeOverlap verb inp
127-
allRefacts = (fmap . fmap . fmap) (toGhcSrcSpan file) <$> noOverlapInp
136+
allRefacts = (fmap . fmap . fmap) toGhcSS <$> noOverlapInp
128137

129138
posFilter (_, rs) =
130139
case mpos of

src/Refact/Run.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ runPipe Options{..} file = do
9090
(as, m) <- either (onError "runPipe") (uncurry applyFixities)
9191
=<< parseModuleWithArgs (enabledExts, disabledExts) file
9292
when optionsDebug (putStrLn (showAnnData as 0 m))
93-
apply optionsPos optionsStep inp file verb as m
93+
apply optionsPos optionsStep inp (Just file) verb as m
9494

9595
if optionsInplace && isJust optionsTarget
9696
then writeFileUTF8 file output

src/Refact/Utils.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Refact.Utils ( -- * Synonyms
2323
, modifyAnnKey
2424
, replaceAnnKey
2525
, toGhcSrcSpan
26+
, toGhcSrcSpan'
2627
, setSrcSpanFile
2728
, findParent
2829

@@ -189,12 +190,15 @@ replaceAnnKey :: AnnKey -> AnnKey -> AnnKey -> AnnKey -> Anns -> Anns
189190
replaceAnnKey old new inp deltainfo a =
190191
fromMaybe a (replace old new inp deltainfo a)
191192

192-
193193
-- | Convert a @Refact.Types.SrcSpan@ to a @SrcLoc.SrcSpan@
194194
toGhcSrcSpan :: FilePath -> R.SrcSpan -> SrcSpan
195-
toGhcSrcSpan file R.SrcSpan{..} = mkSrcSpan (f startLine startCol) (f endLine endCol)
195+
toGhcSrcSpan = toGhcSrcSpan' . GHC.mkFastString
196+
197+
-- | Convert a @Refact.Types.SrcSpan@ to a @SrcLoc.SrcSpan@
198+
toGhcSrcSpan' :: FastString -> R.SrcSpan -> SrcSpan
199+
toGhcSrcSpan' file R.SrcSpan{..} = mkSrcSpan (f startLine startCol) (f endLine endCol)
196200
where
197-
f = mkSrcLoc (GHC.mkFastString file)
201+
f = mkSrcLoc file
198202

199203
setSrcSpanFile :: FastString -> SrcSpan -> SrcSpan
200204
setSrcSpanFile file s

0 commit comments

Comments
 (0)