Skip to content

Commit 95c1654

Browse files
committed
Make the Ormolu plugin respect .ormolu fixity files
1 parent 6cf5774 commit 95c1654

File tree

9 files changed

+47
-5
lines changed

9 files changed

+47
-5
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# TODO Ormolu fixity parser does not understand CRLF
2+
plugins/hls-ormolu-plugin/test/testdata/.ormolu binary

plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ build-type: Simple
1515
extra-source-files:
1616
LICENSE
1717
test/testdata/**/*.hs
18+
test/testdata/.ormolu
19+
test/testdata/test.cabal
1820

1921
source-repository head
2022
type: git

plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE OverloadedStrings #-}
23
{-# LANGUAGE TypeApplications #-}
34

@@ -7,7 +8,8 @@ module Ide.Plugin.Ormolu
78
)
89
where
910

10-
import Control.Exception (try)
11+
import Control.Exception (Handler (..), IOException,
12+
SomeException (..), catches)
1113
import Control.Monad.IO.Class (liftIO)
1214
import qualified Data.Text as T
1315
import Development.IDE hiding (pluginHandlers)
@@ -22,6 +24,11 @@ import Language.LSP.Types
2224
import Ormolu
2325
import System.FilePath (takeFileName)
2426

27+
#if MIN_VERSION_ormolu(0,5,0)
28+
import Ormolu.Utils.Cabal (getCabalInfoForSourceFile)
29+
import Ormolu.Utils.Fixity (getFixityOverridesForSourceFile)
30+
#endif
31+
2532
-- ---------------------------------------------------------------------
2633

2734
descriptor :: PluginId -> PluginDescriptor IdeState
@@ -43,9 +50,21 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
4350
fullRegion = RegionIndices Nothing Nothing
4451
rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1)
4552
mkConf o region = defaultConfig { cfgDynOptions = o, cfgRegion = region }
46-
fmt :: T.Text -> Config RegionIndices -> IO (Either OrmoluException T.Text)
47-
fmt cont conf =
48-
try @OrmoluException $ ormolu conf (fromNormalizedFilePath fp) $ T.unpack cont
53+
fmt :: T.Text -> Config RegionIndices -> IO (Either SomeException T.Text)
54+
fmt cont conf = flip catches handlers $ do
55+
let fp' = fromNormalizedFilePath fp
56+
#if MIN_VERSION_ormolu(0,5,0)
57+
fixityOverrides <-
58+
getFixityOverridesForSourceFile =<< getCabalInfoForSourceFile fp'
59+
let conf' = conf { cfgFixityOverrides = fixityOverrides }
60+
#else
61+
let conf' = conf
62+
#endif
63+
Right <$> ormolu conf' fp' (T.unpack cont)
64+
handlers =
65+
[ Handler $ pure . Left . SomeException @OrmoluException
66+
, Handler $ pure . Left . SomeException @IOException
67+
]
4968

5069
case typ of
5170
FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -54,7 +73,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
5473
where
5574
title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp)
5675

57-
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit)
76+
ret :: Either SomeException T.Text -> Either ResponseError (List TextEdit)
5877
ret (Left err) = Left . responseError . T.pack $ "ormoluCmd: " ++ show err
5978
ret (Right new) = Right $ makeDiffTextEdit contents new
6079

plugins/hls-ormolu-plugin/test/Main.hs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ tests = testGroup "ormolu"
2020
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
2121
, goldenWithOrmolu "formats imports correctly" "Ormolu2" "formatted" $ \doc -> do
2222
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
23+
, goldenWithOrmolu "formats operators correctly" "Ormolu3" "formatted" $ \doc -> do
24+
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
2325
]
2426

2527
goldenWithOrmolu :: TestName -> FilePath -> FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
infixl 7 .=?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
foo :: String
2+
foo =
3+
"a" .=? "b"
4+
<> "c" .=? "d"
5+
<> "e" .=? "f"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
foo :: String
2+
foo =
3+
"a" .=? "b"
4+
<> "c" .=? "d"
5+
<> "e" .=? "f"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo :: String
2+
foo = "a" .=? "b"
3+
<> "c" .=? "d" <> "e" .=? "f"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cabal-version: 3.0
2+
name: test
3+
version: 0

0 commit comments

Comments
 (0)