Skip to content

Commit

Permalink
autodoc: support enums too
Browse files Browse the repository at this point in the history
  • Loading branch information
develop7 committed Oct 11, 2024
1 parent 0a22218 commit e84845f
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 80 deletions.
107 changes: 81 additions & 26 deletions hls-plugin-api/src/Ide/Plugin/ConfigUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import Data.String (IsString (fromString))
import qualified Data.Text as T
import GHC.TypeLits (symbolVal)
import Ide.Plugin.Config
import Ide.Plugin.Properties (MetaData (..), Properties (..),
import Ide.Plugin.Properties (KeyNameProxy, MetaData (..),
PluginCustomConfig (..),
PluginCustomConfigParam (..),
Properties (..),
SPropertyKey (..),
SomePropertyKeyWithMetaData (..),
toDefaultJSON,
toVSCodeExtensionSchema)
import Ide.Types
Expand Down Expand Up @@ -142,41 +147,91 @@ pluginsToVSCodeExtensionSchema IdePlugins {..} = A.object $ mconcat $ singlePlug
withIdPrefix x = "haskell.plugin." <> pId <> "." <> x
toKey' = fromString . T.unpack . withIdPrefix

data PluginCustomConfig = PluginCustomConfig {
pccHeader :: T.Text,
pccParams :: [PluginCustomConfigParam]
}
data PluginCustomConfigParam = PluginCustomConfigParam {
pccpName :: T.Text,
pccpDescription :: T.Text,
pccpIsDefault :: Bool
}

-- | Generates markdown tables for custom config
pluginsCustomConfigToMarkdownTables :: IdePlugins a -> T.Text
pluginsCustomConfigToMarkdownTables IdePlugins {..} = T.unlines
$ map renderCfg
$ filter (\(PluginCustomConfig _ params) -> not $ null params)
$ map pluginCfg ipMap
$ map toPluginCustomConfig ipMap
where
toPluginCustomConfig :: PluginDescriptor ideState -> PluginCustomConfig
toPluginCustomConfig PluginDescriptor {pluginConfigDescriptor = ConfigDescriptor {configCustomConfig = c}, pluginId = PluginId pId} =
PluginCustomConfig { pcc'Name = pId, pcc'Params = toPluginCustomConfigParams c}
toPluginCustomConfigParams :: CustomConfig -> [PluginCustomConfigParam]
toPluginCustomConfigParams (CustomConfig p) = toPluginCustomConfigParams' p
toPluginCustomConfigParams' :: Properties r -> [PluginCustomConfigParam]
toPluginCustomConfigParams' EmptyProperties = []
toPluginCustomConfigParams' (ConsProperties (keyNameProxy :: KeyNameProxy s) (k :: SPropertyKey k) (m :: MetaData t) xs) =
toEntry (SomePropertyKeyWithMetaData k m) : toPluginCustomConfigParams' xs
where
toEntry :: SomePropertyKeyWithMetaData -> PluginCustomConfigParam
toEntry (SomePropertyKeyWithMetaData SNumber MetaData {..}) =
PluginCustomConfigParam {
pccp'Name = T.pack $ symbolVal keyNameProxy,
pccp'Description = description,
pccp'Default = T.pack $ show defaultValue,
pccp'EnumValues = []
}
toEntry (SomePropertyKeyWithMetaData SInteger MetaData {..}) =
PluginCustomConfigParam {
pccp'Name = T.pack $ symbolVal keyNameProxy,
pccp'Description = description,
pccp'Default = T.pack $ show defaultValue,
pccp'EnumValues = []
}
toEntry (SomePropertyKeyWithMetaData SString MetaData {..}) =
PluginCustomConfigParam {
pccp'Name = T.pack $ symbolVal keyNameProxy,
pccp'Description = description,
pccp'Default = T.pack $ show defaultValue,
pccp'EnumValues = []
}
toEntry (SomePropertyKeyWithMetaData SBoolean MetaData {..}) =
PluginCustomConfigParam {
pccp'Name = T.pack $ symbolVal keyNameProxy,
pccp'Description = description,
pccp'Default = T.pack $ show defaultValue,
pccp'EnumValues = []
}
toEntry (SomePropertyKeyWithMetaData (SObject _) MetaData {..}) =
PluginCustomConfigParam {
pccp'Name = T.pack $ symbolVal keyNameProxy,
pccp'Description = description,
pccp'Default = "TODO: nested object", -- T.pack $ show defaultValue,
pccp'EnumValues = []
}
toEntry (SomePropertyKeyWithMetaData (SArray _) MetaData {..}) =
PluginCustomConfigParam {
pccp'Name = T.pack $ symbolVal keyNameProxy,
pccp'Description = description,
pccp'Default = "TODO: Array values", -- T.pack $ show defaultValue,
pccp'EnumValues = []
}
toEntry (SomePropertyKeyWithMetaData (SEnum _) EnumMetaData {..}) =
PluginCustomConfigParam {
pccp'Name = T.pack $ symbolVal keyNameProxy,
pccp'Description = description,
pccp'Default = T.pack $ show defaultValue,
pccp'EnumValues = map (T.pack . show) enumValues
}
toEntry (SomePropertyKeyWithMetaData SProperties PropertiesMetaData {..}) =
PluginCustomConfigParam {
pccp'Name = T.pack $ symbolVal keyNameProxy,
pccp'Description = description,
pccp'Default = T.pack $ show defaultValue,
pccp'EnumValues = []
}
renderCfg :: PluginCustomConfig -> T.Text
renderCfg (PluginCustomConfig pId pccParams) =
T.unlines (pluginHeader : tableHeader : rows pccParams)
where
pluginHeader = "## " <> pId
tableHeader = "| Property | Description | Default |" <> "\n" <> "| --- | --- | --- |"
tableHeader =
"| Property | Description | Default | Allowed values |" <> "\n" <>
"| --- | --- | --- | --- |"
rows = map renderRow
renderRow (PluginCustomConfigParam name desc isDefault) =
"| `" <> name <> "` | " <> desc <> " | " <> if isDefault then "Yes" else "No" <> " |"
pluginCfg :: PluginDescriptor r -> PluginCustomConfig
pluginCfg PluginDescriptor {pluginConfigDescriptor = ConfigDescriptor {configCustomConfig = c}, pluginId = PluginId pId} =
PluginCustomConfig pId (pccProcess c)
where
pccProcess :: CustomConfig -> [PluginCustomConfigParam]
pccProcess (CustomConfig EmptyProperties) = mempty
pccProcess (CustomConfig (ConsProperties keyNameProxy _k m xs)) =
let (desc, isDefault) = case m of
PropertiesMetaData _ desc _ -> (desc, False)
EnumMetaData _ desc _ _ -> (desc, True)
MetaData _ desc -> (desc, False)
in PluginCustomConfigParam (T.pack $ symbolVal keyNameProxy) desc isDefault : pccProcess (CustomConfig xs)
renderRow PluginCustomConfigParam {..} =
"| `" <> pccp'Name <> "` | " <> pccp'Description <> " | `" <> pccp'Default <> "` | " <> renderEnum pccp'EnumValues <> " |"
renderEnum [] = " &nbsp; " -- Placeholder to prevent missing cells
renderEnum vs = "<ul> " <> (T.intercalate " " $ map (\x -> "<li><code>" <> x <> "</code></li>") vs) <> " </ul>"
14 changes: 14 additions & 0 deletions hls-plugin-api/src/Ide/Plugin/Properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module Ide.Plugin.Properties
usePropertyByPathEither,
usePropertyByPath,
(&),
PluginCustomConfig(..),
PluginCustomConfigParam(..),
)
where

Expand Down Expand Up @@ -517,3 +519,15 @@ toVSCodeExtensionSchema' ps = case ps of
]
(SomePropertyKeyWithMetaData SProperties PropertiesMetaData {..}) ->
map (first Just) $ toVSCodeExtensionSchema' childrenProperties

data PluginCustomConfig = PluginCustomConfig {
pcc'Name :: T.Text,
pcc'Params :: [PluginCustomConfigParam]
}
data PluginCustomConfigParam = PluginCustomConfigParam {
pccp'Name :: T.Text,
pccp'Description :: T.Text,
pccp'Default :: T.Text,
pccp'EnumValues :: [T.Text]
}

Loading

0 comments on commit e84845f

Please sign in to comment.