|
| 1 | +{-# LANGUAGE RecordWildCards #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +{-# LANGUAGE FlexibleInstances #-} |
| 4 | +{-# LANGUAGE OverloadedStrings #-} |
| 5 | +{-# LANGUAGE TypeFamilies #-} |
| 6 | +module Ide.Plugin.Config |
| 7 | + ( |
| 8 | + getInitialConfig |
| 9 | + , getConfigFromNotification |
| 10 | + , Config(..) |
| 11 | + ) where |
| 12 | + |
| 13 | +import qualified Data.Aeson as A |
| 14 | +import Data.Aeson hiding ( Error ) |
| 15 | +import Data.Default |
| 16 | +import qualified Data.Text as T |
| 17 | +import Language.Haskell.LSP.Types |
| 18 | + |
| 19 | +-- --------------------------------------------------------------------- |
| 20 | + |
| 21 | +-- | Given a DidChangeConfigurationNotification message, this function returns the parsed |
| 22 | +-- Config object if possible. |
| 23 | +getConfigFromNotification :: DidChangeConfigurationNotification -> Either T.Text Config |
| 24 | +getConfigFromNotification (NotificationMessage _ _ (DidChangeConfigurationParams p)) = |
| 25 | + case fromJSON p of |
| 26 | + A.Success c -> Right c |
| 27 | + A.Error err -> Left $ T.pack err |
| 28 | + |
| 29 | +-- | Given an InitializeRequest message, this function returns the parsed |
| 30 | +-- Config object if possible. Otherwise, it returns the default configuration |
| 31 | +getInitialConfig :: InitializeRequest -> Either T.Text Config |
| 32 | +getInitialConfig (RequestMessage _ _ _ InitializeParams{_initializationOptions = Nothing }) = Right def |
| 33 | +getInitialConfig (RequestMessage _ _ _ InitializeParams{_initializationOptions = Just opts}) = |
| 34 | + case fromJSON opts of |
| 35 | + A.Success c -> Right c |
| 36 | + A.Error err -> Left $ T.pack err |
| 37 | + |
| 38 | +-- --------------------------------------------------------------------- |
| 39 | + |
| 40 | +-- | We (initially anyway) mirror the hie configuration, so that existing |
| 41 | +-- clients can simply switch executable and not have any nasty surprises. There |
| 42 | +-- will be surprises relating to config options being ignored, initially though. |
| 43 | +data Config = |
| 44 | + Config |
| 45 | + { hlintOn :: Bool |
| 46 | + , diagnosticsOnChange :: Bool |
| 47 | + , maxNumberOfProblems :: Int |
| 48 | + , diagnosticsDebounceDuration :: Int |
| 49 | + , liquidOn :: Bool |
| 50 | + , completionSnippetsOn :: Bool |
| 51 | + , formatOnImportOn :: Bool |
| 52 | + , formattingProvider :: T.Text |
| 53 | + } deriving (Show,Eq) |
| 54 | + |
| 55 | +instance Default Config where |
| 56 | + def = Config |
| 57 | + { hlintOn = True |
| 58 | + , diagnosticsOnChange = True |
| 59 | + , maxNumberOfProblems = 100 |
| 60 | + , diagnosticsDebounceDuration = 350000 |
| 61 | + , liquidOn = False |
| 62 | + , completionSnippetsOn = True |
| 63 | + , formatOnImportOn = True |
| 64 | + -- , formattingProvider = "brittany" |
| 65 | + , formattingProvider = "ormolu" |
| 66 | + } |
| 67 | + |
| 68 | +-- TODO: Add API for plugins to expose their own LSP config options |
| 69 | +instance A.FromJSON Config where |
| 70 | + parseJSON = A.withObject "Config" $ \v -> do |
| 71 | + s <- v .: "languageServerHaskell" |
| 72 | + flip (A.withObject "Config.settings") s $ \o -> Config |
| 73 | + <$> o .:? "hlintOn" .!= hlintOn def |
| 74 | + <*> o .:? "diagnosticsOnChange" .!= diagnosticsOnChange def |
| 75 | + <*> o .:? "maxNumberOfProblems" .!= maxNumberOfProblems def |
| 76 | + <*> o .:? "diagnosticsDebounceDuration" .!= diagnosticsDebounceDuration def |
| 77 | + <*> o .:? "liquidOn" .!= liquidOn def |
| 78 | + <*> o .:? "completionSnippetsOn" .!= completionSnippetsOn def |
| 79 | + <*> o .:? "formatOnImportOn" .!= formatOnImportOn def |
| 80 | + <*> o .:? "formattingProvider" .!= formattingProvider def |
| 81 | + |
| 82 | +-- 2017-10-09 23:22:00.710515298 [ThreadId 11] - ---> {"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"languageServerHaskell":{"maxNumberOfProblems":100,"hlintOn":true}}}} |
| 83 | +-- 2017-10-09 23:22:00.710667381 [ThreadId 15] - reactor:got didChangeConfiguration notification: |
| 84 | +-- NotificationMessage |
| 85 | +-- {_jsonrpc = "2.0" |
| 86 | +-- , _method = WorkspaceDidChangeConfiguration |
| 87 | +-- , _params = DidChangeConfigurationParams |
| 88 | +-- {_settings = Object (fromList [("languageServerHaskell",Object (fromList [("hlintOn",Bool True) |
| 89 | +-- ,("maxNumberOfProblems",Number 100.0)]))])}} |
| 90 | + |
| 91 | +instance A.ToJSON Config where |
| 92 | + toJSON (Config h diag m d l c f fp) = object [ "languageServerHaskell" .= r ] |
| 93 | + where |
| 94 | + r = object [ "hlintOn" .= h |
| 95 | + , "diagnosticsOnChange" .= diag |
| 96 | + , "maxNumberOfProblems" .= m |
| 97 | + , "diagnosticsDebounceDuration" .= d |
| 98 | + , "liquidOn" .= l |
| 99 | + , "completionSnippetsOn" .= c |
| 100 | + , "formatOnImportOn" .= f |
| 101 | + , "formattingProvider" .= fp |
| 102 | + ] |
0 commit comments