Skip to content

Regularize custom config of plugins #1576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 19, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions hls-plugin-api/src/Ide/Plugin/ConfigUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ import Ide.Plugin.Properties (toDefaultJSON, toVSCodeExtensionSchema)
import Ide.Types
import Language.LSP.Types

-- Attention:
-- 'diagnosticsOn' will never be added into the default config or the schema,
-- since diagnostics emit in arbitrary shake rules -- we don't know
-- whether a plugin is capable of producing diagnostics.

-- | Generates a defalut 'Config', but remains only effective items
pluginsToDefaultConfig :: IdePlugins a -> A.Value
pluginsToDefaultConfig IdePlugins {..} =
A.Object $
HMap.adjust
( \(unsafeValueToObject -> o) ->
A.Object $ HMap.insert "plugin" elems o
A.Object $ HMap.insert "plugin" elems o -- inplace the "plugin" section with our 'elems', leaving others unchanged
)
"haskell"
(unsafeValueToObject (A.toJSON defaultConfig))
Expand All @@ -31,25 +37,56 @@ pluginsToDefaultConfig IdePlugins {..} =
unsafeValueToObject (A.Object o) = o
unsafeValueToObject _ = error "impossible"
elems = A.object $ mconcat $ singlePlugin <$> Map.elems ipMap
-- Splice genericDefaultConfig and dedicatedDefaultConfig
-- Example:
--
-- {
-- "plugin-id": {
-- "globalOn": true,
-- "codeActionsOn": true,
-- "codeLensOn": true,
-- "config": {
-- "property1": "foo"
-- }
-- }
-- }
singlePlugin PluginDescriptor {..} =
let x = geenericDefaultConfig <> dedicatedDefaultConfig
let x = genericDefaultConfig <> dedicatedDefaultConfig
in [pId A..= A.object x | not $ null x]
where
(PluginHandlers (DMap.toList -> handlers)) = pluginHandlers
customConfigToDedicatedDefaultConfig (CustomConfig p) = toDefaultJSON p
-- Example:
--
-- {
-- "globalOn": true,
-- "codeActionsOn": true,
-- "codeLensOn": true
-- }
--
-- we don't generate the config section if the plugin doesn't register any of the following six methods,
-- which avoids producing redundant configuration for formatters:
-- which avoids producing trivial configuration for formatters:
--
-- "stylish-haskell": {
-- "globalOn": true
-- }
geenericDefaultConfig =
genericDefaultConfig =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, really nice

let x = mconcat (handlersToGenericDefaultConfig <$> handlers)
in ["globalOn" A..= True | not $ null x] <> x
-- Example:
--
-- {
-- "config": {
-- "property1": "foo"
-- }
--}
dedicatedDefaultConfig =
let x = customConfigToDedicatedDefaultConfig pluginCustomConfig
in ["config" A..= A.object x | not $ null x]

(PluginId pId) = pluginId

-- This function captures ide methods registered by the plugin, and then converts it to kv pairs
handlersToGenericDefaultConfig :: DSum.DSum IdeMethod f -> [A.Pair]
handlersToGenericDefaultConfig (IdeMethod m DSum.:=> _) = case m of
STextDocumentCodeAction -> ["codeActionsOn" A..= True]
Expand All @@ -60,6 +97,8 @@ pluginsToDefaultConfig IdePlugins {..} =
STextDocumentCompletion -> ["completionOn" A..= True]
_ -> []

-- | Generates json schema used in haskell vscode extension
-- Similar to 'pluginsToDefaultConfig' but simpler, since schema has a flatten structure
pluginsToVSCodeExtensionSchema :: IdePlugins a -> A.Value
pluginsToVSCodeExtensionSchema IdePlugins {..} = A.object $ mconcat $ singlePlugin <$> Map.elems ipMap
where
Expand Down