|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | +module FormatSpec where |
| 3 | + |
| 4 | +import Control.Monad.IO.Class |
| 5 | +import Data.Aeson |
| 6 | +import qualified Data.Text as T |
| 7 | +import Language.Haskell.LSP.Test |
| 8 | +import Language.Haskell.LSP.Types |
| 9 | +import Test.Hspec |
| 10 | +import TestUtils |
| 11 | + |
| 12 | +spec :: Spec |
| 13 | +spec = do |
| 14 | + describe "format document" $ do |
| 15 | + it "works" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 16 | + doc <- openDoc "Format.hs" "haskell" |
| 17 | + formatDoc doc (FormattingOptions 2 True) |
| 18 | + documentContents doc >>= liftIO . (`shouldBe` formattedDocTabSize2) |
| 19 | + it "works with custom tab size" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 20 | + doc <- openDoc "Format.hs" "haskell" |
| 21 | + formatDoc doc (FormattingOptions 5 True) |
| 22 | + documentContents doc >>= liftIO . (`shouldBe` formattedDocTabSize5) |
| 23 | + |
| 24 | + describe "format range" $ do |
| 25 | + it "works" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 26 | + doc <- openDoc "Format.hs" "haskell" |
| 27 | + formatRange doc (FormattingOptions 2 True) (Range (Position 1 0) (Position 3 10)) |
| 28 | + documentContents doc >>= liftIO . (`shouldBe` formattedRangeTabSize2) |
| 29 | + it "works with custom tab size" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 30 | + doc <- openDoc "Format.hs" "haskell" |
| 31 | + formatRange doc (FormattingOptions 5 True) (Range (Position 4 0) (Position 7 19)) |
| 32 | + documentContents doc >>= liftIO . (`shouldBe` formattedRangeTabSize5) |
| 33 | + |
| 34 | + describe "formatting provider" $ do |
| 35 | + let formatLspConfig provider = |
| 36 | + object [ "languageServerHaskell" .= object ["formattingProvider" .= (provider :: Value)] ] |
| 37 | + formatConfig provider = defaultConfig { lspConfig = Just (formatLspConfig provider) } |
| 38 | + |
| 39 | + it "respects none" $ runSessionWithConfig (formatConfig "none") hieCommand fullCaps "test/testdata" $ do |
| 40 | + doc <- openDoc "Format.hs" "haskell" |
| 41 | + orig <- documentContents doc |
| 42 | + |
| 43 | + formatDoc doc (FormattingOptions 2 True) |
| 44 | + documentContents doc >>= liftIO . (`shouldBe` orig) |
| 45 | + |
| 46 | + formatRange doc (FormattingOptions 2 True) (Range (Position 1 0) (Position 3 10)) |
| 47 | + documentContents doc >>= liftIO . (`shouldBe` orig) |
| 48 | + |
| 49 | + it "can change on the fly" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 50 | + doc <- openDoc "Format.hs" "haskell" |
| 51 | + |
| 52 | + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany")) |
| 53 | + formatDoc doc (FormattingOptions 2 True) |
| 54 | + documentContents doc >>= liftIO . (`shouldBe` formattedDocTabSize2) |
| 55 | + |
| 56 | + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "floskell")) |
| 57 | + formatDoc doc (FormattingOptions 2 True) |
| 58 | + documentContents doc >>= liftIO . (`shouldBe` formattedFloskell) |
| 59 | + |
| 60 | + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany")) |
| 61 | + formatDoc doc (FormattingOptions 2 True) |
| 62 | + documentContents doc >>= liftIO . (`shouldBe` formattedBrittanyPostFloskell) |
| 63 | + |
| 64 | + describe "brittany" $ do |
| 65 | + it "formats a document with LF endings" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 66 | + doc <- openDoc "BrittanyLF.hs" "haskell" |
| 67 | + let opts = DocumentFormattingParams doc (FormattingOptions 4 True) Nothing |
| 68 | + ResponseMessage _ _ (Just edits) _ <- request TextDocumentFormatting opts |
| 69 | + liftIO $ edits `shouldBe` [TextEdit (Range (Position 0 0) (Position 3 0)) |
| 70 | + "foo :: Int -> String -> IO ()\nfoo x y = do\n print x\n return 42\n"] |
| 71 | + |
| 72 | + it "formats a document with CRLF endings" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 73 | + doc <- openDoc "BrittanyCRLF.hs" "haskell" |
| 74 | + let opts = DocumentFormattingParams doc (FormattingOptions 4 True) Nothing |
| 75 | + ResponseMessage _ _ (Just edits) _ <- request TextDocumentFormatting opts |
| 76 | + liftIO $ edits `shouldBe` [TextEdit (Range (Position 0 0) (Position 3 0)) |
| 77 | + "foo :: Int -> String -> IO ()\nfoo x y = do\n print x\n return 42\n"] |
| 78 | + |
| 79 | + it "formats a range with LF endings" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 80 | + doc <- openDoc "BrittanyLF.hs" "haskell" |
| 81 | + let range = Range (Position 1 0) (Position 2 22) |
| 82 | + opts = DocumentRangeFormattingParams doc range (FormattingOptions 4 True) Nothing |
| 83 | + ResponseMessage _ _ (Just edits) _ <- request TextDocumentRangeFormatting opts |
| 84 | + liftIO $ edits `shouldBe` [TextEdit (Range (Position 1 0) (Position 3 0)) |
| 85 | + "foo x y = do\n print x\n return 42\n"] |
| 86 | + |
| 87 | + it "formats a range with CRLF endings" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 88 | + doc <- openDoc "BrittanyCRLF.hs" "haskell" |
| 89 | + let range = Range (Position 1 0) (Position 2 22) |
| 90 | + opts = DocumentRangeFormattingParams doc range (FormattingOptions 4 True) Nothing |
| 91 | + ResponseMessage _ _ (Just edits) _ <- request TextDocumentRangeFormatting opts |
| 92 | + liftIO $ edits `shouldBe` [TextEdit (Range (Position 1 0) (Position 3 0)) |
| 93 | + "foo x y = do\n print x\n return 42\n"] |
| 94 | + |
| 95 | + describe "ormolu" $ do |
| 96 | + let formatLspConfig provider = |
| 97 | + object [ "languageServerHaskell" .= object ["formattingProvider" .= (provider :: Value)] ] |
| 98 | + |
| 99 | + it "formats correctly" $ runSession hieCommand fullCaps "test/testdata" $ do |
| 100 | + sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "ormolu")) |
| 101 | + doc <- openDoc "Format.hs" "haskell" |
| 102 | + formatDoc doc (FormattingOptions 2 True) |
| 103 | + docContent <- documentContents doc |
| 104 | + let formatted = liftIO $ docContent `shouldBe` formattedOrmolu |
| 105 | + case ghcVersion of |
| 106 | + GHC88 -> formatted |
| 107 | + GHC86 -> formatted |
| 108 | + _ -> liftIO $ docContent `shouldBe` unchangedOrmolu |
| 109 | + |
| 110 | + |
| 111 | +formattedDocTabSize2 :: T.Text |
| 112 | +formattedDocTabSize2 = |
| 113 | + "module Format where\n\ |
| 114 | + \foo :: Int -> Int\n\ |
| 115 | + \foo 3 = 2\n\ |
| 116 | + \foo x = x\n\ |
| 117 | + \bar :: String -> IO String\n\ |
| 118 | + \bar s = do\n\ |
| 119 | + \ x <- return \"hello\"\n\ |
| 120 | + \ return \"asdf\"\n\n" |
| 121 | + |
| 122 | +formattedDocTabSize5 :: T.Text |
| 123 | +formattedDocTabSize5 = |
| 124 | + "module Format where\n\ |
| 125 | + \foo :: Int -> Int\n\ |
| 126 | + \foo 3 = 2\n\ |
| 127 | + \foo x = x\n\ |
| 128 | + \bar :: String -> IO String\n\ |
| 129 | + \bar s = do\n\ |
| 130 | + \ x <- return \"hello\"\n\ |
| 131 | + \ return \"asdf\"\n\n" |
| 132 | + |
| 133 | +formattedRangeTabSize2 :: T.Text |
| 134 | +formattedRangeTabSize2 = |
| 135 | + "module Format where\n\ |
| 136 | + \foo :: Int -> Int\n\ |
| 137 | + \foo 3 = 2\n\ |
| 138 | + \foo x = x\n\ |
| 139 | + \bar :: String -> IO String\n\ |
| 140 | + \bar s = do\n\ |
| 141 | + \ x <- return \"hello\"\n\ |
| 142 | + \ return \"asdf\"\n\ |
| 143 | + \ \n" |
| 144 | + |
| 145 | +formattedRangeTabSize5 :: T.Text |
| 146 | +formattedRangeTabSize5 = |
| 147 | + "module Format where\n\ |
| 148 | + \foo :: Int -> Int\n\ |
| 149 | + \foo 3 = 2\n\ |
| 150 | + \foo x = x\n\ |
| 151 | + \bar :: String -> IO String\n\ |
| 152 | + \bar s = do\n\ |
| 153 | + \ x <- return \"hello\"\n\ |
| 154 | + \ return \"asdf\"\n\ |
| 155 | + \ \n" |
| 156 | + |
| 157 | +formattedFloskell :: T.Text |
| 158 | +formattedFloskell = |
| 159 | + "module Format where\n\ |
| 160 | + \\n\ |
| 161 | + \foo :: Int -> Int\n\ |
| 162 | + \foo 3 = 2\n\ |
| 163 | + \foo x = x\n\ |
| 164 | + \\n\ |
| 165 | + \bar :: String -> IO String\n\ |
| 166 | + \bar s = do\n\ |
| 167 | + \ x <- return \"hello\"\n\ |
| 168 | + \ return \"asdf\"\n\n\ |
| 169 | + \" |
| 170 | + |
| 171 | +formattedBrittanyPostFloskell :: T.Text |
| 172 | +formattedBrittanyPostFloskell = |
| 173 | + "module Format where\n\ |
| 174 | + \\n\ |
| 175 | + \foo :: Int -> Int\n\ |
| 176 | + \foo 3 = 2\n\ |
| 177 | + \foo x = x\n\ |
| 178 | + \\n\ |
| 179 | + \bar :: String -> IO String\n\ |
| 180 | + \bar s = do\n\ |
| 181 | + \ x <- return \"hello\"\n\ |
| 182 | + \ return \"asdf\"\n\n" |
| 183 | + |
| 184 | +formattedOrmolu :: T.Text |
| 185 | +formattedOrmolu = |
| 186 | + "module Format where\n\ |
| 187 | + \\n\ |
| 188 | + \foo :: Int -> Int\n\ |
| 189 | + \foo 3 = 2\n\ |
| 190 | + \foo x = x\n\ |
| 191 | + \\n\ |
| 192 | + \bar :: String -> IO String\n\ |
| 193 | + \bar s = do\n\ |
| 194 | + \ x <- return \"hello\"\n\ |
| 195 | + \ return \"asdf\"\n" |
| 196 | + |
| 197 | +unchangedOrmolu :: T.Text |
| 198 | +unchangedOrmolu = |
| 199 | + "module Format where\n\ |
| 200 | + \foo :: Int -> Int\n\ |
| 201 | + \foo 3 = 2\n\ |
| 202 | + \foo x = x\n\ |
| 203 | + \bar :: String -> IO String\n\ |
| 204 | + \bar s = do\n\ |
| 205 | + \ x <- return \"hello\"\n\ |
| 206 | + \ return \"asdf\"\n\ |
| 207 | + \ \n" |
0 commit comments