Skip to content

Add support for Fourmolu 0.8 #3078

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

Merged
merged 4 commits into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ 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:
, base >=4.12 && <5
, filepath
, fourmolu ^>=0.3 || ^>=0.4 || ^>= 0.6 || ^>= 0.7
, fourmolu ^>=0.3 || ^>=0.4 || ^>= 0.6 || ^>= 0.7 || ^>= 0.8
, ghc
, ghc-boot-th
, ghcide ^>=1.7
Expand Down
43 changes: 8 additions & 35 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 @@ -16,7 +15,7 @@ import Control.Exception (IOException, try)
import Control.Lens ((^.))
import Control.Monad
import Control.Monad.IO.Class
import Data.Bifunctor (first)
import Data.Bifunctor (bimap, first)
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
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 @@ -100,17 +99,12 @@ provider recorder plId ideState typ contents fp fo = withIndefiniteProgress titl
pure . Left . responseError $ "Fourmolu failed with exit code " <> T.pack (show n)
else do
let format fourmoluConfig =
first (mkError . show)
<$> try @OrmoluException (makeDiffTextEdit contents <$> ormolu config fp' (T.unpack contents))
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
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
66 changes: 66 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,66 @@
{-# 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
cfgFilePrinterOpts,
cfgFileFixities,
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)
type FourmoluConfig = PrinterOptsPartial

cfgFilePrinterOpts :: FourmoluConfig -> PrinterOptsPartial
cfgFilePrinterOpts = id

cfgFileFixities :: FourmoluConfig -> FixityMap
cfgFileFixities _ = mempty
#endif

#if MIN_VERSION_fourmolu(0,7,0)
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

#else
emptyConfig :: FourmoluConfig
emptyConfig = mempty
#endif

{-- 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