Skip to content

Commit 0c958da

Browse files
committed
Make the Ormolu plugin respect .ormolu fixity files
1 parent 00f4e61 commit 0c958da

File tree

9 files changed

+53
-7
lines changed

9 files changed

+53
-7
lines changed

cabal.project

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ package *
5050

5151
write-ghc-environment-files: never
5252

53-
index-state: 2023-01-10T00:00:00Z
53+
index-state: 2023-01-27T00:00:00Z
5454

5555
constraints:
5656
-- For GHC 9.4, older versions of entropy fail to build on Windows

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

+4-1
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
@@ -32,7 +34,7 @@ library
3234
, hls-plugin-api ^>=1.3 || ^>=1.4 || ^>= 1.5 || ^>= 1.6
3335
, lens
3436
, lsp
35-
, ormolu ^>=0.1.2 || ^>= 0.2 || ^>= 0.3 || ^>= 0.5
37+
, ormolu ^>=0.1.2 || ^>= 0.2 || ^>= 0.3 || ^>= 0.5 || ^>= 0.6
3638
, text
3739

3840
default-language: Haskell2010
@@ -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

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE LambdaCase #-}
13
{-# LANGUAGE OverloadedStrings #-}
24
{-# LANGUAGE TypeApplications #-}
35

@@ -7,8 +9,10 @@ module Ide.Plugin.Ormolu
79
)
810
where
911

10-
import Control.Exception (try)
12+
import Control.Exception (Handler (..), IOException,
13+
SomeException (..), catches)
1114
import Control.Monad.IO.Class (liftIO)
15+
import Data.Functor ((<&>))
1216
import qualified Data.Text as T
1317
import Development.IDE hiding (pluginHandlers)
1418
import Development.IDE.GHC.Compat (hsc_dflags, moduleNameString)
@@ -43,9 +47,26 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
4347
fullRegion = RegionIndices Nothing Nothing
4448
rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1)
4549
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
50+
fmt :: T.Text -> Config RegionIndices -> IO (Either SomeException T.Text)
51+
fmt cont conf = flip catches handlers $ do
52+
let fp' = fromNormalizedFilePath fp
53+
#if MIN_VERSION_ormolu(0,5,3)
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
61+
#else
62+
let conf' = conf
63+
cont' = T.unpack cont
64+
#endif
65+
Right <$> ormolu conf' fp' cont'
66+
handlers =
67+
[ Handler $ pure . Left . SomeException @OrmoluException
68+
, Handler $ pure . Left . SomeException @IOException
69+
]
4970

5071
case typ of
5172
FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -54,7 +75,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
5475
where
5576
title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp)
5677

57-
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit)
78+
ret :: Either SomeException T.Text -> Either ResponseError (List TextEdit)
5879
ret (Left err) = Left . responseError . T.pack $ "ormoluCmd: " ++ show err
5980
ret (Right new) = Right $ makeDiffTextEdit contents new
6081

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)