diff --git a/plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal b/plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal index f9d9870a96..e7b8d0c9cd 100644 --- a/plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal +++ b/plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal @@ -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 @@ -49,3 +51,4 @@ test-suite tests , hls-ormolu-plugin , hls-test-utils ^>=1.5 , lsp-types + , ormolu diff --git a/plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs b/plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs index a1854d9636..d34fc837bc 100644 --- a/plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs +++ b/plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs @@ -1,4 +1,5 @@ {-# LANGUAGE CPP #-} +{-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} @@ -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) @@ -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) @@ -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 diff --git a/plugins/hls-ormolu-plugin/test/Main.hs b/plugins/hls-ormolu-plugin/test/Main.hs index f03b65719d..f395b6a2d3 100644 --- a/plugins/hls-ormolu-plugin/test/Main.hs +++ b/plugins/hls-ormolu-plugin/test/Main.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} module Main ( main @@ -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 diff --git a/plugins/hls-ormolu-plugin/test/testdata/.ormolu b/plugins/hls-ormolu-plugin/test/testdata/.ormolu new file mode 100644 index 0000000000..8710f312a9 --- /dev/null +++ b/plugins/hls-ormolu-plugin/test/testdata/.ormolu @@ -0,0 +1 @@ +infixl 7 .=? diff --git a/plugins/hls-ormolu-plugin/test/testdata/Ormolu3.expected.hs b/plugins/hls-ormolu-plugin/test/testdata/Ormolu3.expected.hs new file mode 100644 index 0000000000..386166490e --- /dev/null +++ b/plugins/hls-ormolu-plugin/test/testdata/Ormolu3.expected.hs @@ -0,0 +1,5 @@ +foo :: String +foo = + "a" .=? "b" + <> "c" .=? "d" + <> "e" .=? "f" diff --git a/plugins/hls-ormolu-plugin/test/testdata/Ormolu3.formatted.hs b/plugins/hls-ormolu-plugin/test/testdata/Ormolu3.formatted.hs new file mode 100644 index 0000000000..386166490e --- /dev/null +++ b/plugins/hls-ormolu-plugin/test/testdata/Ormolu3.formatted.hs @@ -0,0 +1,5 @@ +foo :: String +foo = + "a" .=? "b" + <> "c" .=? "d" + <> "e" .=? "f" diff --git a/plugins/hls-ormolu-plugin/test/testdata/Ormolu3.hs b/plugins/hls-ormolu-plugin/test/testdata/Ormolu3.hs new file mode 100644 index 0000000000..fd7e1f4270 --- /dev/null +++ b/plugins/hls-ormolu-plugin/test/testdata/Ormolu3.hs @@ -0,0 +1,3 @@ +foo :: String +foo = "a" .=? "b" + <> "c" .=? "d" <> "e" .=? "f" diff --git a/plugins/hls-ormolu-plugin/test/testdata/test.cabal b/plugins/hls-ormolu-plugin/test/testdata/test.cabal new file mode 100644 index 0000000000..004fc9e694 --- /dev/null +++ b/plugins/hls-ormolu-plugin/test/testdata/test.cabal @@ -0,0 +1,3 @@ +cabal-version: 3.0 +name: test +version: 0