2
2
{-# LANGUAGE GADTs #-}
3
3
module Haskell.Ide.ApplyRefactPlugin where
4
4
5
+ import Control.Arrow
5
6
import Control.Monad.IO.Class
7
+ import Data.Aeson
6
8
import qualified Data.Text as T
7
9
import qualified Data.Text.IO as T
8
10
import Data.Vinyl
9
11
import Haskell.Ide.Engine.MonadFunctions
10
12
import Haskell.Ide.Engine.PluginDescriptor
11
13
import Haskell.Ide.Engine.PluginUtils
12
14
import Haskell.Ide.Engine.SemanticTypes
13
- import Language.Haskell.HLint
14
15
import Language.Haskell.HLint3
15
16
import Refact.Apply
16
17
import qualified Refact.Types as R
@@ -49,10 +50,10 @@ applyOneCmd = CmdSync $ \_ctxs req -> do
49
50
logm $ " applyOneCmd:res=" ++ show res
50
51
case res of
51
52
Left err -> return $ IdeResponseFail (IdeError PluginError
52
- (T. pack $ " applyOne: " ++ show err) Nothing )
53
+ (T. pack $ " applyOne: " ++ show err) Null )
53
54
Right fs -> return (IdeResponseOk fs)
54
55
Right _ -> return $ IdeResponseError (IdeError InternalError
55
- " ApplyRefactPlugin.applyOneCmd: ghc’s exhaustiveness checker is broken" Nothing )
56
+ " ApplyRefactPlugin.applyOneCmd: ghc’s exhaustiveness checker is broken" Null )
56
57
57
58
58
59
-- ---------------------------------------------------------------------
@@ -66,50 +67,31 @@ applyAllCmd = CmdSync $ \_ctxs req -> do
66
67
logm $ " applyAllCmd:res=" ++ show res
67
68
case res of
68
69
Left err -> return $ IdeResponseFail (IdeError PluginError
69
- (T. pack $ " applyOne: " ++ show err) Nothing )
70
+ (T. pack $ " applyOne: " ++ show err) Null )
70
71
Right fs -> return (IdeResponseOk fs)
71
72
Right _ -> return $ IdeResponseError (IdeError InternalError
72
- " ApplyRefactPlugin.applyOneCmd: ghc’s exhaustiveness checker is broken" Nothing )
73
+ " ApplyRefactPlugin.applyOneCmd: ghc’s exhaustiveness checker is broken" Null )
73
74
74
75
75
76
-- ---------------------------------------------------------------------
76
77
77
78
applyHint :: FilePath -> Maybe Pos -> IO (Either String HieDiff )
78
79
applyHint file mpos = do
79
80
withTempFile $ \ f -> do
80
- -- absFile <- makeAbsolute file
81
- -- hlint /tmp/Foo.hs --refactor --refactor-options="-o /tmp/Bar.hs --pos 2,8"
82
-
83
- let
84
- optsf = " -o " ++ f
85
- opts = case mpos of
86
- Nothing -> optsf
87
- Just (r,c) -> optsf ++ " --pos " ++ show r ++ " ," ++ show c
88
- -- let hlintOpts = [file, "--quiet", "--refactor", "--refactor-options=" ++ opts ]
89
- let hlintOpts = [file, " --quiet" ]
90
- logm $ " applyHint=" ++ show hlintOpts
91
- res <- catchException $ hlint hlintOpts
92
- logm $ " applyHint:res=" ++ show res
93
- -- res <- hlint hlintOpts
81
+ (flags,classify,hint) <- autoSettings
82
+ res <- parseModuleEx flags file Nothing
94
83
case res of
95
- Left x -> return $ Left (show x)
96
- Right x -> do
97
- let commands = makeApplyRefact x
84
+ Left err -> return $ Left (unlines [show $ parseErrorLocation err
85
+ ,parseErrorMessage err
86
+ ,parseErrorContents err])
87
+ Right mod -> do
88
+ let commands = map (show &&& ideaRefactoring) $ applyHints classify hint [mod ]
98
89
logm $ " applyHint:commands=" ++ show commands
99
90
appliedFile <- applyRefactorings mpos commands file
100
91
diff <- makeDiffResult file (T. pack appliedFile)
101
92
logm $ " applyHint:diff=" ++ show diff
102
93
return $ Right diff
103
94
104
- -- ---------------------------------------------------------------------
105
-
106
- makeApplyRefact :: [Suggestion ] -> [(String , [Refactoring R. SrcSpan ])]
107
- makeApplyRefact suggestions =
108
- map (\ (Suggestion i) -> (show i, ideaRefactoring i)) suggestions
109
-
110
- -- ---------------------------------------------------------------------
111
-
112
-
113
95
makeDiffResult :: FilePath -> T. Text -> IO HieDiff
114
96
makeDiffResult orig new = do
115
97
origText <- T. readFile orig
0 commit comments