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

Commit 8425de6

Browse files
committed
Can use apply-refact without worrying about stdio
1 parent 3ce3b52 commit 8425de6

File tree

4 files changed

+48
-19
lines changed

4 files changed

+48
-19
lines changed

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

+26-8
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ module Haskell.Ide.ApplyRefactPlugin where
55
import Control.Exception
66
import Control.Monad.IO.Class
77
import qualified Data.Text as T
8+
import qualified Data.Text.IO as T
89
import Data.Vinyl
910
import Haskell.Ide.Engine.MonadFunctions
1011
import Haskell.Ide.Engine.PluginDescriptor
1112
import Haskell.Ide.Engine.PluginUtils
1213
import Haskell.Ide.Engine.SemanticTypes
1314
import qualified Language.Haskell.GhcMod as GM (defaultOptions)
1415
import Language.Haskell.HLint
16+
import Language.Haskell.HLint3
17+
import Refact.Apply
18+
import qualified Refact.Types as R
19+
import Refact.Types hiding (SrcSpan)
1520
import System.Directory
1621
import System.Exit
1722
import System.FilePath.Posix
@@ -46,6 +51,7 @@ applyOneCmd = CmdSync $ \_ctxs req -> do
4651
Left err -> return err
4752
Right (ParamFile fileName :& ParamPos pos :& RNil) -> do
4853
res <- liftIO $ applyHint (T.unpack fileName) (Just pos)
54+
logm $ "applyOneCmd:res=" ++ show res
4955
case res of
5056
Left err -> return $ IdeResponseFail (IdeError PluginError
5157
(T.pack $ "applyOne: " ++ show err) Nothing)
@@ -62,6 +68,7 @@ applyAllCmd = CmdSync $ \_ctxs req -> do
6268
Left err -> return err
6369
Right (ParamFile fileName :& RNil) -> do
6470
res <- liftIO $ applyHint (T.unpack fileName) Nothing
71+
logm $ "applyAllCmd:res=" ++ show res
6572
case res of
6673
Left err -> return $ IdeResponseFail (IdeError PluginError
6774
(T.pack $ "applyOne: " ++ show err) Nothing)
@@ -83,25 +90,36 @@ applyHint file mpos = do
8390
opts = case mpos of
8491
Nothing -> optsf
8592
Just (r,c) -> optsf ++ " --pos " ++ show r ++ "," ++ show c
86-
let hlintOpts = [file, "--refactor", "--refactor-options=" ++ opts ]
93+
-- let hlintOpts = [file, "--quiet", "--refactor", "--refactor-options=" ++ opts ]
94+
let hlintOpts = [file, "--quiet" ]
8795
logm $ "applyHint=" ++ show hlintOpts
8896
res <- catchException $ hlint hlintOpts
8997
logm $ "applyHint:res=" ++ show res
98+
-- res <- hlint hlintOpts
9099
case res of
91-
Left "ExitSuccess" -> do
92-
diff <- makeDiffResult file f
100+
Left x -> return $ Left (show x)
101+
Right x -> do
102+
let commands = makeApplyRefact x
103+
logm $ "applyHint:commands=" ++ show commands
104+
appliedFile <- applyRefactorings mpos commands file
105+
diff <- makeDiffResult file (T.pack appliedFile)
93106
logm $ "applyHint:diff=" ++ show diff
94107
return $ Right diff
95-
Left x -> return $ Left (show x)
96-
Right x -> return $ Left (show x)
97108

98109
-- ---------------------------------------------------------------------
99110

100-
makeDiffResult :: FilePath -> FilePath -> IO HieDiff
111+
makeApplyRefact :: [Suggestion] -> [(String, [Refactoring R.SrcSpan])]
112+
makeApplyRefact suggestions =
113+
map (\(Suggestion i) -> (show i, ideaRefactoring i)) suggestions
114+
115+
-- ---------------------------------------------------------------------
116+
117+
118+
makeDiffResult :: FilePath -> T.Text -> IO HieDiff
101119
makeDiffResult orig new = do
102-
(HieDiff f s d) <- diffFiles orig new
120+
origText <- T.readFile orig
121+
let (HieDiff f s d) = diffText (orig,origText) ("changed",new)
103122
f' <- liftIO $ makeRelativeToCurrentDirectory f
104-
s' <- liftIO $ makeRelativeToCurrentDirectory s
105123
-- return (HieDiff f' s' d)
106124
return (HieDiff f' "changed" d)
107125

hie-apply-refact/hie-apply-refact.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ library
2323
, ghc-mod
2424
, hie-plugin-api
2525
, hlint
26+
, refact
2627
, text
2728
, transformers
2829
, vinyl >= 0.5 && < 0.6

hie-plugin-api/Haskell/Ide/Engine/PluginUtils.hs

+13-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Haskell.Ide.Engine.PluginUtils
88
getParams
99
, mapEithers
1010
, diffFiles
11+
, diffText
1112
-- * Helper functions for errors
1213
, missingParameter
1314
, incorrectParameter
@@ -89,11 +90,16 @@ diffFiles :: FilePath -> FilePath -> IO HieDiff
8990
diffFiles f1 f2 = do
9091
f1Text <- T.readFile f1
9192
f2Text <- T.readFile f2
92-
let diffb = getDiffBy (\(_,a) (_,b) -> a == b)
93-
(zip [1..] (T.lines f1Text))
94-
(zip [1..] (T.lines f2Text))
95-
isDiff (Both {}) = False
96-
isDiff _ = True
93+
return $ diffText (f1,f1Text) (f2,f2Text)
9794

98-
diff = filter isDiff diffb
99-
return (HieDiff f1 f2 diff)
95+
-- |Generate a 'HieDiff' value from a pair of source Text
96+
diffText :: (FilePath,T.Text) -> (FilePath,T.Text) -> HieDiff
97+
diffText (f1,f1Text) (f2,f2Text) = HieDiff f1 f2 diff
98+
where
99+
diffb = getDiffBy (\(_,a) (_,b) -> a == b)
100+
(zip [1..] (T.lines f1Text))
101+
(zip [1..] (T.lines f2Text))
102+
isDiff (Both {}) = False
103+
isDiff _ = True
104+
105+
diff = filter isDiff diffb

stack.yaml

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ packages:
1414
commit: b9bd4ebf77b22d2d9061d647d7799ddcc7c51228
1515
# commit: bff86be69f556f80a8dcd9dd42774ab77cb00eba
1616
extra-dep: true
17-
# - location:
18-
# git: https://github.com/alanz/hlint.git
19-
# commit: 6fb531d5ba32a4ba0e1a1c189e5f81ac7c43803f
20-
# extra-dep: true
17+
- location:
18+
git: https://github.com/alanz/hlint.git
19+
commit: e32f4d3cf32d15003e54d4f42afae7bf06b50168
20+
extra-dep: true
21+
- location:
22+
git: https://github.com/alanz/apply-refact.git
23+
commit: ba98a2902e5333519e60d38803f30f82c44eaffc
24+
extra-dep: true
2125
extra-deps:
2226
- HaRe-0.8.2.1
2327
- rosezipper-0.2

0 commit comments

Comments
 (0)