Skip to content

Commit 425763a

Browse files
Consolidate shims
1 parent d965e02 commit 425763a

File tree

3 files changed

+77
-34
lines changed

3 files changed

+77
-34
lines changed

plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ source-repository head
2323
location: git://github.com/haskell/haskell-language-server.git
2424

2525
library
26-
exposed-modules: Ide.Plugin.Fourmolu
26+
exposed-modules:
27+
Ide.Plugin.Fourmolu
28+
, Ide.Plugin.Fourmolu.Shim
2729
hs-source-dirs: src
2830
ghc-options: -Wall
2931
build-depends:

plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# LANGUAGE CPP #-}
21
{-# LANGUAGE DataKinds #-}
32
{-# LANGUAGE DisambiguateRecordFields #-}
43
{-# LANGUAGE LambdaCase #-}
@@ -25,6 +24,7 @@ import Development.IDE.GHC.Compat as Compat hiding (Cpp, Warning,
2524
hang, vcat)
2625
import qualified Development.IDE.GHC.Compat.Util as S
2726
import GHC.LanguageExtensions.Type (Extension (Cpp))
27+
import Ide.Plugin.Fourmolu.Shim
2828
import Ide.Plugin.Properties
2929
import Ide.PluginUtils (makeDiffTextEdit,
3030
usePropertyLsp)
@@ -33,7 +33,6 @@ import Language.LSP.Server hiding (defaultConfig)
3333
import Language.LSP.Types hiding (line)
3434
import Language.LSP.Types.Lens (HasTabSize (tabSize))
3535
import Ormolu
36-
import Ormolu.Config
3736
import System.Exit
3837
import System.FilePath
3938
import System.Process.Run (cwd, proc)
@@ -103,14 +102,9 @@ provider recorder plId ideState typ contents fp fo = withIndefiniteProgress titl
103102
bimap (mkError . show) (makeDiffTextEdit contents)
104103
<$> try @OrmoluException (ormolu config fp' (T.unpack contents))
105104
where
106-
printerOpts =
107-
#if MIN_VERSION_fourmolu(0,7,0)
108-
cfgFilePrinterOpts fourmoluConfig
109-
#else
110-
fourmoluConfig
111-
112-
#endif
105+
printerOpts = cfgFilePrinterOpts fourmoluConfig
113106
config =
107+
addFixityOverrides (cfgFileFixities fourmoluConfig) $
114108
defaultConfig
115109
{ cfgDynOptions = map DynOption fileOpts
116110
, cfgRegion = region
@@ -119,29 +113,14 @@ provider recorder plId ideState typ contents fp fo = withIndefiniteProgress titl
119113
fillMissingPrinterOpts
120114
(printerOpts <> lspPrinterOpts)
121115
defaultPrinterOpts
122-
#if MIN_VERSION_fourmolu(0,7,0)
123-
, cfgFixityOverrides =
124-
cfgFileFixities fourmoluConfig
125-
#endif
126116
}
127117
in liftIO (loadConfigFile fp') >>= \case
128118
ConfigLoaded file opts -> liftIO $ do
129119
logWith recorder Info $ ConfigPath file
130-
format opts
120+
format (toConfig opts)
131121
ConfigNotFound searchDirs -> liftIO $ do
132122
logWith recorder Info $ NoConfigPath searchDirs
133-
format emptyOptions
134-
where
135-
emptyOptions =
136-
#if MIN_VERSION_fourmolu(0,7,0)
137-
FourmoluConfig
138-
{ cfgFilePrinterOpts = mempty
139-
, cfgFileFixities = mempty
140-
}
141-
#else
142-
mempty
143-
#endif
144-
123+
format emptyConfig
145124
ConfigParseError f err -> do
146125
sendNotification SWindowShowMessage $
147126
ShowMessageParams
@@ -150,13 +129,7 @@ provider recorder plId ideState typ contents fp fo = withIndefiniteProgress titl
150129
}
151130
return . Left $ responseError errorMessage
152131
where
153-
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (convertErr err)
154-
convertErr =
155-
#if MIN_VERSION_fourmolu(0,7,0)
156-
show
157-
#else
158-
snd
159-
#endif
132+
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (showParseError err)
160133
where
161134
fp' = fromNormalizedFilePath fp
162135
title = "Formatting " <> T.pack (takeFileName fp')
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{-# LANGUAGE CPP #-}
2+
3+
module Ide.Plugin.Fourmolu.Shim (
4+
-- * FourmoluConfig
5+
FourmoluConfig (..),
6+
toConfig,
7+
emptyConfig,
8+
9+
-- * FixityMap
10+
addFixityOverrides,
11+
12+
-- * ConfigParseError
13+
showParseError,
14+
) where
15+
16+
import Ormolu.Config
17+
18+
#if MIN_VERSION_fourmolu(0,7,0)
19+
import Ormolu.Fixity
20+
#endif
21+
22+
{-- Backport FourmoluConfig --}
23+
24+
#if MIN_VERSION_fourmolu(0,7,0)
25+
toConfig :: FourmoluConfig -> FourmoluConfig
26+
toConfig = id
27+
#else
28+
data FourmoluConfig = FourmoluConfig
29+
{ cfgFilePrinterOpts :: PrinterOptsPartial
30+
, cfgFileFixities :: FixityMap
31+
}
32+
33+
toConfig :: PrinterOptsPartial -> FourmoluConfig
34+
toConfig opts =
35+
FourmoluConfig
36+
{ cfgFilePrinterOpts = opts
37+
, cfgFileFixities = mempty
38+
}
39+
#endif
40+
41+
emptyConfig :: FourmoluConfig
42+
emptyConfig =
43+
FourmoluConfig
44+
{ cfgFilePrinterOpts = mempty
45+
, cfgFileFixities = mempty
46+
}
47+
48+
{-- Backport FixityMap --}
49+
50+
#if MIN_VERSION_fourmolu(0,7,0)
51+
addFixityOverrides :: FixityMap -> Config region -> Config region
52+
addFixityOverrides fixities cfg = cfg{cfgFixityOverrides = fixities}
53+
#else
54+
type FixityMap = ()
55+
56+
addFixityOverrides :: FixityMap -> Config region -> Config region
57+
addFixityOverrides _ = id
58+
#endif
59+
60+
{-- Backport ConfigParseError --}
61+
62+
#if MIN_VERSION_fourmolu(0,7,0)
63+
showParseError :: Show parseException => parseException -> String
64+
showParseError = show
65+
#else
66+
showParseError :: (pos, String) -> String
67+
showParseError = snd
68+
#endif

0 commit comments

Comments
 (0)