11
11
12
12
import Control.Monad.Except (throwError )
13
13
import Control.Monad.IO.Class
14
+ import Data.ByteString as B
15
+ import Data.List (inits , nub )
16
+ import Data.Maybe
14
17
import Data.Text (Text )
15
18
import qualified Data.Text as T
19
+ import Data.Yaml
20
+ import Debug.Trace
16
21
import Development.IDE hiding (getExtensions ,
17
22
pluginHandlers )
18
23
import Development.IDE.Core.PluginUtils
@@ -26,8 +31,11 @@ import Ide.Types hiding (Config)
26
31
import Language.Haskell.Stylish
27
32
import Language.LSP.Protocol.Types as LSP
28
33
import System.Directory
34
+
29
35
import System.FilePath
30
36
37
+
38
+
31
39
data Log
32
40
= LogLanguageExtensionFromDynFlags
33
41
@@ -61,7 +69,7 @@ provider recorder ide _token typ contents fp _opts = do
61
69
Right new -> pure $ LSP. InL [TextEdit range new]
62
70
where
63
71
getMergedConfig dyn config
64
- | null (configLanguageExtensions config)
72
+ | Prelude. null (configLanguageExtensions config)
65
73
= do
66
74
logWith recorder Info LogLanguageExtensionFromDynFlags
67
75
pure
@@ -70,19 +78,61 @@ provider recorder ide _token typ contents fp _opts = do
70
78
| otherwise
71
79
= pure config
72
80
73
- getExtensions = map showExtension . Util. toList . extensionFlags
81
+ getExtensions = Prelude. map showExtension . Util. toList . extensionFlags
74
82
75
83
showExtension Cpp = " CPP"
76
84
showExtension other = show other
77
85
86
+ -- | taken and refactored from stylish-haskell which uses getCurrentDirectory
87
+ -- https://hackage.haskell.org/package/stylish-haskell-0.14.6.0/docs/src/Language.Haskell.Stylish.Config.html#configFilePath
88
+ -- https://github.com/haskell/haskell-language-server/issues/4234#issuecomment-2191571281
89
+ ancestors :: FilePath -> [FilePath ]
90
+ ancestors = Prelude. map joinPath . Prelude. reverse . Prelude. dropWhile Prelude. null . Data.List. inits . splitPath
91
+
92
+ configFileName :: String
93
+ configFileName = " .stylish-haskell.yaml"
94
+
95
+ configFilePathMT :: Verbose -> FilePath -> IO (Maybe FilePath )
96
+ configFilePathMT verbose currentDir = do
97
+ configPath <- getXdgDirectory XdgConfig " stylish-haskell"
98
+ home <- getHomeDirectory
99
+ search verbose $
100
+ [d </> configFileName | d <- ancestors currentDir] ++
101
+ [configPath </> " config.yaml" , home </> configFileName]
102
+
103
+ search :: Verbose -> [FilePath ] -> IO (Maybe FilePath )
104
+ search _ [] = return Nothing
105
+ search verbose (f : fs) = do
106
+ -- TODO Maybe catch an error here, dir might be unreadable
107
+ exists <- doesFileExist f
108
+ verbose $ f ++ if exists then " exists" else " does not exist"
109
+ if exists then return (Just f) else search verbose fs
110
+
111
+ loadConfigMT :: Verbose -> FilePath -> IO Config
112
+ loadConfigMT verbose currentDir = do
113
+ mbFp <- configFilePathMT verbose currentDir
114
+ verbose $ " Loading configuration at " ++ fromMaybe " <embedded>" mbFp
115
+ bytes <- maybe (return defaultConfigBytes) B. readFile mbFp
116
+ case decodeEither' bytes of
117
+ Left exception -> error $ prettyPrintParseException exception
118
+ Right config -> do
119
+ -- | TODO
120
+ cabalLanguageExtensions <- pure []
121
+
122
+ return $ config
123
+ { configLanguageExtensions = nub $
124
+ configLanguageExtensions config
125
+ }
126
+ where toStr (ext, True ) = show ext
127
+ toStr (ext, False ) = " No" ++ show ext
128
+
129
+
130
+
78
131
-- | Recursively search in every directory of the given filepath for .stylish-haskell.yaml.
79
132
-- If no such file has been found, return default config.
80
133
loadConfigFrom :: FilePath -> IO Config
81
134
loadConfigFrom file = do
82
- currDir <- getCurrentDirectory
83
- setCurrentDirectory (takeDirectory file)
84
- config <- loadConfig (makeVerbose False ) Nothing
85
- setCurrentDirectory currDir
135
+ config <- loadConfigMT (makeVerbose True ) (takeDirectory file)
86
136
pure config
87
137
88
138
-- | Run stylish-haskell on the given text with the given configuration.
0 commit comments