@@ -10,12 +10,14 @@ module Ide.Plugin.Config
10
10
getInitialConfig
11
11
, getConfigFromNotification
12
12
, Config (.. )
13
+ , parseConfig
13
14
, PluginConfig (.. )
14
15
, CheckParents (.. )
15
16
) where
16
17
17
18
import Control.Applicative
18
19
import qualified Data.Aeson as A
20
+ import qualified Data.Aeson.Types as A
19
21
import Data.Aeson hiding ( Error )
20
22
import Data.Default
21
23
import qualified Data.Text as T
@@ -27,18 +29,18 @@ import GHC.Generics (Generic)
27
29
28
30
-- | Given a DidChangeConfigurationNotification message, this function returns the parsed
29
31
-- Config object if possible.
30
- getConfigFromNotification :: DidChangeConfigurationNotification -> Either T. Text Config
31
- getConfigFromNotification (NotificationMessage _ _ (DidChangeConfigurationParams p)) =
32
- case fromJSON p of
32
+ getConfigFromNotification :: Config -> DidChangeConfigurationNotification -> Either T. Text Config
33
+ getConfigFromNotification defaultValue (NotificationMessage _ _ (DidChangeConfigurationParams p)) =
34
+ case A. parse (parseConfig defaultValue) p of
33
35
A. Success c -> Right c
34
36
A. Error err -> Left $ T. pack err
35
37
36
38
-- | Given an InitializeRequest message, this function returns the parsed
37
39
-- Config object if possible. Otherwise, it returns the default configuration
38
- getInitialConfig :: InitializeRequest -> Either T. Text Config
39
- getInitialConfig (RequestMessage _ _ _ InitializeParams {_initializationOptions = Nothing }) = Right def
40
- getInitialConfig (RequestMessage _ _ _ InitializeParams {_initializationOptions = Just opts}) =
41
- case fromJSON opts of
40
+ getInitialConfig :: Config -> InitializeRequest -> Either T. Text Config
41
+ getInitialConfig defaultValue (RequestMessage _ _ _ InitializeParams {_initializationOptions = Nothing }) = Right defaultValue
42
+ getInitialConfig defaultValue (RequestMessage _ _ _ InitializeParams {_initializationOptions = Just opts}) =
43
+ case A. parse (parseConfig defaultValue) opts of
42
44
A. Success c -> Right c
43
45
A. Error err -> Left $ T. pack err
44
46
@@ -93,35 +95,26 @@ instance Default Config where
93
95
}
94
96
95
97
-- TODO: Add API for plugins to expose their own LSP config options
96
- instance A. FromJSON Config where
97
- parseJSON = A. withObject " Config" $ \ v -> do
98
+ parseConfig :: Config -> Value -> A. Parser Config
99
+ parseConfig defValue = A. withObject " Config" $ \ v -> do
98
100
-- Officially, we use "haskell" as the section name but for
99
101
-- backwards compatibility we also accept "languageServerHaskell"
100
102
c <- v .: " haskell" <|> v .:? " languageServerHaskell"
101
103
case c of
102
- Nothing -> return def
104
+ Nothing -> return defValue
103
105
Just s -> flip (A. withObject " Config.settings" ) s $ \ o -> Config
104
- <$> (o .:? " checkParents" <|> v .:? " checkParents" ) .!= checkParents def
105
- <*> (o .:? " checkProject" <|> v .:? " checkProject" ) .!= checkProject def
106
- <*> o .:? " hlintOn" .!= hlintOn def
107
- <*> o .:? " diagnosticsOnChange" .!= diagnosticsOnChange def
108
- <*> o .:? " maxNumberOfProblems" .!= maxNumberOfProblems def
109
- <*> o .:? " diagnosticsDebounceDuration" .!= diagnosticsDebounceDuration def
110
- <*> o .:? " liquidOn" .!= liquidOn def
111
- <*> o .:? " completionSnippetsOn" .!= completionSnippetsOn def
112
- <*> o .:? " formatOnImportOn" .!= formatOnImportOn def
113
- <*> o .:? " formattingProvider" .!= formattingProvider def
114
- <*> o .:? " maxCompletions" .!= maxCompletions def
115
- <*> o .:? " plugin" .!= plugins def
116
-
117
- -- 2017-10-09 23:22:00.710515298 [ThreadId 11] - ---> {"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"haskell":{"maxNumberOfProblems":100,"hlintOn":true}}}}
118
- -- 2017-10-09 23:22:00.710667381 [ThreadId 15] - reactor:got didChangeConfiguration notification:
119
- -- NotificationMessage
120
- -- {_jsonrpc = "2.0"
121
- -- , _method = WorkspaceDidChangeConfiguration
122
- -- , _params = DidChangeConfigurationParams
123
- -- {_settings = Object (fromList [("haskell",Object (fromList [("hlintOn",Bool True)
124
- -- ,("maxNumberOfProblems",Number 100.0)]))])}}
106
+ <$> (o .:? " checkParents" <|> v .:? " checkParents" ) .!= checkParents defValue
107
+ <*> (o .:? " checkProject" <|> v .:? " checkProject" ) .!= checkProject defValue
108
+ <*> o .:? " hlintOn" .!= hlintOn defValue
109
+ <*> o .:? " diagnosticsOnChange" .!= diagnosticsOnChange defValue
110
+ <*> o .:? " maxNumberOfProblems" .!= maxNumberOfProblems defValue
111
+ <*> o .:? " diagnosticsDebounceDuration" .!= diagnosticsDebounceDuration defValue
112
+ <*> o .:? " liquidOn" .!= liquidOn defValue
113
+ <*> o .:? " completionSnippetsOn" .!= completionSnippetsOn defValue
114
+ <*> o .:? " formatOnImportOn" .!= formatOnImportOn defValue
115
+ <*> o .:? " formattingProvider" .!= formattingProvider defValue
116
+ <*> o .:? " maxCompletions" .!= maxCompletions defValue
117
+ <*> o .:? " plugin" .!= plugins defValue
125
118
126
119
instance A. ToJSON Config where
127
120
toJSON Config {.. } =
0 commit comments