Skip to content

Commit 199f4cb

Browse files
committed
Make StylishHaskell plugin to recognize extensions from DynFlags
1 parent 32e1fad commit 199f4cb

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

plugins/default/src/Ide/Plugin/StylishHaskell.hs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE ViewPatterns #-}
13
module Ide.Plugin.StylishHaskell
24
(
35
descriptor
@@ -6,14 +8,17 @@ module Ide.Plugin.StylishHaskell
68
where
79

810
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
1218
import Ide.PluginUtils
1319
import Ide.Types
20+
import Language.Haskell.LSP.Types as J
1421
import Language.Haskell.Stylish
15-
import Language.Haskell.LSP.Types as J
16-
1722
import System.Directory
1823
import System.FilePath
1924

@@ -26,16 +31,33 @@ descriptor plId = (defaultPluginDescriptor plId)
2631
-- Formats the given source in either a given Range or the whole Document.
2732
-- If the provider fails an error is returned that can be displayed to the user.
2833
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
3036
let file = fromNormalizedFilePath fp
3137
config <- liftIO $ loadConfigFrom file
38+
mergedConfig <- getMergedConfig dyn config
3239
let (range, selectedContents) = case typ of
3340
FormatText -> (fullRange contents, contents)
3441
FormatRange r -> (normalize r, extractRange r contents)
35-
result = runStylishHaskell file config selectedContents
42+
result = runStylishHaskell file mergedConfig selectedContents
3643
case result of
3744
Left err -> return $ Left $ responseError $ T.pack $ "stylishHaskellCmd: " ++ err
3845
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
3961

4062
-- | Recursively search in every directory of the given filepath for .stylish-haskell.yaml.
4163
-- If no such file has been found, return default config.
@@ -45,7 +67,7 @@ loadConfigFrom file = do
4567
setCurrentDirectory (takeDirectory file)
4668
config <- loadConfig (makeVerbose False) Nothing
4769
setCurrentDirectory currDir
48-
return config
70+
pure config
4971

5072
-- | Run stylish-haskell on the given text with the given configuration.
5173
runStylishHaskell :: FilePath -- ^ Location of the file being formatted. Used for error message

0 commit comments

Comments
 (0)