Skip to content

Commit 378c444

Browse files
authored
Merge pull request #455 from georgefst/fourmolu-3
Update Fourmolu to 0.2
2 parents 2b72891 + e4bb856 commit 378c444

12 files changed

+109
-88
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ package ghcide
2020

2121
write-ghc-environment-files: never
2222

23-
index-state: 2020-09-30T21:52:43Z
23+
index-state: 2020-10-02T22:25:53Z
2424

2525
allow-newer: data-tree-print:base

haskell-language-server.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ executable haskell-language-server
124124
, containers
125125
, deepseq
126126
, floskell ^>=0.10
127-
, fourmolu ^>=0.1
127+
, fourmolu ^>=0.2
128128
, ghc
129129
, ghc-boot-th
130130
, ghcide >=0.1
@@ -155,7 +155,7 @@ executable haskell-language-server
155155
, transformers
156156
, unordered-containers
157157
, ghc-source-gen
158-
, refinery >=0.2.0.0
158+
, refinery ^>=0.2
159159
, ghc-exactprint
160160
, fingertree
161161

Lines changed: 96 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,111 @@
1-
{-# LANGUAGE OverloadedStrings #-}
2-
{-# LANGUAGE PackageImports #-}
3-
{-# LANGUAGE RecordWildCards #-}
4-
{-# LANGUAGE ScopedTypeVariables #-}
5-
{-# LANGUAGE TypeApplications #-}
1+
{-# LANGUAGE LambdaCase #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE PackageImports #-}
4+
{-# LANGUAGE RecordWildCards #-}
5+
{-# LANGUAGE TypeApplications #-}
66

7-
module Ide.Plugin.Fourmolu
8-
(
9-
descriptor
10-
, provider
11-
)
12-
where
7+
module Ide.Plugin.Fourmolu (
8+
descriptor,
9+
provider,
10+
) where
1311

14-
import Control.Exception
15-
import qualified Data.Text as T
16-
import Development.IDE as D
17-
import qualified DynFlags as D
18-
import qualified EnumSet as S
19-
import GHC
20-
import GHC.LanguageExtensions.Type
21-
import GhcPlugins (HscEnv (hsc_dflags))
22-
import Ide.Plugin.Formatter
23-
import Ide.PluginUtils
24-
import Ide.Types
25-
import Language.Haskell.LSP.Core (LspFuncs (withIndefiniteProgress),
26-
ProgressCancellable (Cancellable))
27-
import Language.Haskell.LSP.Types
12+
import Control.Exception
13+
import Data.Either.Extra
14+
import System.FilePath
15+
16+
import Control.Lens ((^.))
17+
import qualified Data.Text as T
18+
import Development.IDE as D
19+
import qualified DynFlags as D
20+
import qualified EnumSet as S
21+
import GHC (DynFlags, moduleNameString)
22+
import GHC.LanguageExtensions.Type (Extension (Cpp))
23+
import GhcPlugins (HscEnv (hsc_dflags))
24+
import Ide.Plugin.Formatter (responseError)
25+
import Ide.PluginUtils (makeDiffTextEdit)
26+
import Language.Haskell.LSP.Messages (FromServerMessage (ReqShowMessage))
27+
28+
import Ide.Types
29+
import Language.Haskell.LSP.Core
30+
import Language.Haskell.LSP.Types
31+
import Language.Haskell.LSP.Types.Lens
2832
import "fourmolu" Ormolu
29-
import System.FilePath (takeFileName)
30-
import Text.Regex.TDFA.Text ()
3133

3234
-- ---------------------------------------------------------------------
3335

3436
descriptor :: PluginId -> PluginDescriptor
35-
descriptor plId = (defaultPluginDescriptor plId)
36-
{ pluginFormattingProvider = Just provider
37-
}
37+
descriptor plId =
38+
(defaultPluginDescriptor plId)
39+
{ pluginFormattingProvider = Just provider
40+
}
3841

3942
-- ---------------------------------------------------------------------
4043

4144
provider :: FormattingProvider IO
42-
provider lf ideState typ contents fp _ = withIndefiniteProgress lf title Cancellable $ do
43-
let
44-
fromDyn :: DynFlags -> IO [DynOption]
45-
fromDyn df =
46-
let
47-
pp =
48-
let p = D.sPgm_F $ D.settings df
49-
in if null p then [] else ["-pgmF=" <> p]
50-
pm = map (("-fplugin=" <>) . moduleNameString) $ D.pluginModNames df
51-
ex = map showExtension $ S.toList $ D.extensionFlags df
52-
in
53-
return $ map DynOption $ pp <> pm <> ex
45+
provider lf ideState typ contents fp fo = withIndefiniteProgress lf title Cancellable $ do
46+
ghc <- runAction "Fourmolu" ideState $ use GhcSession fp
47+
fileOpts <- case hsc_dflags . hscEnv <$> ghc of
48+
Nothing -> return []
49+
Just df -> convertDynFlags df
5450

55-
ghc <- runAction "Fourmolu" ideState $ use GhcSession fp
56-
let df = hsc_dflags . hscEnv <$> ghc
57-
fileOpts <- case df of
58-
Nothing -> return []
59-
Just df -> fromDyn df
51+
let format printerOpts =
52+
mapLeft (responseError . ("Fourmolu: " <>) . T.pack . show)
53+
<$> try @OrmoluException (makeDiffTextEdit contents <$> ormolu config fp' (T.unpack contents))
54+
where
55+
config =
56+
defaultConfig
57+
{ cfgDynOptions = fileOpts
58+
, cfgRegion = region
59+
, cfgDebug = True
60+
, cfgPrinterOpts =
61+
fillMissingPrinterOpts
62+
(lspPrinterOpts <> printerOpts)
63+
defaultPrinterOpts
64+
}
6065

61-
let
62-
fullRegion = RegionIndices Nothing Nothing
63-
rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1)
64-
mkConf o region = do
65-
printerOpts <- loadConfigFile True (Just fp') defaultPrinterOpts
66-
return $ defaultConfig
67-
{ cfgDynOptions = o
68-
, cfgRegion = region
69-
, cfgDebug = True
70-
, cfgPrinterOpts = printerOpts
71-
}
72-
fmt :: T.Text -> Config RegionIndices -> IO (Either OrmoluException T.Text)
73-
fmt cont conf =
74-
try @OrmoluException (ormolu conf fp' $ T.unpack cont)
66+
loadConfigFile fp' >>= \case
67+
ConfigLoaded file opts -> do
68+
putStrLn $ "Loaded Fourmolu config from: " <> file
69+
format opts
70+
ConfigNotFound searchDirs -> do
71+
putStrLn
72+
. unlines
73+
$ ("No " ++ show configFileName ++ " found in any of:") :
74+
map (" " ++) searchDirs
75+
format mempty
76+
ConfigParseError f (_, err) -> do
77+
sendFunc lf . ReqShowMessage $
78+
RequestMessage
79+
{ _jsonrpc = ""
80+
, _id = IdString "fourmolu"
81+
, _method = WindowShowMessageRequest
82+
, _params =
83+
ShowMessageRequestParams
84+
{ _xtype = MtError
85+
, _message = errorMessage
86+
, _actions = Nothing
87+
}
88+
}
89+
return . Left $ responseError errorMessage
90+
where
91+
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack err
92+
where
7593
fp' = fromNormalizedFilePath fp
94+
title = "Formatting " <> T.pack (takeFileName fp')
95+
lspPrinterOpts = mempty{poIndentation = Just $ fo ^. tabSize}
96+
region = case typ of
97+
FormatText ->
98+
RegionIndices Nothing Nothing
99+
FormatRange (Range (Position sl _) (Position el _)) ->
100+
RegionIndices (Just $ sl + 1) (Just $ el + 1)
76101

77-
case typ of
78-
FormatText -> ret <$> (fmt contents =<< mkConf fileOpts fullRegion)
79-
FormatRange (Range (Position sl _) (Position el _)) ->
80-
ret <$> (fmt contents =<< mkConf fileOpts (rangeRegion sl el))
81-
where
82-
title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp)
83-
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit)
84-
ret (Left err) = Left
85-
(responseError (T.pack $ "fourmoluCmd: " ++ show err) )
86-
ret (Right new) = Right (makeDiffTextEdit contents new)
87-
88-
showExtension :: Extension -> String
89-
showExtension Cpp = "-XCPP"
90-
showExtension other = "-X" ++ show other
102+
convertDynFlags :: DynFlags -> IO [DynOption]
103+
convertDynFlags df =
104+
let pp = if null p then [] else ["-pgmF=" <> p]
105+
p = D.sPgm_F $ D.settings df
106+
pm = map (("-fplugin=" <>) . moduleNameString) $ D.pluginModNames df
107+
ex = map showExtension $ S.toList $ D.extensionFlags df
108+
showExtension = \case
109+
Cpp -> "-XCPP"
110+
x -> "-X" ++ show x
111+
in return $ map DynOption $ pp <> pm <> ex

stack-8.10.1.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extra-deps:
1616
- clock-0.7.2
1717
- data-tree-print-0.1.0.2
1818
- floskell-0.10.4
19-
- fourmolu-0.1.0.0@rev:1
19+
- fourmolu-0.2.0.0
2020
- HsYAML-aeson-0.2.0.0@rev:2
2121
- monad-dijkstra-0.1.1.2
2222
- opentelemetry-0.4.2

stack-8.10.2.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extra-deps:
1717
- clock-0.7.2
1818
- data-tree-print-0.1.0.2
1919
- floskell-0.10.4
20-
- fourmolu-0.1.0.0@rev:1
20+
- fourmolu-0.2.0.0
2121
- HsYAML-aeson-0.2.0.0@rev:2
2222
- monad-dijkstra-0.1.1.2
2323
- opentelemetry-0.4.2

stack-8.6.4.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extra-deps:
2121
- clock-0.7.2
2222
- extra-1.7.3
2323
- floskell-0.10.4
24-
- fourmolu-0.1.0.0@rev:1
24+
- fourmolu-0.2.0.0
2525
- fuzzy-0.1.0.0
2626
# - ghcide-0.1.0
2727
- ghc-check-0.5.0.1

stack-8.6.5.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extra-deps:
2020
- clock-0.7.2
2121
- extra-1.7.3
2222
- floskell-0.10.4
23-
- fourmolu-0.1.0.0@rev:1
23+
- fourmolu-0.2.0.0
2424
- fuzzy-0.1.0.0
2525
# - ghcide-0.1.0
2626
- ghc-check-0.5.0.1

stack-8.8.2.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extra-deps:
1919
- constrained-dynamic-0.1.0.0
2020
- extra-1.7.3
2121
- floskell-0.10.4
22-
- fourmolu-0.1.0.0@rev:1
22+
- fourmolu-0.2.0.0
2323
# - ghcide-0.1.0
2424
- ghc-check-0.5.0.1
2525
- ghc-lib-parser-8.10.1.20200523

stack-8.8.3.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extra-deps:
1919
- constrained-dynamic-0.1.0.0
2020
- extra-1.7.3
2121
- floskell-0.10.4
22-
- fourmolu-0.1.0.0@rev:1
22+
- fourmolu-0.2.0.0
2323
# - ghcide-0.1.0
2424
- haskell-src-exts-1.21.1
2525
- hlint-2.2.8

stack-8.8.4.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extra-deps:
2121
- constrained-dynamic-0.1.0.0
2222
- extra-1.7.3
2323
- floskell-0.10.4
24-
- fourmolu-0.1.0.0@rev:1
24+
- fourmolu-0.2.0.0
2525
# - ghcide-0.1.0
2626
- haskell-src-exts-1.21.1
2727
- hie-bios-0.7.1

stack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extra-deps:
2020
- clock-0.7.2
2121
- extra-1.7.3
2222
- floskell-0.10.4
23-
- fourmolu-0.1.0.0@rev:1
23+
- fourmolu-0.2.0.0
2424
- fuzzy-0.1.0.0
2525
# - ghcide-0.1.0
2626
- ghc-check-0.5.0.1

test/testdata/Format.fourmolu.formatted.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module Format where
22

3-
import Data.Int
43
import Data.List
4+
5+
import Data.Int
56
import Prelude
67

78
foo :: Int -> Int
89
foo 3 = 2
910
foo x = x
10-
1111
bar :: String -> IO String
1212
bar s = do
1313
x <- return "hello"

0 commit comments

Comments
 (0)