Skip to content

Commit 32bbafe

Browse files
committed
Make the Ormolu plugin respect .ormolu fixity files
1 parent ddc67b2 commit 32bbafe

File tree

9 files changed

+52
-6
lines changed

9 files changed

+52
-6
lines changed

cabal.project

+7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ packages:
3737
./plugins/hls-explicit-record-fields-plugin
3838
./plugins/hls-refactor-plugin
3939

40+
if impl(ghc == 9.2.*)
41+
-- https://github.com/tweag/ormolu/pull/976
42+
source-repository-package
43+
type: git
44+
location: https://github.com/amesgen/ormolu
45+
tag: 120ea70ed055883b053dc5dc998d9e69efdeccf5
46+
4047
-- Standard location for temporary packages needed for particular environments
4148
-- For example it is used in the project gitlab mirror to help in the MAcOS M1 build script
4249
-- See https://github.com/haskell/haskell-language-server/blob/master/.gitlab-ci.yml

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

+19-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)
@@ -43,9 +45,21 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
4345
fullRegion = RegionIndices Nothing Nothing
4446
rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1)
4547
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
48+
fmt :: T.Text -> Config RegionIndices -> IO (Either SomeException T.Text)
49+
fmt cont conf = flip catches handlers $ do
50+
let fp' = fromNormalizedFilePath fp
51+
#if MIN_VERSION_ormolu(0,6,0)
52+
cabalInfo <- getCabalInfoForSourceFile fp'
53+
fixityOverrides <- getFixityOverridesForSourceFile fp'
54+
let conf' = refineConfig ModuleSource cabalInfo fixityOverrides conf
55+
#else
56+
let conf' = conf
57+
#endif
58+
Right <$> ormolu conf' fp' (T.unpack cont)
59+
handlers =
60+
[ Handler $ pure . Left . SomeException @OrmoluException
61+
, Handler $ pure . Left . SomeException @IOException
62+
]
4963

5064
case typ of
5165
FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -54,7 +68,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
5468
where
5569
title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp)
5670

57-
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit)
71+
ret :: Either SomeException T.Text -> Either ResponseError (List TextEdit)
5872
ret (Left err) = Left . responseError . T.pack $ "ormoluCmd: " ++ show err
5973
ret (Right new) = Right $ makeDiffTextEdit contents new
6074

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,6,0)
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)