Skip to content

Commit 836f958

Browse files
committed
autodoc: support enums too
1 parent 182bad5 commit 836f958

File tree

3 files changed

+149
-80
lines changed

3 files changed

+149
-80
lines changed

hls-plugin-api/src/Ide/Plugin/ConfigUtils.hs

+81-26
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import Data.String (IsString (fromString))
1717
import qualified Data.Text as T
1818
import GHC.TypeLits (symbolVal)
1919
import Ide.Plugin.Config
20-
import Ide.Plugin.Properties (MetaData (..), Properties (..),
20+
import Ide.Plugin.Properties (KeyNameProxy, MetaData (..),
21+
PluginCustomConfig (..),
22+
PluginCustomConfigParam (..),
23+
Properties (..),
24+
SPropertyKey (..),
25+
SomePropertyKeyWithMetaData (..),
2126
toDefaultJSON,
2227
toVSCodeExtensionSchema)
2328
import Ide.Types
@@ -142,41 +147,91 @@ pluginsToVSCodeExtensionSchema IdePlugins {..} = A.object $ mconcat $ singlePlug
142147
withIdPrefix x = "haskell.plugin." <> pId <> "." <> x
143148
toKey' = fromString . T.unpack . withIdPrefix
144149

145-
data PluginCustomConfig = PluginCustomConfig {
146-
pccHeader :: T.Text,
147-
pccParams :: [PluginCustomConfigParam]
148-
}
149-
data PluginCustomConfigParam = PluginCustomConfigParam {
150-
pccpName :: T.Text,
151-
pccpDescription :: T.Text,
152-
pccpIsDefault :: Bool
153-
}
154150

155151
-- | Generates markdown tables for custom config
156152
pluginsCustomConfigToMarkdownTables :: IdePlugins a -> T.Text
157153
pluginsCustomConfigToMarkdownTables IdePlugins {..} = T.unlines
158154
$ map renderCfg
159155
$ filter (\(PluginCustomConfig _ params) -> not $ null params)
160-
$ map pluginCfg ipMap
156+
$ map toPluginCustomConfig ipMap
161157
where
158+
toPluginCustomConfig :: PluginDescriptor ideState -> PluginCustomConfig
159+
toPluginCustomConfig PluginDescriptor {pluginConfigDescriptor = ConfigDescriptor {configCustomConfig = c}, pluginId = PluginId pId} =
160+
PluginCustomConfig { pcc'Name = pId, pcc'Params = toPluginCustomConfigParams c}
161+
toPluginCustomConfigParams :: CustomConfig -> [PluginCustomConfigParam]
162+
toPluginCustomConfigParams (CustomConfig p) = toPluginCustomConfigParams' p
163+
toPluginCustomConfigParams' :: Properties r -> [PluginCustomConfigParam]
164+
toPluginCustomConfigParams' EmptyProperties = []
165+
toPluginCustomConfigParams' (ConsProperties (keyNameProxy :: KeyNameProxy s) (k :: SPropertyKey k) (m :: MetaData t) xs) =
166+
toEntry (SomePropertyKeyWithMetaData k m) : toPluginCustomConfigParams' xs
167+
where
168+
toEntry :: SomePropertyKeyWithMetaData -> PluginCustomConfigParam
169+
toEntry (SomePropertyKeyWithMetaData SNumber MetaData {..}) =
170+
PluginCustomConfigParam {
171+
pccp'Name = T.pack $ symbolVal keyNameProxy,
172+
pccp'Description = description,
173+
pccp'Default = T.pack $ show defaultValue,
174+
pccp'EnumValues = []
175+
}
176+
toEntry (SomePropertyKeyWithMetaData SInteger MetaData {..}) =
177+
PluginCustomConfigParam {
178+
pccp'Name = T.pack $ symbolVal keyNameProxy,
179+
pccp'Description = description,
180+
pccp'Default = T.pack $ show defaultValue,
181+
pccp'EnumValues = []
182+
}
183+
toEntry (SomePropertyKeyWithMetaData SString MetaData {..}) =
184+
PluginCustomConfigParam {
185+
pccp'Name = T.pack $ symbolVal keyNameProxy,
186+
pccp'Description = description,
187+
pccp'Default = T.pack $ show defaultValue,
188+
pccp'EnumValues = []
189+
}
190+
toEntry (SomePropertyKeyWithMetaData SBoolean MetaData {..}) =
191+
PluginCustomConfigParam {
192+
pccp'Name = T.pack $ symbolVal keyNameProxy,
193+
pccp'Description = description,
194+
pccp'Default = T.pack $ show defaultValue,
195+
pccp'EnumValues = []
196+
}
197+
toEntry (SomePropertyKeyWithMetaData (SObject _) MetaData {..}) =
198+
PluginCustomConfigParam {
199+
pccp'Name = T.pack $ symbolVal keyNameProxy,
200+
pccp'Description = description,
201+
pccp'Default = "TODO: nested object", -- T.pack $ show defaultValue,
202+
pccp'EnumValues = []
203+
}
204+
toEntry (SomePropertyKeyWithMetaData (SArray _) MetaData {..}) =
205+
PluginCustomConfigParam {
206+
pccp'Name = T.pack $ symbolVal keyNameProxy,
207+
pccp'Description = description,
208+
pccp'Default = "TODO: Array values", -- T.pack $ show defaultValue,
209+
pccp'EnumValues = []
210+
}
211+
toEntry (SomePropertyKeyWithMetaData (SEnum _) EnumMetaData {..}) =
212+
PluginCustomConfigParam {
213+
pccp'Name = T.pack $ symbolVal keyNameProxy,
214+
pccp'Description = description,
215+
pccp'Default = T.pack $ show defaultValue,
216+
pccp'EnumValues = map (T.pack . show) enumValues
217+
}
218+
toEntry (SomePropertyKeyWithMetaData SProperties PropertiesMetaData {..}) =
219+
PluginCustomConfigParam {
220+
pccp'Name = T.pack $ symbolVal keyNameProxy,
221+
pccp'Description = description,
222+
pccp'Default = T.pack $ show defaultValue,
223+
pccp'EnumValues = []
224+
}
162225
renderCfg :: PluginCustomConfig -> T.Text
163226
renderCfg (PluginCustomConfig pId pccParams) =
164227
T.unlines (pluginHeader : tableHeader : rows pccParams)
165228
where
166229
pluginHeader = "## " <> pId
167-
tableHeader = "| Property | Description | Default |" <> "\n" <> "| --- | --- | --- |"
230+
tableHeader =
231+
"| Property | Description | Default | Allowed values |" <> "\n" <>
232+
"| --- | --- | --- | --- |"
168233
rows = map renderRow
169-
renderRow (PluginCustomConfigParam name desc isDefault) =
170-
"| `" <> name <> "` | " <> desc <> " | " <> if isDefault then "Yes" else "No" <> " |"
171-
pluginCfg :: PluginDescriptor r -> PluginCustomConfig
172-
pluginCfg PluginDescriptor {pluginConfigDescriptor = ConfigDescriptor {configCustomConfig = c}, pluginId = PluginId pId} =
173-
PluginCustomConfig pId (pccProcess c)
174-
where
175-
pccProcess :: CustomConfig -> [PluginCustomConfigParam]
176-
pccProcess (CustomConfig EmptyProperties) = mempty
177-
pccProcess (CustomConfig (ConsProperties keyNameProxy _k m xs)) =
178-
let (desc, isDefault) = case m of
179-
PropertiesMetaData _ desc _ -> (desc, False)
180-
EnumMetaData _ desc _ _ -> (desc, True)
181-
MetaData _ desc -> (desc, False)
182-
in PluginCustomConfigParam (T.pack $ symbolVal keyNameProxy) desc isDefault : pccProcess (CustomConfig xs)
234+
renderRow PluginCustomConfigParam {..} =
235+
"| `" <> pccp'Name <> "` | " <> pccp'Description <> " | `" <> pccp'Default <> "` | " <> renderEnum pccp'EnumValues <> " |"
236+
renderEnum [] = " &nbsp; " -- Placeholder to prevent missing cells
237+
renderEnum vs = "<ul> " <> (T.intercalate " " $ map (\x -> "<li><code>" <> x <> "</code></li>") vs) <> " </ul>"

hls-plugin-api/src/Ide/Plugin/Properties.hs

+14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ module Ide.Plugin.Properties
4343
usePropertyByPathEither,
4444
usePropertyByPath,
4545
(&),
46+
PluginCustomConfig(..),
47+
PluginCustomConfigParam(..),
4648
)
4749
where
4850

@@ -517,3 +519,15 @@ toVSCodeExtensionSchema' ps = case ps of
517519
]
518520
(SomePropertyKeyWithMetaData SProperties PropertiesMetaData {..}) ->
519521
map (first Just) $ toVSCodeExtensionSchema' childrenProperties
522+
523+
data PluginCustomConfig = PluginCustomConfig {
524+
pcc'Name :: T.Text,
525+
pcc'Params :: [PluginCustomConfigParam]
526+
}
527+
data PluginCustomConfigParam = PluginCustomConfigParam {
528+
pccp'Name :: T.Text,
529+
pccp'Description :: T.Text,
530+
pccp'Default :: T.Text,
531+
pccp'EnumValues :: [T.Text]
532+
}
533+

0 commit comments

Comments
 (0)