Skip to content

Commit 9b84ba7

Browse files
committed
AbstractPath and hls-stylish-plugin refactor
the loadConfig function calls setCurrentDirectory and getCurrentDirectory for recursively searching the current dir for `.stylish_haskell.yaml`, this has been replaced by a function which directly chooses the parent directory of the file as the currentDirectory, the `search` function nonetheless looks recursively outwards. TODO: cabalLanguageExtensions parsing support
1 parent a6b9611 commit 9b84ba7

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

ghcide/ghcide.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ library
129129
exposed-modules:
130130
Control.Concurrent.Strict
131131
Development.IDE
132+
Development.IDE.Core.AbstractPath
132133
Development.IDE.Core.Actions
133134
Development.IDE.Core.Compile
134135
Development.IDE.Core.Debouncer
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Development.IDE.Core.AbstractPath where
2+
3+
import System.FilePath
4+
5+
data AbstractPath = RelativePath FilePath
6+
| AbsolutePath FilePath
7+
deriving (Show)
8+
9+
mkAbstract :: FilePath -> AbstractPath
10+
mkAbstract x | isRelative x = RelativePath x
11+
| otherwise = AbsolutePath x

haskell-language-server.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,7 @@ library hls-stylish-haskell-plugin
15781578
hs-source-dirs: plugins/hls-stylish-haskell-plugin/src
15791579
build-depends:
15801580
, base >=4.12 && <5
1581+
, bytestring
15811582
, directory
15821583
, filepath
15831584
, ghc-boot-th
@@ -1587,6 +1588,7 @@ library hls-stylish-haskell-plugin
15871588
, mtl
15881589
, stylish-haskell ^>=0.12 || ^>=0.13 || ^>=0.14
15891590
, text
1591+
, yaml
15901592

15911593

15921594
test-suite hls-stylish-haskell-plugin-tests

plugins/hls-stylish-haskell-plugin/src/Ide/Plugin/StylishHaskell.hs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ where
1111

1212
import Control.Monad.Except (throwError)
1313
import Control.Monad.IO.Class
14+
import Data.ByteString as B
15+
import Data.List (inits, nub)
16+
import Data.Maybe
1417
import Data.Text (Text)
1518
import qualified Data.Text as T
19+
import Data.Yaml
20+
import Debug.Trace
1621
import Development.IDE hiding (getExtensions,
1722
pluginHandlers)
1823
import Development.IDE.Core.PluginUtils
@@ -26,8 +31,11 @@ import Ide.Types hiding (Config)
2631
import Language.Haskell.Stylish
2732
import Language.LSP.Protocol.Types as LSP
2833
import System.Directory
34+
2935
import System.FilePath
3036

37+
38+
3139
data Log
3240
= LogLanguageExtensionFromDynFlags
3341

@@ -61,7 +69,7 @@ provider recorder ide _token typ contents fp _opts = do
6169
Right new -> pure $ LSP.InL [TextEdit range new]
6270
where
6371
getMergedConfig dyn config
64-
| null (configLanguageExtensions config)
72+
| Prelude.null (configLanguageExtensions config)
6573
= do
6674
logWith recorder Info LogLanguageExtensionFromDynFlags
6775
pure
@@ -70,19 +78,61 @@ provider recorder ide _token typ contents fp _opts = do
7078
| otherwise
7179
= pure config
7280

73-
getExtensions = map showExtension . Util.toList . extensionFlags
81+
getExtensions = Prelude.map showExtension . Util.toList . extensionFlags
7482

7583
showExtension Cpp = "CPP"
7684
showExtension other = show other
7785

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+
78131
-- | Recursively search in every directory of the given filepath for .stylish-haskell.yaml.
79132
-- If no such file has been found, return default config.
80133
loadConfigFrom :: FilePath -> IO Config
81134
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)
86136
pure config
87137

88138
-- | Run stylish-haskell on the given text with the given configuration.

0 commit comments

Comments
 (0)