Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Fourmolu 0.8 #3078

Merged
merged 4 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ source-repository head
location: git://github.com/haskell/haskell-language-server.git

library
exposed-modules: Ide.Plugin.Fourmolu
exposed-modules:
Ide.Plugin.Fourmolu
, Ide.Plugin.Fourmolu.Shim
hs-source-dirs: src
ghc-options: -Wall
build-depends:
Expand Down
39 changes: 6 additions & 33 deletions plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
Expand All @@ -25,6 +24,7 @@ import Development.IDE.GHC.Compat as Compat hiding (Cpp, Warning,
hang, vcat)
import qualified Development.IDE.GHC.Compat.Util as S
import GHC.LanguageExtensions.Type (Extension (Cpp))
import Ide.Plugin.Fourmolu.Shim
import Ide.Plugin.Properties
import Ide.PluginUtils (makeDiffTextEdit,
usePropertyLsp)
Expand All @@ -33,7 +33,6 @@ import Language.LSP.Server hiding (defaultConfig)
import Language.LSP.Types hiding (line)
import Language.LSP.Types.Lens (HasTabSize (tabSize))
import Ormolu
import Ormolu.Config
import System.Exit
import System.FilePath
import System.Process.Run (cwd, proc)
Expand Down Expand Up @@ -103,14 +102,9 @@ provider recorder plId ideState typ contents fp fo = withIndefiniteProgress titl
bimap (mkError . show) (makeDiffTextEdit contents)
<$> try @OrmoluException (ormolu config fp' (T.unpack contents))
Copy link
Collaborator

@georgefst georgefst Aug 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Surprised I didn't notice that opportunity.

where
printerOpts =
#if MIN_VERSION_fourmolu(0,7,0)
cfgFilePrinterOpts fourmoluConfig
#else
fourmoluConfig

#endif
printerOpts = cfgFilePrinterOpts fourmoluConfig
config =
addFixityOverrides (cfgFileFixities fourmoluConfig) $
defaultConfig
{ cfgDynOptions = map DynOption fileOpts
, cfgRegion = region
Expand All @@ -119,29 +113,14 @@ provider recorder plId ideState typ contents fp fo = withIndefiniteProgress titl
fillMissingPrinterOpts
(printerOpts <> lspPrinterOpts)
defaultPrinterOpts
#if MIN_VERSION_fourmolu(0,7,0)
, cfgFixityOverrides =
cfgFileFixities fourmoluConfig
#endif
}
in liftIO (loadConfigFile fp') >>= \case
ConfigLoaded file opts -> liftIO $ do
logWith recorder Info $ ConfigPath file
format opts
format (toConfig opts)
ConfigNotFound searchDirs -> liftIO $ do
logWith recorder Info $ NoConfigPath searchDirs
format emptyOptions
where
emptyOptions =
#if MIN_VERSION_fourmolu(0,7,0)
FourmoluConfig
{ cfgFilePrinterOpts = mempty
, cfgFileFixities = mempty
}
#else
mempty
#endif

format emptyConfig
ConfigParseError f err -> do
sendNotification SWindowShowMessage $
ShowMessageParams
Expand All @@ -150,13 +129,7 @@ provider recorder plId ideState typ contents fp fo = withIndefiniteProgress titl
}
return . Left $ responseError errorMessage
where
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (convertErr err)
convertErr =
#if MIN_VERSION_fourmolu(0,7,0)
show
#else
snd
#endif
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (showParseError err)
where
fp' = fromNormalizedFilePath fp
title = "Formatting " <> T.pack (takeFileName fp')
Expand Down
68 changes: 68 additions & 0 deletions plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu/Shim.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{-# LANGUAGE CPP #-}

module Ide.Plugin.Fourmolu.Shim (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent. This is much cleaner.

-- * FourmoluConfig
FourmoluConfig (..),
toConfig,
emptyConfig,

-- * FixityMap
addFixityOverrides,

-- * ConfigParseError
showParseError,
) where

import Ormolu.Config

#if MIN_VERSION_fourmolu(0,7,0)
import Ormolu.Fixity
#endif

{-- Backport FourmoluConfig --}

#if MIN_VERSION_fourmolu(0,7,0)
toConfig :: FourmoluConfig -> FourmoluConfig
toConfig = id
#else
data FourmoluConfig = FourmoluConfig
{ cfgFilePrinterOpts :: PrinterOptsPartial
, cfgFileFixities :: FixityMap
}

toConfig :: PrinterOptsPartial -> FourmoluConfig
toConfig opts =
FourmoluConfig
{ cfgFilePrinterOpts = opts
, cfgFileFixities = mempty
}
#endif

emptyConfig :: FourmoluConfig
emptyConfig =
FourmoluConfig
{ cfgFilePrinterOpts = mempty
, cfgFileFixities = mempty
}
Comment on lines +35 to +40
Copy link
Contributor Author

@brandonchinn178 brandonchinn178 Aug 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

condition will eventually be #if !MIN_VERSION_fourmolu(0,8,1): fourmolu/fourmolu#221


{-- Backport FixityMap --}

#if MIN_VERSION_fourmolu(0,7,0)
addFixityOverrides :: FixityMap -> Config region -> Config region
addFixityOverrides fixities cfg = cfg{cfgFixityOverrides = fixities}
#else
type FixityMap = ()

addFixityOverrides :: FixityMap -> Config region -> Config region
addFixityOverrides _ = id
#endif

{-- Backport ConfigParseError --}

#if MIN_VERSION_fourmolu(0,7,0)
showParseError :: Show parseException => parseException -> String
showParseError = show
#else
showParseError :: (pos, String) -> String
showParseError = snd
#endif