diff --git a/src/Ide/Plugin/Ormolu.hs b/src/Ide/Plugin/Ormolu.hs index aa337fbc8e..8457ffd3a3 100644 --- a/src/Ide/Plugin/Ormolu.hs +++ b/src/Ide/Plugin/Ormolu.hs @@ -57,7 +57,7 @@ provider _lf ideState typ contents fp _ = do let fullRegion = RegionIndices Nothing Nothing - rangeRegion s e = RegionIndices (Just s) (Just e) + 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 = @@ -65,11 +65,8 @@ provider _lf ideState typ contents fp _ = do case typ of FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion) - FormatRange r -> - let - Range (Position sl _) (Position el _) = normalize r - in - ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el)) + FormatRange (Range (Position sl _) (Position el _)) -> + ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el)) where ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit) ret (Left err) = Left diff --git a/src/Ide/PluginUtils.hs b/src/Ide/PluginUtils.hs index 41a295b639..442cc770f7 100644 --- a/src/Ide/PluginUtils.hs +++ b/src/Ide/PluginUtils.hs @@ -67,13 +67,11 @@ diffTextEdit fText f2Text withDeletions = J.List r (J.Position el 0) diffOperationToTextEdit (Addition fm l) = J.TextEdit range nt - -- fm has a range wrt to the changed file, which starts in the current file at l - -- So the range has to be shifted to start at l + -- fm has a range wrt to the changed file, which starts in the current file at l + 1 + -- So the range has to be shifted to start at l + 1 where - range = J.Range (J.Position (l' - 1) 0) - (J.Position (l' - 1) 0) - l' = max l sl -- Needed to add at the end of the file - sl = fst $ lrNumbers fm + range = J.Range (J.Position l 0) + (J.Position l 0) nt = T.pack $ unlines $ lrContents fm @@ -109,4 +107,4 @@ clientSupportsDocumentChanges caps = WorkspaceEditClientCapabilities mDc <- _workspaceEdit wCaps mDc in - fromMaybe False supports \ No newline at end of file + fromMaybe False supports diff --git a/test/functional/Format.hs b/test/functional/Format.hs index f1f8e7c291..56e15689a7 100644 --- a/test/functional/Format.hs +++ b/test/functional/Format.hs @@ -42,11 +42,11 @@ rangeTests :: TestTree rangeTests = testGroup "format range" [ goldenVsStringDiff "works" goldenGitDiff "test/testdata/Format.formatted_range.hs" $ runSession hieCommand fullCaps "test/testdata" $ do doc <- openDoc "Format.hs" "haskell" - formatRange doc (FormattingOptions 2 True) (Range (Position 1 0) (Position 3 10)) + formatRange doc (FormattingOptions 2 True) (Range (Position 5 0) (Position 7 10)) BS.fromStrict . T.encodeUtf8 <$> documentContents doc , goldenVsStringDiff "works with custom tab size" goldenGitDiff "test/testdata/Format.formatted_range_with_tabsize.hs" $ runSession hieCommand fullCaps "test/testdata" $ do doc <- openDoc "Format.hs" "haskell" - formatRange doc (FormattingOptions 5 True) (Range (Position 4 0) (Position 7 19)) + formatRange doc (FormattingOptions 5 True) (Range (Position 8 0) (Position 11 19)) BS.fromStrict . T.encodeUtf8 <$> documentContents doc ] @@ -143,19 +143,18 @@ brittanyTests = testGroup "brittany" [ ] ormoluTests :: TestTree -ormoluTests = testGroup "ormolu" [ - goldenVsStringDiff "formats correctly" goldenGitDiff ("test/testdata/Format.ormolu." ++ ormoluGoldenSuffix ++ ".hs") $ runSession hieCommand fullCaps "test/testdata" $ do - sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "ormolu")) - doc <- openDoc "Format.hs" "haskell" - formatDoc doc (FormattingOptions 2 True) - BS.fromStrict . T.encodeUtf8 <$> documentContents doc - ] - where - ormoluGoldenSuffix = case ghcVersion of - GHC88 -> "formatted" - GHC86 -> "formatted" - _ -> "unchanged" - +ormoluTests = testGroup "ormolu" + [ goldenVsStringDiff "formats correctly" goldenGitDiff "test/testdata/Format.ormolu.formatted.hs" $ runSession hieCommand fullCaps "test/testdata" $ do + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "ormolu")) + doc <- openDoc "Format.hs" "haskell" + formatDoc doc (FormattingOptions 2 True) + BS.fromStrict . T.encodeUtf8 <$> documentContents doc + , goldenVsStringDiff "formats imports correctly" goldenGitDiff "test/testdata/Format2.ormolu.formatted.hs" $ runSession hieCommand fullCaps "test/testdata" $ do + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "ormolu")) + doc <- openDoc "Format2.hs" "haskell" + formatDoc doc (FormattingOptions 2 True) + BS.fromStrict . T.encodeUtf8 <$> documentContents doc + ] formatLspConfig :: Value -> Value formatLspConfig provider = object [ "haskell" .= object ["formattingProvider" .= (provider :: Value)] ] diff --git a/test/testdata/Format.brittany.formatted.hs b/test/testdata/Format.brittany.formatted.hs index d0bde680f2..d3da590142 100644 --- a/test/testdata/Format.brittany.formatted.hs +++ b/test/testdata/Format.brittany.formatted.hs @@ -1,4 +1,8 @@ module Format where +import Data.List + +import Prelude +import Data.Int foo :: Int -> Int foo 3 = 2 foo x = x diff --git a/test/testdata/Format.brittany_post_floskell.formatted.hs b/test/testdata/Format.brittany_post_floskell.formatted.hs index 208e754e24..02de9c673d 100644 --- a/test/testdata/Format.brittany_post_floskell.formatted.hs +++ b/test/testdata/Format.brittany_post_floskell.formatted.hs @@ -1,5 +1,9 @@ module Format where +import Data.List +import Prelude +import Data.Int + foo :: Int -> Int foo 3 = 2 foo x = x diff --git a/test/testdata/Format.floskell.formatted.hs b/test/testdata/Format.floskell.formatted.hs index 208e754e24..02de9c673d 100644 --- a/test/testdata/Format.floskell.formatted.hs +++ b/test/testdata/Format.floskell.formatted.hs @@ -1,5 +1,9 @@ module Format where +import Data.List +import Prelude +import Data.Int + foo :: Int -> Int foo 3 = 2 foo x = x diff --git a/test/testdata/Format.formatted_document.hs b/test/testdata/Format.formatted_document.hs index ec1ce57379..ac43b2d285 100644 --- a/test/testdata/Format.formatted_document.hs +++ b/test/testdata/Format.formatted_document.hs @@ -1,12 +1,16 @@ module Format where +import Data.Int +import Data.List +import Prelude + foo :: Int -> Int foo 3 = 2 foo x = x + bar :: String -> IO String bar s = do x <- return "hello" return "asdf" data Baz = Baz {a :: Int, b :: String} - diff --git a/test/testdata/Format.formatted_document_with_tabsize.hs b/test/testdata/Format.formatted_document_with_tabsize.hs index ec1ce57379..ac43b2d285 100644 --- a/test/testdata/Format.formatted_document_with_tabsize.hs +++ b/test/testdata/Format.formatted_document_with_tabsize.hs @@ -1,12 +1,16 @@ module Format where +import Data.Int +import Data.List +import Prelude + foo :: Int -> Int foo 3 = 2 foo x = x + bar :: String -> IO String bar s = do x <- return "hello" return "asdf" data Baz = Baz {a :: Int, b :: String} - diff --git a/test/testdata/Format.formatted_range.hs b/test/testdata/Format.formatted_range.hs index 393584a9e4..920a07916e 100644 --- a/test/testdata/Format.formatted_range.hs +++ b/test/testdata/Format.formatted_range.hs @@ -1,5 +1,8 @@ -module Format where +module Format where +import Data.List +import Prelude +import Data.Int foo :: Int -> Int foo 3 = 2 foo x = x diff --git a/test/testdata/Format.formatted_range_with_tabsize.hs b/test/testdata/Format.formatted_range_with_tabsize.hs index 0a98f42e8f..33a942e43d 100644 --- a/test/testdata/Format.formatted_range_with_tabsize.hs +++ b/test/testdata/Format.formatted_range_with_tabsize.hs @@ -1,12 +1,15 @@ module Format where +import Data.List + +import Prelude +import Data.Int foo :: Int -> Int foo 3 = 2 -foo x = x +foo x = x bar :: String -> IO String bar s = do x <- return "hello" return "asdf" - data Baz = Baz { a :: Int, b :: String } diff --git a/test/testdata/Format.hs b/test/testdata/Format.hs index d4682acaa2..b8bb374e2e 100644 --- a/test/testdata/Format.hs +++ b/test/testdata/Format.hs @@ -1,4 +1,8 @@ module Format where +import Data.List + +import Prelude +import Data.Int foo :: Int -> Int foo 3 = 2 foo x = x diff --git a/test/testdata/Format.ormolu.formatted.hs b/test/testdata/Format.ormolu.formatted.hs index ec1ce57379..ac43b2d285 100644 --- a/test/testdata/Format.ormolu.formatted.hs +++ b/test/testdata/Format.ormolu.formatted.hs @@ -1,12 +1,16 @@ module Format where +import Data.Int +import Data.List +import Prelude + foo :: Int -> Int foo 3 = 2 foo x = x + bar :: String -> IO String bar s = do x <- return "hello" return "asdf" data Baz = Baz {a :: Int, b :: String} - diff --git a/test/testdata/Format.ormolu.unchanged.hs b/test/testdata/Format.ormolu.unchanged.hs deleted file mode 100644 index d4682acaa2..0000000000 --- a/test/testdata/Format.ormolu.unchanged.hs +++ /dev/null @@ -1,11 +0,0 @@ -module Format where -foo :: Int -> Int -foo 3 = 2 -foo x = x -bar :: String -> IO String -bar s = do - x <- return "hello" - return "asdf" - -data Baz = Baz { a :: Int, b :: String } - diff --git a/test/testdata/Format2.hs b/test/testdata/Format2.hs new file mode 100644 index 0000000000..bb011b5638 --- /dev/null +++ b/test/testdata/Format2.hs @@ -0,0 +1,5 @@ +import Data.Char +import Data.Either +import Data.Int +import Data.Data +import Data.Bool diff --git a/test/testdata/Format2.ormolu.formatted.hs b/test/testdata/Format2.ormolu.formatted.hs new file mode 100644 index 0000000000..b3d867e700 --- /dev/null +++ b/test/testdata/Format2.ormolu.formatted.hs @@ -0,0 +1,5 @@ +import Data.Bool +import Data.Char +import Data.Data +import Data.Either +import Data.Int