Skip to content

Commit 5163c41

Browse files
authored
Merge pull request #257 from sureyeaah/ormolu-fix
Ormolu fix
2 parents 530ccb0 + 7a8f51d commit 5163c41

15 files changed

+72
-45
lines changed

src/Ide/Plugin/Ormolu.hs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,16 @@ provider lf ideState typ contents fp _ = withIndefiniteProgress lf title Cancell
6464

6565
let
6666
fullRegion = RegionIndices Nothing Nothing
67-
rangeRegion s e = RegionIndices (Just s) (Just e)
67+
rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1)
6868
mkConf o region = defaultConfig { cfgDynOptions = o, cfgRegion = region }
6969
fmt :: T.Text -> Config RegionIndices -> IO (Either OrmoluException T.Text)
7070
fmt cont conf =
7171
try @OrmoluException (ormolu conf (fromNormalizedFilePath fp) $ T.unpack cont)
7272

7373
case typ of
7474
FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
75-
FormatRange r ->
76-
let
77-
Range (Position sl _) (Position el _) = normalize r
78-
in
79-
ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el))
75+
FormatRange (Range (Position sl _) (Position el _)) ->
76+
ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el))
8077
where
8178
title = T.pack $ "Formatting " <> takeFileName (fromNormalizedFilePath fp)
8279
ret :: Either OrmoluException T.Text -> Either ResponseError (List TextEdit)

src/Ide/PluginUtils.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,11 @@ diffTextEdit fText f2Text withDeletions = J.List r
6767
(J.Position el 0)
6868

6969
diffOperationToTextEdit (Addition fm l) = J.TextEdit range nt
70-
-- fm has a range wrt to the changed file, which starts in the current file at l
71-
-- So the range has to be shifted to start at l
70+
-- fm has a range wrt to the changed file, which starts in the current file at l + 1
71+
-- So the range has to be shifted to start at l + 1
7272
where
73-
range = J.Range (J.Position (l' - 1) 0)
74-
(J.Position (l' - 1) 0)
75-
l' = max l sl -- Needed to add at the end of the file
76-
sl = fst $ lrNumbers fm
73+
range = J.Range (J.Position l 0)
74+
(J.Position l 0)
7775
nt = T.pack $ unlines $ lrContents fm
7876

7977

@@ -109,4 +107,4 @@ clientSupportsDocumentChanges caps =
109107
WorkspaceEditClientCapabilities mDc <- _workspaceEdit wCaps
110108
mDc
111109
in
112-
fromMaybe False supports
110+
fromMaybe False supports

test/functional/Format.hs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ rangeTests :: TestTree
4242
rangeTests = testGroup "format range" [
4343
goldenVsStringDiff "works" goldenGitDiff "test/testdata/Format.formatted_range.hs" $ runSession hieCommand fullCaps "test/testdata" $ do
4444
doc <- openDoc "Format.hs" "haskell"
45-
formatRange doc (FormattingOptions 2 True) (Range (Position 1 0) (Position 3 10))
45+
formatRange doc (FormattingOptions 2 True) (Range (Position 5 0) (Position 7 10))
4646
BS.fromStrict . T.encodeUtf8 <$> documentContents doc
4747
, goldenVsStringDiff "works with custom tab size" goldenGitDiff "test/testdata/Format.formatted_range_with_tabsize.hs" $ runSession hieCommand fullCaps "test/testdata" $ do
4848
doc <- openDoc "Format.hs" "haskell"
49-
formatRange doc (FormattingOptions 5 True) (Range (Position 4 0) (Position 7 19))
49+
formatRange doc (FormattingOptions 5 True) (Range (Position 8 0) (Position 11 19))
5050
BS.fromStrict . T.encodeUtf8 <$> documentContents doc
5151
]
5252

@@ -143,19 +143,18 @@ brittanyTests = testGroup "brittany" [
143143
]
144144

145145
ormoluTests :: TestTree
146-
ormoluTests = testGroup "ormolu" [
147-
goldenVsStringDiff "formats correctly" goldenGitDiff ("test/testdata/Format.ormolu." ++ ormoluGoldenSuffix ++ ".hs") $ runSession hieCommand fullCaps "test/testdata" $ do
148-
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "ormolu"))
149-
doc <- openDoc "Format.hs" "haskell"
150-
formatDoc doc (FormattingOptions 2 True)
151-
BS.fromStrict . T.encodeUtf8 <$> documentContents doc
152-
]
153-
where
154-
ormoluGoldenSuffix = case ghcVersion of
155-
GHC88 -> "formatted"
156-
GHC86 -> "formatted"
157-
_ -> "unchanged"
158-
146+
ormoluTests = testGroup "ormolu"
147+
[ goldenVsStringDiff "formats correctly" goldenGitDiff "test/testdata/Format.ormolu.formatted.hs" $ runSession hieCommand fullCaps "test/testdata" $ do
148+
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "ormolu"))
149+
doc <- openDoc "Format.hs" "haskell"
150+
formatDoc doc (FormattingOptions 2 True)
151+
BS.fromStrict . T.encodeUtf8 <$> documentContents doc
152+
, goldenVsStringDiff "formats imports correctly" goldenGitDiff "test/testdata/Format2.ormolu.formatted.hs" $ runSession hieCommand fullCaps "test/testdata" $ do
153+
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "ormolu"))
154+
doc <- openDoc "Format2.hs" "haskell"
155+
formatDoc doc (FormattingOptions 2 True)
156+
BS.fromStrict . T.encodeUtf8 <$> documentContents doc
157+
]
159158

160159
formatLspConfig :: Value -> Value
161160
formatLspConfig provider = object [ "haskell" .= object ["formattingProvider" .= (provider :: Value)] ]

test/testdata/Format.brittany.formatted.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module Format where
2+
import Data.List
3+
4+
import Prelude
5+
import Data.Int
26
foo :: Int -> Int
37
foo 3 = 2
48
foo x = x

test/testdata/Format.brittany_post_floskell.formatted.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module Format where
22

3+
import Data.List
4+
import Prelude
5+
import Data.Int
6+
37
foo :: Int -> Int
48
foo 3 = 2
59
foo x = x

test/testdata/Format.floskell.formatted.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module Format where
22

3+
import Data.List
4+
import Prelude
5+
import Data.Int
6+
37
foo :: Int -> Int
48
foo 3 = 2
59
foo x = x

test/testdata/Format.formatted_document.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
module Format where
22

3+
import Data.Int
4+
import Data.List
5+
import Prelude
6+
37
foo :: Int -> Int
48
foo 3 = 2
59
foo x = x
10+
611
bar :: String -> IO String
712
bar s = do
813
x <- return "hello"
914
return "asdf"
1015

1116
data Baz = Baz {a :: Int, b :: String}
12-

test/testdata/Format.formatted_document_with_tabsize.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
module Format where
22

3+
import Data.Int
4+
import Data.List
5+
import Prelude
6+
37
foo :: Int -> Int
48
foo 3 = 2
59
foo x = x
10+
611
bar :: String -> IO String
712
bar s = do
813
x <- return "hello"
914
return "asdf"
1015

1116
data Baz = Baz {a :: Int, b :: String}
12-

test/testdata/Format.formatted_range.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
module Format where
1+
module Format where
2+
import Data.List
23

4+
import Prelude
5+
import Data.Int
36
foo :: Int -> Int
47
foo 3 = 2
58
foo x = x
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
module Format where
2+
import Data.List
3+
4+
import Prelude
5+
import Data.Int
26
foo :: Int -> Int
37
foo 3 = 2
4-
foo x = x
8+
foo x = x
59
bar :: String -> IO String
610
bar s = do
711
x <- return "hello"
812
return "asdf"
913

10-
1114
data Baz = Baz { a :: Int, b :: String }
1215

test/testdata/Format.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module Format where
2+
import Data.List
3+
4+
import Prelude
5+
import Data.Int
26
foo :: Int -> Int
37
foo 3 = 2
48
foo x = x

test/testdata/Format.ormolu.formatted.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
module Format where
22

3+
import Data.Int
4+
import Data.List
5+
import Prelude
6+
37
foo :: Int -> Int
48
foo 3 = 2
59
foo x = x
10+
611
bar :: String -> IO String
712
bar s = do
813
x <- return "hello"
914
return "asdf"
1015

1116
data Baz = Baz {a :: Int, b :: String}
12-

test/testdata/Format.ormolu.unchanged.hs

Lines changed: 0 additions & 11 deletions
This file was deleted.

test/testdata/Format2.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Data.Char
2+
import Data.Either
3+
import Data.Int
4+
import Data.Data
5+
import Data.Bool
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Data.Bool
2+
import Data.Char
3+
import Data.Data
4+
import Data.Either
5+
import Data.Int

0 commit comments

Comments
 (0)