-
-
Notifications
You must be signed in to change notification settings - Fork 391
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
berberman
merged 18 commits into
haskell:master
from
berberman:declarative-custom-config
Mar 19, 2021
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cc8e4fd
Support declarative custom config, add --vscode-extension-schema
berberman 3a0b101
Add globalOn
berberman 5a2ed74
Merge branch 'master' into declarative-custom-config
berberman 8ba5e6e
Add --generate-default-config
berberman 9c0d0ff
Port tactic plugin
berberman dfa856f
Fix build of tactic plugin test
berberman a364178
Fix tactic plugin test
berberman 26d1277
Revert format changes in tactics plugin
berberman 14018f8
Change the descriptor of tactics plugin to "tactics"
berberman d626aa8
Update plugins/hls-tactics-plugin/src/Wingman/LanguageServer.hs
berberman 052b1eb
Merge branch 'master' into declarative-custom-config
berberman 0960bb7
Apply a bunch of @isovector's suggestions
berberman 2724d9b
Merge branch 'master' into declarative-custom-config
berberman 8106ea4
Merge branch 'master' into declarative-custom-config
berberman 03088ee
Document Ide.Plugin.ConfigUtils
berberman fa4662c
Merge branch 'master' into declarative-custom-config
mergify[bot] 0bda527
Add TInteger
berberman f79bd34
Fix build
berberman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
module Ide.Plugin.ConfigUtils where | ||
berberman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import qualified Data.Aeson as A | ||
import qualified Data.Aeson.Types as A | ||
import Data.Default (def) | ||
import qualified Data.Dependent.Map as DMap | ||
import qualified Data.Dependent.Sum as DSum | ||
import qualified Data.HashMap.Lazy as HMap | ||
import qualified Data.Map as Map | ||
import Ide.Plugin.Config | ||
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 -- inplace the "plugin" section with our 'elems', leaving others unchanged | ||
) | ||
"haskell" | ||
(unsafeValueToObject (A.toJSON defaultConfig)) | ||
where | ||
defaultConfig@Config {} = def | ||
unsafeValueToObject (A.Object o) = o | ||
unsafeValueToObject _ = error "impossible" | ||
berberman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 = 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 trivial configuration for formatters: | ||
-- | ||
-- "stylish-haskell": { | ||
-- "globalOn": true | ||
-- } | ||
genericDefaultConfig = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
STextDocumentCodeLens -> ["codeLensOn" A..= True] | ||
STextDocumentRename -> ["renameOn" A..= True] | ||
STextDocumentHover -> ["hoverOn" A..= True] | ||
STextDocumentDocumentSymbol -> ["symbolsOn" A..= True] | ||
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 | ||
singlePlugin PluginDescriptor {..} = genericSchema <> dedicatedSchema | ||
where | ||
(PluginHandlers (DMap.toList -> handlers)) = pluginHandlers | ||
customConfigToDedicatedSchema (CustomConfig p) = toVSCodeExtensionSchema (withIdPrefix "config.") p | ||
(PluginId pId) = pluginId | ||
genericSchema = withIdPrefix "globalOn" A..= schemaEntry "plugin" : mconcat (handlersToGenericSchema <$> handlers) | ||
dedicatedSchema = customConfigToDedicatedSchema pluginCustomConfig | ||
handlersToGenericSchema (IdeMethod m DSum.:=> _) = case m of | ||
STextDocumentCodeAction -> [withIdPrefix "codeActionsOn" A..= schemaEntry "code actions"] | ||
STextDocumentCodeLens -> [withIdPrefix "codeLensOn" A..= schemaEntry "code lenses"] | ||
STextDocumentRename -> [withIdPrefix "renameOn" A..= schemaEntry "rename"] | ||
STextDocumentHover -> [withIdPrefix "hoverOn" A..= schemaEntry "hover"] | ||
STextDocumentDocumentSymbol -> [withIdPrefix "symbolsOn" A..= schemaEntry "symbols"] | ||
STextDocumentCompletion -> [withIdPrefix "completionOn" A..= schemaEntry "completions"] | ||
_ -> [] | ||
schemaEntry desc = | ||
A.object | ||
[ "scope" A..= A.String "resource", | ||
"type" A..= A.String "boolean", | ||
"default" A..= True, | ||
"description" A..= A.String ("Enables " <> pId <> " " <> desc) | ||
] | ||
withIdPrefix x = "haskell.plugin." <> pId <> "." <> x |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.