1
+ {-# LANGUAGE OverloadedStrings #-}
2
+ {-# LANGUAGE ViewPatterns #-}
1
3
module Ide.Plugin.StylishHaskell
2
4
(
3
5
descriptor
@@ -6,14 +8,17 @@ module Ide.Plugin.StylishHaskell
6
8
where
7
9
8
10
import Control.Monad.IO.Class
9
- import Data.Text (Text )
10
- import qualified Data.Text as T
11
- import Development.IDE (IdeState )
11
+ import Data.Text (Text )
12
+ import qualified Data.Text as T
13
+ import Development.IDE
14
+ import Development.IDE.GHC.Compat (ModSummary (ms_hspp_opts ))
15
+ import qualified DynFlags as D
16
+ import qualified EnumSet as ES
17
+ import GHC.LanguageExtensions.Type
12
18
import Ide.PluginUtils
13
19
import Ide.Types
20
+ import Language.Haskell.LSP.Types as J
14
21
import Language.Haskell.Stylish
15
- import Language.Haskell.LSP.Types as J
16
-
17
22
import System.Directory
18
23
import System.FilePath
19
24
@@ -26,16 +31,33 @@ descriptor plId = (defaultPluginDescriptor plId)
26
31
-- Formats the given source in either a given Range or the whole Document.
27
32
-- If the provider fails an error is returned that can be displayed to the user.
28
33
provider :: FormattingProvider IdeState IO
29
- provider _lf _ideState typ contents fp _opts = do
34
+ provider _lf ide typ contents fp _opts = do
35
+ (ms_hspp_opts -> dyn, _) <- runAction " stylish-haskell" ide $ use_ GetModSummary fp
30
36
let file = fromNormalizedFilePath fp
31
37
config <- liftIO $ loadConfigFrom file
38
+ mergedConfig <- getMergedConfig dyn config
32
39
let (range, selectedContents) = case typ of
33
40
FormatText -> (fullRange contents, contents)
34
41
FormatRange r -> (normalize r, extractRange r contents)
35
- result = runStylishHaskell file config selectedContents
42
+ result = runStylishHaskell file mergedConfig selectedContents
36
43
case result of
37
44
Left err -> return $ Left $ responseError $ T. pack $ " stylishHaskellCmd: " ++ err
38
45
Right new -> return $ Right $ J. List [TextEdit range new]
46
+ where
47
+ getMergedConfig dyn config
48
+ | null (configLanguageExtensions config)
49
+ = do
50
+ logInfo (ideLogger ide) " stylish-haskell uses the language extensions from DynFlags"
51
+ pure
52
+ $ config
53
+ { configLanguageExtensions = getExtensions dyn }
54
+ | otherwise
55
+ = pure config
56
+
57
+ getExtensions = map showExtension . ES. toList . D. extensionFlags
58
+
59
+ showExtension Cpp = " CPP"
60
+ showExtension other = show other
39
61
40
62
-- | Recursively search in every directory of the given filepath for .stylish-haskell.yaml.
41
63
-- If no such file has been found, return default config.
@@ -45,7 +67,7 @@ loadConfigFrom file = do
45
67
setCurrentDirectory (takeDirectory file)
46
68
config <- loadConfig (makeVerbose False ) Nothing
47
69
setCurrentDirectory currDir
48
- return config
70
+ pure config
49
71
50
72
-- | Run stylish-haskell on the given text with the given configuration.
51
73
runStylishHaskell :: FilePath -- ^ Location of the file being formatted. Used for error message
0 commit comments