Skip to content

Make the Ormolu plugin respect .ormolu fixity files when Ormolu ≥0.5.3.0 #3449

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

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ build-type: Simple
extra-source-files:
LICENSE
test/testdata/**/*.hs
test/testdata/.ormolu
test/testdata/test.cabal

source-repository head
type: git
Expand Down Expand Up @@ -49,3 +51,4 @@ test-suite tests
, hls-ormolu-plugin
, hls-test-utils ^>=1.5
, lsp-types
, ormolu
28 changes: 22 additions & 6 deletions plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

Expand All @@ -8,8 +9,10 @@ module Ide.Plugin.Ormolu
)
where

import Control.Exception (try)
import Control.Exception (Handler (..), IOException,
SomeException (..), catches)
import Control.Monad.IO.Class (liftIO)
import Data.Functor ((<&>))
import qualified Data.Text as T
import Development.IDE hiding (pluginHandlers)
import Development.IDE.GHC.Compat (hsc_dflags, moduleNameString)
Expand Down Expand Up @@ -44,13 +47,26 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
fullRegion = RegionIndices Nothing Nothing
rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1)
mkConf o region = defaultConfig { cfgDynOptions = o, cfgRegion = region }
fmt :: T.Text -> Config RegionIndices -> IO (Either OrmoluException T.Text)
fmt cont conf =
fmt :: T.Text -> Config RegionIndices -> IO (Either SomeException T.Text)
fmt cont conf = flip catches handlers $ do
let fp' = fromNormalizedFilePath fp
#if MIN_VERSION_ormolu(0,5,3)
try @OrmoluException $ ormolu conf (fromNormalizedFilePath fp) cont
cabalInfo <- getCabalInfoForSourceFile fp' <&> \case
CabalNotFound -> Nothing
CabalDidNotMention cabalInfo -> Just cabalInfo
CabalFound cabalInfo -> Just cabalInfo
fixityOverrides <- traverse getFixityOverridesForSourceFile cabalInfo
let conf' = refineConfig ModuleSource cabalInfo fixityOverrides conf
cont' = cont
#else
try @OrmoluException $ ormolu conf (fromNormalizedFilePath fp) $ T.unpack cont
let conf' = conf
cont' = T.unpack cont
#endif
Right <$> ormolu conf' fp' cont'
handlers =
[ Handler $ pure . Left . SomeException @OrmoluException
, Handler $ pure . Left . SomeException @IOException
]

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

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

Expand Down
5 changes: 5 additions & 0 deletions plugins/hls-ormolu-plugin/test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
module Main
( main
Expand All @@ -20,6 +21,10 @@ tests = testGroup "ormolu"
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
, goldenWithOrmolu "formats imports correctly" "Ormolu2" "formatted" $ \doc -> do
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
#if MIN_VERSION_ormolu(0,5,3)
, goldenWithOrmolu "formats operators correctly" "Ormolu3" "formatted" $ \doc -> do
formatDoc doc (FormattingOptions 4 True Nothing Nothing Nothing)
#endif
]

goldenWithOrmolu :: TestName -> FilePath -> FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
Expand Down
1 change: 1 addition & 0 deletions plugins/hls-ormolu-plugin/test/testdata/.ormolu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
infixl 7 .=?
5 changes: 5 additions & 0 deletions plugins/hls-ormolu-plugin/test/testdata/Ormolu3.expected.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo :: String
foo =
"a" .=? "b"
<> "c" .=? "d"
<> "e" .=? "f"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foo :: String
foo =
"a" .=? "b"
<> "c" .=? "d"
<> "e" .=? "f"
3 changes: 3 additions & 0 deletions plugins/hls-ormolu-plugin/test/testdata/Ormolu3.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo :: String
foo = "a" .=? "b"
<> "c" .=? "d" <> "e" .=? "f"
3 changes: 3 additions & 0 deletions plugins/hls-ormolu-plugin/test/testdata/test.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cabal-version: 3.0
name: test
version: 0