@@ -5,13 +5,18 @@ module Haskell.Ide.ApplyRefactPlugin where
5
5
import Control.Exception
6
6
import Control.Monad.IO.Class
7
7
import qualified Data.Text as T
8
+ import qualified Data.Text.IO as T
8
9
import Data.Vinyl
9
10
import Haskell.Ide.Engine.MonadFunctions
10
11
import Haskell.Ide.Engine.PluginDescriptor
11
12
import Haskell.Ide.Engine.PluginUtils
12
13
import Haskell.Ide.Engine.SemanticTypes
13
14
import qualified Language.Haskell.GhcMod as GM (defaultOptions )
14
15
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 )
15
20
import System.Directory
16
21
import System.Exit
17
22
import System.FilePath.Posix
@@ -46,6 +51,7 @@ applyOneCmd = CmdSync $ \_ctxs req -> do
46
51
Left err -> return err
47
52
Right (ParamFile fileName :& ParamPos pos :& RNil ) -> do
48
53
res <- liftIO $ applyHint (T. unpack fileName) (Just pos)
54
+ logm $ " applyOneCmd:res=" ++ show res
49
55
case res of
50
56
Left err -> return $ IdeResponseFail (IdeError PluginError
51
57
(T. pack $ " applyOne: " ++ show err) Nothing )
@@ -62,6 +68,7 @@ applyAllCmd = CmdSync $ \_ctxs req -> do
62
68
Left err -> return err
63
69
Right (ParamFile fileName :& RNil ) -> do
64
70
res <- liftIO $ applyHint (T. unpack fileName) Nothing
71
+ logm $ " applyAllCmd:res=" ++ show res
65
72
case res of
66
73
Left err -> return $ IdeResponseFail (IdeError PluginError
67
74
(T. pack $ " applyOne: " ++ show err) Nothing )
@@ -83,25 +90,36 @@ applyHint file mpos = do
83
90
opts = case mpos of
84
91
Nothing -> optsf
85
92
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" ]
87
95
logm $ " applyHint=" ++ show hlintOpts
88
96
res <- catchException $ hlint hlintOpts
89
97
logm $ " applyHint:res=" ++ show res
98
+ -- res <- hlint hlintOpts
90
99
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)
93
106
logm $ " applyHint:diff=" ++ show diff
94
107
return $ Right diff
95
- Left x -> return $ Left (show x)
96
- Right x -> return $ Left (show x)
97
108
98
109
-- ---------------------------------------------------------------------
99
110
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
101
119
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)
103
122
f' <- liftIO $ makeRelativeToCurrentDirectory f
104
- s' <- liftIO $ makeRelativeToCurrentDirectory s
105
123
-- return (HieDiff f' s' d)
106
124
return (HieDiff f' " changed" d)
107
125
0 commit comments