Skip to content

Commit 4d5ce72

Browse files
authored
Make the Ormolu plugin respect .ormolu fixity files (#3449)
when Ormolu ≥0.5.3.0
1 parent 50799fe commit 4d5ce72

File tree

8 files changed

+47
-6
lines changed

8 files changed

+47
-6
lines changed

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

+3
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
@@ -49,3 +51,4 @@ test-suite tests
4951
, hls-ormolu-plugin
5052
, hls-test-utils ^>=1.5
5153
, lsp-types
54+
, ormolu

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

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE LambdaCase #-}
23
{-# LANGUAGE OverloadedStrings #-}
34
{-# LANGUAGE TypeApplications #-}
45

@@ -8,8 +9,10 @@ module Ide.Plugin.Ormolu
89
)
910
where
1011

11-
import Control.Exception (try)
12+
import Control.Exception (Handler (..), IOException,
13+
SomeException (..), catches)
1214
import Control.Monad.IO.Class (liftIO)
15+
import Data.Functor ((<&>))
1316
import qualified Data.Text as T
1417
import Development.IDE hiding (pluginHandlers)
1518
import Development.IDE.GHC.Compat (hsc_dflags, moduleNameString)
@@ -44,13 +47,26 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
4447
fullRegion = RegionIndices Nothing Nothing
4548
rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1)
4649
mkConf o region = defaultConfig { cfgDynOptions = o, cfgRegion = region }
47-
fmt :: T.Text -> Config RegionIndices -> IO (Either OrmoluException T.Text)
48-
fmt cont conf =
50+
fmt :: T.Text -> Config RegionIndices -> IO (Either SomeException T.Text)
51+
fmt cont conf = flip catches handlers $ do
52+
let fp' = fromNormalizedFilePath fp
4953
#if MIN_VERSION_ormolu(0,5,3)
50-
try @OrmoluException $ ormolu conf (fromNormalizedFilePath fp) cont
54+
cabalInfo <- getCabalInfoForSourceFile fp' <&> \case
55+
CabalNotFound -> Nothing
56+
CabalDidNotMention cabalInfo -> Just cabalInfo
57+
CabalFound cabalInfo -> Just cabalInfo
58+
fixityOverrides <- traverse getFixityOverridesForSourceFile cabalInfo
59+
let conf' = refineConfig ModuleSource cabalInfo fixityOverrides conf
60+
cont' = cont
5161
#else
52-
try @OrmoluException $ ormolu conf (fromNormalizedFilePath fp) $ T.unpack cont
62+
let conf' = conf
63+
cont' = T.unpack cont
5364
#endif
65+
Right <$> ormolu conf' fp' cont'
66+
handlers =
67+
[ Handler $ pure . Left . SomeException @OrmoluException
68+
, Handler $ pure . Left . SomeException @IOException
69+
]
5470

5571
case typ of
5672
FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -59,7 +75,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
5975
where
6076
title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp)
6177

62-
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit)
78+
ret :: Either SomeException T.Text -> Either ResponseError (List TextEdit)
6379
ret (Left err) = Left . responseError . T.pack $ "ormoluCmd: " ++ show err
6480
ret (Right new) = Right $ makeDiffTextEdit contents new
6581

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

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE OverloadedStrings #-}
23
module Main
34
( main
@@ -20,6 +21,10 @@ tests = testGroup "ormolu"
2021
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
2122
, goldenWithOrmolu "formats imports correctly" "Ormolu2" "formatted" $ \doc -> do
2223
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
24+
#if MIN_VERSION_ormolu(0,5,3)
25+
, goldenWithOrmolu "formats operators correctly" "Ormolu3" "formatted" $ \doc -> do
26+
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
27+
#endif
2328
]
2429

2530
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)