55
66module Ide.Plugin.ConfigUtils where
77
8+ import Control.Lens (at , ix , (&) , (?~) )
89import qualified Data.Aeson as A
10+ import Data.Aeson.Lens (_Object )
911import qualified Data.Aeson.Types as A
1012import Data.Default (def )
1113import qualified Data.Dependent.Map as DMap
1214import qualified Data.Dependent.Sum as DSum
13- import qualified Data.HashMap.Lazy as HMap
1415import Data.List (nub )
16+ import Data.String (IsString (fromString ))
17+ import qualified Data.Text as T
1518import Ide.Plugin.Config
1619import Ide.Plugin.Properties (toDefaultJSON , toVSCodeExtensionSchema )
1720import Ide.Types
@@ -25,17 +28,12 @@ import Language.LSP.Types
2528-- | Generates a default 'Config', but remains only effective items
2629pluginsToDefaultConfig :: IdePlugins a -> A. Value
2730pluginsToDefaultConfig IdePlugins {.. } =
28- A. Object $
29- HMap. adjust
30- ( \ (unsafeValueToObject -> o) ->
31- A. Object $ HMap. insert " plugin" elems o -- inplace the "plugin" section with our 'elems', leaving others unchanged
32- )
33- " haskell"
34- (unsafeValueToObject (A. toJSON defaultConfig))
31+ -- Use 'ix' to look at all the "haskell" keys in the outer value (since we're not
32+ -- setting it if missing), then we use '_Object' and 'at' to get at the "plugin" key
33+ -- and actually set it.
34+ A. toJSON defaultConfig & ix " haskell" . _Object . at " plugin" ?~ elems
3535 where
3636 defaultConfig@ Config {} = def
37- unsafeValueToObject (A. Object o) = o
38- unsafeValueToObject _ = error " impossible"
3937 elems = A. object $ mconcat $ singlePlugin <$> map snd ipMap
4038 -- Splice genericDefaultConfig and dedicatedDefaultConfig
4139 -- Example:
@@ -52,7 +50,7 @@ pluginsToDefaultConfig IdePlugins {..} =
5250 -- }
5351 singlePlugin PluginDescriptor {pluginConfigDescriptor = ConfigDescriptor {.. }, .. } =
5452 let x = genericDefaultConfig <> dedicatedDefaultConfig
55- in [pId A. .= A. object x | not $ null x]
53+ in [fromString ( T. unpack pId) A. .= A. object x | not $ null x]
5654 where
5755 (PluginHandlers (DMap. toList -> handlers)) = pluginHandlers
5856 customConfigToDedicatedDefaultConfig (CustomConfig p) = toDefaultJSON p
@@ -107,22 +105,22 @@ pluginsToVSCodeExtensionSchema IdePlugins {..} = A.object $ mconcat $ singlePlug
107105 (PluginId pId) = pluginId
108106 genericSchema =
109107 let x =
110- [withIdPrefix " diagnosticsOn" A. .= schemaEntry " diagnostics" | configHasDiagnostics]
108+ [toKey' " diagnosticsOn" A. .= schemaEntry " diagnostics" | configHasDiagnostics]
111109 <> nub (mconcat (handlersToGenericSchema <$> handlers))
112110 in case x of
113111 -- If the plugin has only one capability, we produce globalOn instead of the specific one;
114112 -- otherwise we don't produce globalOn at all
115- [_] -> [withIdPrefix " globalOn" A. .= schemaEntry " plugin" ]
113+ [_] -> [toKey' " globalOn" A. .= schemaEntry " plugin" ]
116114 _ -> x
117115 dedicatedSchema = customConfigToDedicatedSchema configCustomConfig
118116 handlersToGenericSchema (IdeMethod m DSum. :=> _) = case m of
119- STextDocumentCodeAction -> [withIdPrefix " codeActionsOn" A. .= schemaEntry " code actions" ]
120- STextDocumentCodeLens -> [withIdPrefix " codeLensOn" A. .= schemaEntry " code lenses" ]
121- STextDocumentRename -> [withIdPrefix " renameOn" A. .= schemaEntry " rename" ]
122- STextDocumentHover -> [withIdPrefix " hoverOn" A. .= schemaEntry " hover" ]
123- STextDocumentDocumentSymbol -> [withIdPrefix " symbolsOn" A. .= schemaEntry " symbols" ]
124- STextDocumentCompletion -> [withIdPrefix " completionOn" A. .= schemaEntry " completions" ]
125- STextDocumentPrepareCallHierarchy -> [withIdPrefix " callHierarchyOn" A. .= schemaEntry " call hierarchy" ]
117+ STextDocumentCodeAction -> [toKey' " codeActionsOn" A. .= schemaEntry " code actions" ]
118+ STextDocumentCodeLens -> [toKey' " codeLensOn" A. .= schemaEntry " code lenses" ]
119+ STextDocumentRename -> [toKey' " renameOn" A. .= schemaEntry " rename" ]
120+ STextDocumentHover -> [toKey' " hoverOn" A. .= schemaEntry " hover" ]
121+ STextDocumentDocumentSymbol -> [toKey' " symbolsOn" A. .= schemaEntry " symbols" ]
122+ STextDocumentCompletion -> [toKey' " completionOn" A. .= schemaEntry " completions" ]
123+ STextDocumentPrepareCallHierarchy -> [toKey' " callHierarchyOn" A. .= schemaEntry " call hierarchy" ]
126124 _ -> []
127125 schemaEntry desc =
128126 A. object
@@ -132,3 +130,4 @@ pluginsToVSCodeExtensionSchema IdePlugins {..} = A.object $ mconcat $ singlePlug
132130 " description" A. .= A. String (" Enables " <> pId <> " " <> desc)
133131 ]
134132 withIdPrefix x = " haskell.plugin." <> pId <> " ." <> x
133+ toKey' = fromString . T. unpack . withIdPrefix
0 commit comments