Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit 89bb195

Browse files
committed
apply-refact commands pass tests
1 parent d95045b commit 89bb195

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

hie-apply-refact/Haskell/Ide/ApplyRefactPlugin.hs

+35-18
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ applyRefactDescriptor = PluginDescriptor
3030
buildCommand applyOneCmd "applyOne" "Apply a single hint"
3131
[".hs"] [CtxPoint] []
3232

33-
-- , buildCommand applyAllCmd "applyAll" "Apply all hints to the file"
34-
-- [".hs"] [CtxFile] []
33+
, buildCommand applyAllCmd "applyAll" "Apply all hints to the file"
34+
[".hs"] [CtxFile] []
3535

3636
]
3737
, pdExposedServices = []
@@ -45,39 +45,55 @@ applyOneCmd = CmdSync $ \_ctxs req -> do
4545
case getParams (IdFile "file" :& IdPos "start_pos" :& RNil) req of
4646
Left err -> return err
4747
Right (ParamFile fileName :& ParamPos pos :& RNil) -> do
48-
res <- liftIO $ catchException $ applyHint (T.unpack fileName) pos
48+
res <- liftIO $ applyHint (T.unpack fileName) (Just pos)
4949
case res of
5050
Left err -> return $ IdeResponseFail (IdeError PluginError
5151
(T.pack $ "applyOne: " ++ show err) Nothing)
52-
Right fs -> do
53-
-- r <- liftIO $ makeRefactorResult [fs]
54-
return (IdeResponseOk fs)
52+
Right fs -> return (IdeResponseOk fs)
5553
Right _ -> return $ IdeResponseError (IdeError InternalError
56-
"ApplyRefactPlugin.demoteCmd: ghc’s exhaustiveness checker is broken" Nothing)
54+
"ApplyRefactPlugin.applyOneCmd: ghc’s exhaustiveness checker is broken" Nothing)
5755

5856

5957
-- ---------------------------------------------------------------------
6058

61-
applyHint :: FilePath -> Pos -> IO (Either t HieDiff)
62-
applyHint file pos = do
59+
applyAllCmd :: CommandFunc HieDiff
60+
applyAllCmd = CmdSync $ \_ctxs req -> do
61+
case getParams (IdFile "file" :& RNil) req of
62+
Left err -> return err
63+
Right (ParamFile fileName :& RNil) -> do
64+
res <- liftIO $ applyHint (T.unpack fileName) Nothing
65+
case res of
66+
Left err -> return $ IdeResponseFail (IdeError PluginError
67+
(T.pack $ "applyOne: " ++ show err) Nothing)
68+
Right fs -> return (IdeResponseOk fs)
69+
Right _ -> return $ IdeResponseError (IdeError InternalError
70+
"ApplyRefactPlugin.applyOneCmd: ghc’s exhaustiveness checker is broken" Nothing)
71+
72+
73+
-- ---------------------------------------------------------------------
74+
75+
applyHint :: FilePath -> Maybe Pos -> IO (Either String HieDiff)
76+
applyHint file mpos = do
6377
withTempFile $ \f -> do
64-
absFile <- makeAbsolute file
78+
-- absFile <- makeAbsolute file
6579
-- hlint /tmp/Foo.hs --refactor --refactor-options="-o /tmp/Bar.hs --pos 2,8"
6680

67-
-- let opts = "-o " ++ f
68-
let opts = "-o /tmp/BarOne.hs"
69-
-- let hlintOpts = [absFile, "--refactor", "--refactor-options=" ++ show opts ]
70-
let hlintOpts = ["/tmp/Foo.hs", "--refactor", "--refactor-options=" ++ opts ]
71-
-- let hlintOpts = ["/tmp/Foo.hs", "--refactor" ]
81+
let
82+
optsf = "-o " ++ f
83+
opts = case mpos of
84+
Nothing -> optsf
85+
Just (r,c) -> optsf ++ " --pos " ++ show r ++ "," ++ show c
86+
let hlintOpts = [file, "--refactor", "--refactor-options=" ++ opts ]
7287
logm $ "applyHint=" ++ show hlintOpts
7388
res <- catchException $ hlint hlintOpts
7489
logm $ "applyHint:res=" ++ show res
7590
case res of
76-
Left ExitSuccess -> do
91+
Left "ExitSuccess" -> do
7792
diff <- makeDiffResult file f
7893
logm $ "applyHint:diff=" ++ show diff
7994
return $ Right diff
80-
_ -> return res
95+
Left x -> return $ Left (show x)
96+
Right x -> return $ Left (show x)
8197

8298
-- ---------------------------------------------------------------------
8399

@@ -86,7 +102,8 @@ makeDiffResult orig new = do
86102
(HieDiff f s d) <- diffFiles orig new
87103
f' <- liftIO $ makeRelativeToCurrentDirectory f
88104
s' <- liftIO $ makeRelativeToCurrentDirectory s
89-
return (HieDiff f' s' d)
105+
-- return (HieDiff f' s' d)
106+
return (HieDiff f' "changed" d)
90107

91108
-- ---------------------------------------------------------------------
92109

test/ApplyRefactPluginSpec.hs

+26-10
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,37 @@ applyRefactSpec = do
5151

5252
-- ---------------------------------
5353

54-
it "renames" $ do
54+
it "applies one hint only" $ do
5555

5656
let req = IdeRequest "applyOne" (Map.fromList [("file",ParamValP $ ParamFile "./test/testdata/ApplyRefact.hs")
5757
,("start_pos",ParamValP $ ParamPos (2,8))
5858
])
5959
r <- dispatchRequest req
6060
r `shouldBe`
61-
Just (IdeResponseOk (jsWrite (RefactorResult [HieDiff
62-
"test/testdata/HaReRename.hs"
63-
"test/testdata/HaReRename.refactored.hs"
64-
[ (First (4,"foo :: Int -> Int"))
65-
, (First (5,"foo x = x + 3"))
66-
, (Second (4,"foolong :: Int -> Int"))
67-
, (Second (5, "foolong x = x + 3"))
68-
]
69-
])))
61+
Just (IdeResponseOk (jsWrite (HieDiff
62+
{ dFirst = "./test/testdata/ApplyRefact.hs"
63+
, dSecond = "changed"
64+
, dDiff =
65+
[First (2,"main = (putStrLn \"hello\")")
66+
,Second (2,"main = putStrLn \"hello\"")]}
67+
)))
68+
69+
-- ---------------------------------
70+
71+
it "applies all hints" $ do
72+
73+
let req = IdeRequest "applyAll" (Map.fromList [("file",ParamValP $ ParamFile "./test/testdata/ApplyRefact.hs")
74+
])
75+
r <- dispatchRequest req
76+
r `shouldBe`
77+
Just (IdeResponseOk (jsWrite (HieDiff
78+
{ dFirst = "./test/testdata/ApplyRefact.hs"
79+
, dSecond = "changed"
80+
, dDiff =
81+
[First (2,"main = (putStrLn \"hello\")")
82+
,Second (2,"main = putStrLn \"hello\"")
83+
,First (4,"foo x = (x + 1)")
84+
,Second (4,"foo x = x + 1")]}
85+
)))
7086

7187
-- ---------------------------------

test/testdata/ApplyRefact.hs

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11

22
main = (putStrLn "hello")
3+
4+
foo x = (x + 1)

0 commit comments

Comments
 (0)