Skip to content

Commit

Permalink
Support fourmolu ^>= 0.7 (#2944)
Browse files Browse the repository at this point in the history
  • Loading branch information
parsonsmatt authored Jun 11, 2022
1 parent 140f904 commit bb6b4e1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
8 changes: 7 additions & 1 deletion plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@ license: Apache-2.0
license-file: LICENSE
author: The Haskell IDE Team
copyright: The Haskell IDE Team
homepage: https://github.com/haskell/haskell-language-server
bug-reports: https://github.com/haskell/haskell-language-server/issues
maintainer: [email protected]
category: Development
build-type: Simple
extra-source-files:
LICENSE
test/testdata/**/*.hs

source-repository head
type: git
location: git://github.com/haskell/haskell-language-server.git

library
exposed-modules: Ide.Plugin.Fourmolu
hs-source-dirs: src
ghc-options: -Wall
build-depends:
, base >=4.12 && <5
, filepath
, fourmolu ^>=0.3 || ^>=0.4 || ^>= 0.6
, fourmolu ^>=0.3 || ^>=0.4 || ^>= 0.6 || ^>= 0.7
, ghc
, ghc-boot-th
, ghcide ^>=1.7
Expand Down
47 changes: 39 additions & 8 deletions plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedLabels #-}

module Ide.Plugin.Fourmolu (
descriptor,
Expand All @@ -23,16 +24,18 @@ import Development.IDE.GHC.Compat as Compat hiding (Cpp)
import qualified Development.IDE.GHC.Compat.Util as S
import GHC.LanguageExtensions.Type (Extension (Cpp))
import Ide.Plugin.Properties
import Ide.PluginUtils (makeDiffTextEdit, usePropertyLsp)
import Ide.PluginUtils (makeDiffTextEdit,
usePropertyLsp)
import Ide.Types
import Language.LSP.Server hiding (defaultConfig)
import Language.LSP.Types
import Language.LSP.Types.Lens (HasTabSize (tabSize))
import Ormolu
import Ormolu.Config
import System.Exit
import System.FilePath
import System.IO (stderr)
import System.Process.Run (proc, cwd)
import System.Process.Run (cwd, proc)
import System.Process.Text (readCreateProcessWithExitCode)

descriptor :: PluginId -> PluginDescriptor IdeState
Expand Down Expand Up @@ -78,10 +81,17 @@ provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancell
ExitFailure n ->
pure . Left . responseError $ "Fourmolu failed with exit code " <> T.pack (show n)
else do
let format printerOpts =
let format fourmoluConfig =
first (mkError . show)
<$> try @OrmoluException (makeDiffTextEdit contents <$> ormolu config fp' (T.unpack contents))
where
printerOpts =
#if MIN_VERSION_fourmolu(0,7,0)
cfgFilePrinterOpts fourmoluConfig
#else
fourmoluConfig

#endif
config =
defaultConfig
{ cfgDynOptions = map DynOption fileOpts
Expand All @@ -91,6 +101,10 @@ provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancell
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
Expand All @@ -101,16 +115,33 @@ provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancell
. unlines
$ ("No " ++ show configFileName ++ " found in any of:") :
map (" " ++) searchDirs
format mempty
ConfigParseError f (_, err) -> do
format emptyOptions
where
emptyOptions =
#if MIN_VERSION_fourmolu(0,7,0)
FourmoluConfig
{ cfgFilePrinterOpts = mempty
, cfgFileFixities = mempty
}
#else
mempty
#endif

ConfigParseError f err -> do
sendNotification SWindowShowMessage $
ShowMessageParams
{ _xtype = MtError
, _message = errorMessage
}
return . Left $ responseError errorMessage
where
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack err
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (convertErr err)
convertErr =
#if MIN_VERSION_fourmolu(0,7,0)
show
#else
snd
#endif
where
fp' = fromNormalizedFilePath fp
title = "Formatting " <> T.pack (takeFileName fp')
Expand Down

0 comments on commit bb6b4e1

Please sign in to comment.