-
-
Notifications
You must be signed in to change notification settings - Fork 408
Add fourmolu plugin #161
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 fourmolu plugin #161
Changes from 6 commits
6680505
1e743a0
158a279
cee4232
4c76137
825789c
72df5ff
1ccbf34
114aa8a
605d19a
4fe0f7d
3a6874d
96d42f3
d4371fd
75f0d6b
679e0ec
f51d4e8
7a8f51d
5dbf153
530ccb0
5163c41
6b51c7c
3dbfa68
38672e0
bd5e256
26f1e7f
0adf4bd
75d4c62
a308151
d961197
e0ea2fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{-# LANGUAGE PackageImports #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
module Ide.Plugin.Fourmolu | ||
( | ||
descriptor | ||
, provider | ||
) | ||
where | ||
|
||
import Control.Exception | ||
import qualified Data.Text as T | ||
import Development.IDE.Core.Rules | ||
import Development.IDE.Types.Diagnostics as D | ||
import Development.IDE.Types.Location | ||
import qualified DynFlags as D | ||
import qualified EnumSet as S | ||
import GHC | ||
import Ide.Types | ||
import Ide.PluginUtils | ||
import Ide.Plugin.Formatter | ||
import Language.Haskell.LSP.Types | ||
import "fourmolu" Ormolu | ||
import Text.Regex.TDFA.Text() | ||
|
||
-- --------------------------------------------------------------------- | ||
|
||
descriptor :: PluginId -> PluginDescriptor | ||
descriptor plId = (defaultPluginDescriptor plId) | ||
{ pluginFormattingProvider = Just provider | ||
} | ||
|
||
-- --------------------------------------------------------------------- | ||
|
||
provider :: FormattingProvider IO | ||
provider _lf ideState typ contents fp _ = do | ||
let | ||
fromDyn :: ParsedModule -> IO [DynOption] | ||
fromDyn pmod = | ||
let | ||
df = ms_hspp_opts $ pm_mod_summary pmod | ||
pp = | ||
let p = D.sPgm_F $ D.settings df | ||
in if null p then [] else ["-pgmF=" <> p] | ||
pm = map (("-fplugin=" <>) . moduleNameString) $ D.pluginModNames df | ||
ex = map (("-X" <>) . show') $ S.toList $ D.extensionFlags df | ||
show' x = case show x of | ||
"Cpp" -> "CPP" | ||
s -> s | ||
|
||
in | ||
return $ map DynOption $ pp <> pm <> ex | ||
|
||
m_parsed <- runAction "Fourmolu" ideState $ getParsedModule fp | ||
fileOpts <- case m_parsed of | ||
Nothing -> return [] | ||
Just pm -> fromDyn pm | ||
|
||
let | ||
fullRegion = RegionIndices Nothing Nothing | ||
rangeRegion s e = RegionIndices (Just s) (Just e) | ||
mkConf o region = do | ||
printerOpts <- loadConfigFile True (Just fp') defaultPrinterOpts | ||
return $ defaultConfig | ||
{ cfgDynOptions = o | ||
, cfgRegion = region | ||
, cfgDebug = True | ||
, cfgPrinterOpts = printerOpts | ||
} | ||
fmt :: T.Text -> Config RegionIndices -> IO (Either OrmoluException T.Text) | ||
fmt cont conf = | ||
try @OrmoluException (ormolu conf fp' $ T.unpack cont) | ||
fp' = fromNormalizedFilePath fp | ||
|
||
case typ of | ||
FormatText -> ret <$> (fmt contents =<< mkConf fileOpts fullRegion) | ||
FormatRange r -> | ||
let | ||
Range (Position sl _) (Position el _) = normalize r | ||
in | ||
ret <$> (fmt contents =<< mkConf fileOpts (rangeRegion sl el)) | ||
where | ||
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit) | ||
ret (Left err) = Left | ||
(responseError (T.pack $ "fourmoluCmd: " ++ show err) ) | ||
ret (Right new) = Right (makeDiffTextEdit contents new) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -36,6 +36,7 @@ tests = testGroup "format document" [ | |||||
, brittanyTests | ||||||
#endif | ||||||
, ormoluTests | ||||||
, fourmoluTests | ||||||
] | ||||||
|
||||||
rangeTests :: TestTree | ||||||
|
@@ -156,6 +157,17 @@ ormoluTests = testGroup "ormolu" [ | |||||
GHC86 -> "formatted" | ||||||
_ -> "unchanged" | ||||||
|
||||||
fourmoluTests :: TestTree | ||||||
fourmoluTests = testGroup "fourmolu" [ | ||||||
goldenVsStringDiff "formats correctly" goldenGitDiff ("test/testdata/Format.fourmolu.hs") $ runSession hieCommand fullCaps "test/testdata" $ do | ||||||
let formatLspConfig provider = | ||||||
object [ "languageServerHaskell" .= object ["formattingProvider" .= (provider :: Value)] ] | ||||||
|
let formatLspConfig provider = | |
object [ "languageServerHaskell" .= object ["formattingProvider" .= (provider :: Value)] ] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Format where | ||
|
||
foo :: Int -> Int | ||
foo 3 = 2 | ||
foo x = x | ||
bar :: String -> IO String | ||
bar s = do | ||
x <- return "hello" | ||
return "asdf" | ||
|
||
data Baz = Baz {a :: Int, b :: String} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want to remove these before merging.
I've got an eye on the relevant PRs - thanks @bubba for opening them where necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the remaining packages blocked by aeson-1.5?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Floskell got the bump, but there hasn't been a Hackage release with it.
We're still waiting on Stylish.