Skip to content

Commit

Permalink
Merge pull request #257 from sureyeaah/ormolu-fix
Browse files Browse the repository at this point in the history
Ormolu fix
  • Loading branch information
lukel97 committed Jul 31, 2020
2 parents 530ccb0 + 7a8f51d commit 5163c41
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 45 deletions.
9 changes: 3 additions & 6 deletions src/Ide/Plugin/Ormolu.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,16 @@ provider lf ideState typ contents fp _ = withIndefiniteProgress lf title Cancell

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 =
try @OrmoluException (ormolu conf (fromNormalizedFilePath fp) $ T.unpack cont)

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
title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp)
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit)
Expand Down
12 changes: 5 additions & 7 deletions src/Ide/PluginUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -109,4 +107,4 @@ clientSupportsDocumentChanges caps =
WorkspaceEditClientCapabilities mDc <- _workspaceEdit wCaps
mDc
in
fromMaybe False supports
fromMaybe False supports
29 changes: 14 additions & 15 deletions test/functional/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

Expand Down Expand Up @@ -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)] ]
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/Format.brittany.formatted.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module Format where
import Data.List

import Prelude
import Data.Int
foo :: Int -> Int
foo 3 = 2
foo x = x
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/Format.brittany_post_floskell.formatted.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module Format where

import Data.List
import Prelude
import Data.Int

foo :: Int -> Int
foo 3 = 2
foo x = x
Expand Down
4 changes: 4 additions & 0 deletions test/testdata/Format.floskell.formatted.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module Format where

import Data.List
import Prelude
import Data.Int

foo :: Int -> Int
foo 3 = 2
foo x = x
Expand Down
6 changes: 5 additions & 1 deletion test/testdata/Format.formatted_document.hs
Original file line number Diff line number Diff line change
@@ -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}

6 changes: 5 additions & 1 deletion test/testdata/Format.formatted_document_with_tabsize.hs
Original file line number Diff line number Diff line change
@@ -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}

5 changes: 4 additions & 1 deletion test/testdata/Format.formatted_range.hs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 5 additions & 2 deletions test/testdata/Format.formatted_range_with_tabsize.hs
Original file line number Diff line number Diff line change
@@ -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 }

4 changes: 4 additions & 0 deletions test/testdata/Format.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module Format where
import Data.List

import Prelude
import Data.Int
foo :: Int -> Int
foo 3 = 2
foo x = x
Expand Down
6 changes: 5 additions & 1 deletion test/testdata/Format.ormolu.formatted.hs
Original file line number Diff line number Diff line change
@@ -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}

11 changes: 0 additions & 11 deletions test/testdata/Format.ormolu.unchanged.hs

This file was deleted.

5 changes: 5 additions & 0 deletions test/testdata/Format2.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Data.Char
import Data.Either
import Data.Int
import Data.Data
import Data.Bool
5 changes: 5 additions & 0 deletions test/testdata/Format2.ormolu.formatted.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Data.Bool
import Data.Char
import Data.Data
import Data.Either
import Data.Int

0 comments on commit 5163c41

Please sign in to comment.