Skip to content

Commit

Permalink
Merge pull request #243 from bubba/accept-legacy-config-section-name
Browse files Browse the repository at this point in the history
Accept the legacy "languageServerHaskell" config name
  • Loading branch information
lukel97 authored Jul 27, 2020
2 parents f2384e1 + 95560b1 commit 0aae68b
Show file tree
Hide file tree
Showing 20 changed files with 108 additions and 162 deletions.
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ package ghcide

write-ghc-environment-files: never

index-state: 2020-07-16T17:24:10Z
index-state: 2020-07-27T12:40:45Z
4 changes: 2 additions & 2 deletions haskell-language-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ common hls-test-utils
, hslogger
, hspec
, hspec-core
, lsp-test
, lsp-test >= 0.11.0.3
, stm
, tasty-hunit
, temporary
Expand Down Expand Up @@ -245,7 +245,7 @@ test-suite func-test
, haskell-lsp-types
, hspec-expectations
, lens
, lsp-test >= 0.10.0.0
, lsp-test >= 0.11.0.3
, tasty
, tasty-ant-xml >= 1.1.6
, tasty-expected-failure
Expand Down
5 changes: 4 additions & 1 deletion src/Ide/Plugin/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Ide.Plugin.Config
, Config(..)
) where

import Control.Applicative
import qualified Data.Aeson as A
import Data.Aeson hiding ( Error )
import Data.Default
Expand Down Expand Up @@ -70,7 +71,9 @@ instance Default Config where
-- TODO: Add API for plugins to expose their own LSP config options
instance A.FromJSON Config where
parseJSON = A.withObject "Config" $ \v -> do
s <- v .: "haskell"
-- Officially, we use "haskell" as the section name but for
-- backwards compatibility we also accept "languageServerHaskell"
s <- v .: "haskell" <|> v .: "languageServerHaskell"
flip (A.withObject "Config.settings") s $ \o -> Config
<$> o .:? "hlintOn" .!= hlintOn def
<*> o .:? "diagnosticsOnChange" .!= diagnosticsOnChange def
Expand Down
2 changes: 1 addition & 1 deletion stack-8.10.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extra-deps:
- floskell-0.10.3
- ghc-exactprint-0.6.3
- lens-4.19.1
- lsp-test-0.11.0.2
- lsp-test-0.11.0.3
- monad-dijkstra-0.1.1.2
- optics-core-0.3
- ormolu-0.1.2.0
Expand Down
2 changes: 1 addition & 1 deletion stack-8.6.4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extra-deps:
- HsYAML-0.2.1.0@rev:1
- HsYAML-aeson-0.2.0.0@rev:1
- lens-4.18
- lsp-test-0.11.0.2
- lsp-test-0.11.0.3
- microlens-th-0.4.2.3@rev:1
- monad-dijkstra-0.1.1.2
- monad-memo-0.4.1
Expand Down
2 changes: 1 addition & 1 deletion stack-8.6.5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extra-deps:
- HsYAML-aeson-0.2.0.0@rev:1
- indexed-profunctors-0.1
- lens-4.18
- lsp-test-0.11.0.2
- lsp-test-0.11.0.3
- monad-dijkstra-0.1.1.2
- opentelemetry-0.4.2
- optics-core-0.2
Expand Down
2 changes: 1 addition & 1 deletion stack-8.8.2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extra-deps:
- HsYAML-0.2.1.0@rev:1
- HsYAML-aeson-0.2.0.0@rev:1
- ilist-0.3.1.0
- lsp-test-0.11.0.2
- lsp-test-0.11.0.3
- monad-dijkstra-0.1.1.2
- opentelemetry-0.4.2
- ormolu-0.1.2.0
Expand Down
2 changes: 1 addition & 1 deletion stack-8.8.3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extra-deps:
- hoogle-5.0.17.11
- hsimport-0.11.0
- ilist-0.3.1.0
- lsp-test-0.11.0.2
- lsp-test-0.11.0.3
- monad-dijkstra-0.1.1.2
- semigroups-0.18.5
# - github: wz1000/shake
Expand Down
2 changes: 1 addition & 1 deletion stack-8.8.4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extra-deps:
- hoogle-5.0.17.11
- hsimport-0.11.0
- ilist-0.3.1.0
- lsp-test-0.11.0.2
- lsp-test-0.11.0.3
- monad-dijkstra-0.1.1.2
- semigroups-0.18.5
# - github: wz1000/shake
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extra-deps:
- HsYAML-aeson-0.2.0.0@rev:1
- indexed-profunctors-0.1
- lens-4.18
- lsp-test-0.11.0.2
- lsp-test-0.11.0.3
- monad-dijkstra-0.1.1.2
- opentelemetry-0.4.2
- optics-core-0.2
Expand Down
98 changes: 45 additions & 53 deletions test/functional/Format.hs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedStrings, CPP #-}
module Format (tests) where

import Control.Monad.IO.Class
import Data.Aeson
import qualified Data.ByteString.Lazy as BS
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Language.Haskell.LSP.Test
import Language.Haskell.LSP.Types
import Test.Hls.Util
import Test.Tasty
import Test.Tasty.ExpectedFailure (ignoreTestBecause)
import Test.Tasty.Golden
import Test.Tasty.HUnit
import Test.Hspec.Expectations

#if MIN_VERSION_GLASGOW_HASKELL(8,10,0,0) || !defined(AGPL)
#else
import qualified Data.Text.IO as T
#endif

tests :: TestTree
tests = testGroup "format document" [
Expand All @@ -28,7 +30,11 @@ tests = testGroup "format document" [
, rangeTests
, providerTests
, stylishHaskellTests
-- There's no Brittany formatter on the 8.10.1 builds (yet)
#if MIN_VERSION_GLASGOW_HASKELL(8,10,0,0) || !defined(AGPL)
#else
, brittanyTests
#endif
, ormoluTests
]

Expand All @@ -51,25 +57,46 @@ providerTests = testGroup "formatting provider" [
orig <- documentContents doc

formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (`shouldBe` orig)
documentContents doc >>= liftIO . (@?= orig)

formatRange doc (FormattingOptions 2 True) (Range (Position 1 0) (Position 3 10))
documentContents doc >>= liftIO . (`shouldBe` orig)
documentContents doc >>= liftIO . (@?= orig)

-- There's no Brittany formatter on the 8.10.1 builds (yet)
#if MIN_VERSION_GLASGOW_HASKELL(8,10,0,0) || !defined(AGPL)
#else
, testCase "can change on the fly" $ runSession hieCommand fullCaps "test/testdata" $ do
formattedBrittany <- liftIO $ T.readFile "test/testdata/Format.brittany.formatted.hs"
formattedFloskell <- liftIO $ T.readFile "test/testdata/Format.floskell.formatted.hs"
formattedBrittanyPostFloskell <- liftIO $ T.readFile "test/testdata/Format.brittany_post_floskell.formatted.hs"

, ignoreTestBecause "Broken" $ testCase "can change on the fly" $ runSession hieCommand fullCaps "test/testdata" $ do
doc <- openDoc "Format.hs" "haskell"

sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany"))
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (`shouldBe` formattedBrittany)
documentContents doc >>= liftIO . (@?= formattedBrittany)

sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "floskell"))
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (`shouldBe` formattedFloskell)
documentContents doc >>= liftIO . (@?= formattedFloskell)

sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany"))
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (`shouldBe` formattedBrittanyPostFloskell)
documentContents doc >>= liftIO . (@?= formattedBrittanyPostFloskell)
, testCase "supports both new and old configuration sections" $ runSession hieCommand fullCaps "test/testdata" $ do
formattedBrittany <- liftIO $ T.readFile "test/testdata/Format.brittany.formatted.hs"
formattedFloskell <- liftIO $ T.readFile "test/testdata/Format.floskell.formatted.hs"

doc <- openDoc "Format.hs" "haskell"

sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfigOld "brittany"))
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (@?= formattedBrittany)

sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfigOld "floskell"))
formatDoc doc (FormattingOptions 2 True)
documentContents doc >>= liftIO . (@?= formattedFloskell)
#endif
]

stylishHaskellTests :: TestTree
Expand All @@ -89,22 +116,26 @@ stylishHaskellTests = testGroup "stylish-haskell" [
brittanyTests :: TestTree
brittanyTests = testGroup "brittany" [
goldenVsStringDiff "formats a document with LF endings" goldenGitDiff "test/testdata/BrittanyLF.formatted_document.hs" $ runSession hieCommand fullCaps "test/testdata" $ do
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany"))
doc <- openDoc "BrittanyLF.hs" "haskell"
formatDoc doc (FormattingOptions 4 True)
BS.fromStrict . T.encodeUtf8 <$> documentContents doc

, goldenVsStringDiff "formats a document with CRLF endings" goldenGitDiff "test/testdata/BrittanyCRLF.formatted_document.hs" $ runSession hieCommand fullCaps "test/testdata" $ do
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany"))
doc <- openDoc "BrittanyCRLF.hs" "haskell"
formatDoc doc (FormattingOptions 4 True)
BS.fromStrict . T.encodeUtf8 <$> documentContents doc

, goldenVsStringDiff "formats a range with LF endings" goldenGitDiff "test/testdata/BrittanyLF.formatted_range.hs" $ runSession hieCommand fullCaps "test/testdata" $ do
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany"))
doc <- openDoc "BrittanyLF.hs" "haskell"
let range = Range (Position 1 0) (Position 2 22)
formatRange doc (FormattingOptions 4 True) range
BS.fromStrict . T.encodeUtf8 <$> documentContents doc

, goldenVsStringDiff "formats a range with CRLF endings" goldenGitDiff "test/testdata/BrittanyCRLF.formatted_range.hs" $ runSession hieCommand fullCaps "test/testdata" $ do
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "brittany"))
doc <- openDoc "BrittanyCRLF.hs" "haskell"
let range = Range (Position 1 0) (Position 2 22)
formatRange doc (FormattingOptions 4 True) range
Expand All @@ -114,8 +145,6 @@ brittanyTests = testGroup "brittany" [
ormoluTests :: TestTree
ormoluTests = testGroup "ormolu" [
goldenVsStringDiff "formats correctly" goldenGitDiff ("test/testdata/Format.ormolu." ++ ormoluGoldenSuffix ++ ".hs") $ runSession hieCommand fullCaps "test/testdata" $ do
let formatLspConfig provider =
object [ "haskell" .= object ["formattingProvider" .= (provider :: Value)] ]
sendNotification WorkspaceDidChangeConfiguration (DidChangeConfigurationParams (formatLspConfig "ormolu"))
doc <- openDoc "Format.hs" "haskell"
formatDoc doc (FormattingOptions 2 True)
Expand All @@ -131,49 +160,12 @@ ormoluTests = testGroup "ormolu" [
formatLspConfig :: Value -> Value
formatLspConfig provider = object [ "haskell" .= object ["formattingProvider" .= (provider :: Value)] ]

-- | The same as 'formatLspConfig' but using the legacy section name
formatLspConfigOld :: Value -> Value
formatLspConfigOld provider = object [ "languageServerHaskell" .= object ["formattingProvider" .= (provider :: Value)] ]

formatConfig :: Value -> SessionConfig
formatConfig provider = defaultConfig { lspConfig = Just (formatLspConfig provider) }

goldenGitDiff :: FilePath -> FilePath -> [String]
goldenGitDiff fRef fNew = ["git", "diff", "--no-index", "--text", "--exit-code", fRef, fNew]


formattedBrittany :: T.Text
formattedBrittany =
"module Format where\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\n\
\data Baz = Baz { a :: Int, b :: String }\n\n"

formattedFloskell :: T.Text
formattedFloskell =
"module Format where\n\
\\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\n\
\data Baz = Baz { a :: Int, b :: String }\n\n"

formattedBrittanyPostFloskell :: T.Text
formattedBrittanyPostFloskell =
"module Format where\n\
\\n\
\foo :: Int -> Int\n\
\foo 3 = 2\n\
\foo x = x\n\
\\n\
\bar :: String -> IO String\n\
\bar s = do\n\
\ x <- return \"hello\"\n\
\ return \"asdf\"\n\n\
\data Baz = Baz { a :: Int, b :: String }\n\n"
4 changes: 2 additions & 2 deletions test/testdata/BrittanyCRLF.formatted_document.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
foo :: Int -> String -> IO ()
foo x y = do
print x
return 42
print x
return 42
6 changes: 3 additions & 3 deletions test/testdata/BrittanyCRLF.formatted_range.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
foo :: Int -> String -> IO ()
foo :: Int -> String-> IO ()
foo x y = do
print x
return 42
print x
return 42
4 changes: 2 additions & 2 deletions test/testdata/BrittanyLF.formatted_document.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
foo :: Int -> String -> IO ()
foo x y = do
print x
return 42
print x
return 42
6 changes: 3 additions & 3 deletions test/testdata/BrittanyLF.formatted_range.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
foo :: Int -> String -> IO ()
foo :: Int -> String-> IO ()
foo x y = do
print x
return 42
print x
return 42
11 changes: 11 additions & 0 deletions test/testdata/Format.brittany.formatted.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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 }

13 changes: 13 additions & 0 deletions test/testdata/Format.brittany_post_floskell.formatted.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 }

13 changes: 13 additions & 0 deletions test/testdata/Format.floskell.formatted.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 }

Loading

0 comments on commit 0aae68b

Please sign in to comment.