Skip to content

Support fourmolu ^>= 0.7 #2944

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
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
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
Comment on lines +12 to +13
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It took me a minute to figure out the Git repository for this package, so I added these fields to the .cabal file. Technically an unrelated change, would be easy to put somewhere else.

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is in the wrong commit, right? Not necessarily actually worth changing now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am regrettably a complete git noob and generally just squash+merge all my stuff 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ha, I'm very much in the "git is a wonderful concept with a horrible CLI" camp. Since I finally got around to setting up good editor integration, I haven't looked back.

, ghc
, ghc-boot-th
, ghcide ^>=1.7
Expand Down
38 changes: 34 additions & 4 deletions plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE CPP #-}

module Ide.Plugin.Fourmolu (
descriptor,
Expand Down Expand Up @@ -34,6 +35,7 @@ import System.FilePath
import System.IO (stderr)
import System.Process.Run (proc, cwd)
import System.Process.Text (readCreateProcessWithExitCode)
import Ormolu.Config

descriptor :: PluginId -> PluginDescriptor IdeState
descriptor plId =
Expand Down Expand Up @@ -78,10 +80,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 +100,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 +114,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
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extra-deps:
- rope-utf16-splay-0.3.2.0
- sqlite-simple-0.4.18.0@sha256:3ceea56375c0a3590c814e411a4eb86943f8d31b93b110ca159c90689b6b39e5,3002
- SVGFonts-1.7.0.1 # for Chart-diagrams, https://github.com/timbod7/haskell-chart/issues/232
- fourmolu-0.7.0.1


# currently needed for ghcide>extra, etc.
Expand Down