@@ -4,6 +4,7 @@ module Haskell.Ide.ApplyRefactPlugin where
4
4
5
5
import Control.Arrow
6
6
import Control.Monad.IO.Class
7
+ import Control.Monad.Trans.Either
7
8
import Data.Aeson
8
9
import qualified Data.Text as T
9
10
import qualified Data.Text.IO as T
@@ -12,7 +13,7 @@ import Haskell.Ide.Engine.MonadFunctions
12
13
import Haskell.Ide.Engine.PluginDescriptor
13
14
import Haskell.Ide.Engine.PluginUtils
14
15
import Haskell.Ide.Engine.SemanticTypes
15
- import Language.Haskell.HLint3
16
+ import Language.Haskell.HLint3 as Hlint
16
17
import Refact.Apply
17
18
import qualified Refact.Types as R
18
19
import Refact.Types hiding (SrcSpan )
@@ -78,19 +79,29 @@ applyAllCmd = CmdSync $ \_ctxs req -> do
78
79
applyHint :: FilePath -> Maybe Pos -> IO (Either String HieDiff )
79
80
applyHint file mpos = do
80
81
withTempFile $ \ f -> do
81
- (flags,classify,hint) <- autoSettings
82
- res <- parseModuleEx flags file Nothing
83
- case res of
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 ]
89
- logm $ " applyHint:commands=" ++ show commands
90
- appliedFile <- applyRefactorings mpos commands file
91
- diff <- makeDiffResult file (T. pack appliedFile)
92
- logm $ " applyHint:diff=" ++ show diff
93
- return $ Right diff
82
+ let optsf = " -o " ++ f
83
+ opts = case mpos of
84
+ Nothing -> optsf
85
+ Just (r,c) -> optsf ++ " --pos " ++ show r ++ " ," ++ show c
86
+ hlintOpts = [file, " --quiet" , " --refactor" , " --refactor-options=" ++ opts ]
87
+ runEitherT $ do
88
+ ideas <- runHlint file hlintOpts
89
+ liftIO $ logm $ " applyHint:ideas=" ++ show ideas
90
+ let commands = map (show &&& ideaRefactoring) ideas
91
+ appliedFile <- liftIO $ applyRefactorings mpos commands file
92
+ diff <- liftIO $ makeDiffResult file (T. pack appliedFile)
93
+ liftIO $ logm $ " applyHint:diff=" ++ show diff
94
+ return diff
95
+
96
+
97
+ runHlint :: FilePath -> [String ] -> EitherT String IO [Idea ]
98
+ runHlint file args =
99
+ do (flags,classify,hint) <- liftIO $ argsSettings args
100
+ res <- bimapEitherT showParseError id $ EitherT $ parseModuleEx flags file Nothing
101
+ pure $ applyHints classify hint [res]
102
+
103
+ showParseError :: Hlint. ParseError -> String
104
+ showParseError (Hlint. ParseError loc message content) = unlines [show loc, message, content]
94
105
95
106
makeDiffResult :: FilePath -> T. Text -> IO HieDiff
96
107
makeDiffResult orig new = do
0 commit comments