Skip to content

Commit 0bda527

Browse files
committed
Add TInteger
1 parent fa4662c commit 0bda527

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

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

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Ide.Plugin.Properties
2828
HasProperty,
2929
emptyProperties,
3030
defineNumberProperty,
31+
defineIntegerProperty,
3132
defineStringProperty,
3233
defineBooleanProperty,
3334
defineObjectProperty,
@@ -55,14 +56,16 @@ import Unsafe.Coerce (unsafeCoerce)
5556
-- | Types properties may have
5657
data PropertyType
5758
= TNumber
59+
| TInteger
5860
| TString
5961
| TBoolean
6062
| TObject
6163
| TArray
6264
| TEnum
6365

6466
type family ToHsType (t :: PropertyType) where
65-
ToHsType 'TNumber = Int
67+
ToHsType 'TNumber = Double -- in js, there are no distinct types for integers and floating-point values
68+
ToHsType 'TInteger = Int -- so here we use Double for Number, Int for Integer
6669
ToHsType 'TString = T.Text
6770
ToHsType 'TBoolean = Bool
6871
ToHsType 'TObject = A.Object
@@ -94,6 +97,7 @@ data PropertyKey = PropertyKey Symbol PropertyType
9497
-- | Singleton type of 'PropertyKey'
9598
data SPropertyKey (k :: PropertyKey) where
9699
SNumber :: SPropertyKey ('PropertyKey s 'TNumber)
100+
SInteger :: SPropertyKey ('PropertyKey s 'TInteger)
97101
SString :: SPropertyKey ('PropertyKey s 'TString)
98102
SBoolean :: SPropertyKey ('PropertyKey s 'TBoolean)
99103
SObject :: SPropertyKey ('PropertyKey s 'TObject)
@@ -106,7 +110,8 @@ data SomePropertyKeyWithMetaData
106110
(k ~ 'PropertyKey s t) =>
107111
SomePropertyKeyWithMetaData (SPropertyKey k) (MetaData t)
108112

109-
-- | 'Properties' defines a set of properties which used in dedicated configuration of a plugin.
113+
-- | 'Properties' is a partial implementation of json schema, without supporting union types and validation.
114+
-- In hls, it defines a set of properties which used in dedicated configuration of a plugin.
110115
-- A property is an immediate child of the json object in each plugin's "config" section.
111116
-- It was designed to be compatible with vscode's settings UI.
112117
-- Use 'emptyProperties' and 'useProperty' to create and consume 'Properties'.
@@ -202,7 +207,7 @@ usePropertyEither ::
202207
Properties r ->
203208
A.Object ->
204209
Either String (ToHsType t)
205-
usePropertyEither k p = parseProperty k (find k p)
210+
usePropertyEither kn p = parseProperty kn (find k p)
206211

207212
-- | Like 'usePropertyEither' but returns 'defaultValue' on parse error
208213
useProperty ::
@@ -211,21 +216,22 @@ useProperty ::
211216
Properties r ->
212217
Maybe A.Object ->
213218
ToHsType t
214-
useProperty k p =
219+
useProperty kn p =
215220
maybe
216221
(defaultValue metadata)
217-
(fromRight (defaultValue metadata) . usePropertyEither k p)
222+
(fromRight (defaultValue metadata) . usePropertyEither kn p)
218223
where
219-
(_, metadata) = find k p
224+
(_, metadata) = find kn p
220225

221226
parseProperty ::
222227
(k ~ 'PropertyKey s t, KnownSymbol s) =>
223228
KeyNameProxy s ->
224229
(SPropertyKey k, MetaData t) ->
225230
A.Object ->
226231
Either String (ToHsType t)
227-
parseProperty k km x = case km of
232+
parseProperty kn k x = case k of
228233
(SNumber, _) -> parseEither
234+
(SInteger, _) -> parseEither
229235
(SString, _) -> parseEither
230236
(SBoolean, _) -> parseEither
231237
(SObject, _) -> parseEither
@@ -245,7 +251,7 @@ parseProperty k km x = case km of
245251
)
246252
x
247253
where
248-
keyName = T.pack $ symbolVal k
254+
keyName = T.pack $ symbolVal kn
249255
parseEither :: forall a. A.FromJSON a => Either String a
250256
parseEither = A.parseEither (A..: keyName) x
251257

@@ -258,12 +264,25 @@ defineNumberProperty ::
258264
-- | description
259265
T.Text ->
260266
-- | default value
261-
Int ->
267+
Double ->
262268
Properties r ->
263269
Properties ('PropertyKey s 'TNumber : r)
264270
defineNumberProperty kn description defaultValue =
265271
insert kn SNumber MetaData {..}
266272

273+
-- | Defines an integer property
274+
defineIntegerProperty ::
275+
(KnownSymbol s, NotElem s r) =>
276+
KeyNameProxy s ->
277+
-- | description
278+
T.Text ->
279+
-- | default value
280+
Int ->
281+
Properties r ->
282+
Properties ('PropertyKey s 'TInteger : r)
283+
defineIntegerProperty kn description defaultValue =
284+
insert kn SInteger MetaData {..}
285+
267286
-- | Defines a string property
268287
defineStringProperty ::
269288
(KnownSymbol s, NotElem s r) =>
@@ -341,6 +360,8 @@ toDefaultJSON (Properties p) = [toEntry s v | (s, v) <- Map.toList p]
341360
toEntry (T.pack -> s) = \case
342361
(SomePropertyKeyWithMetaData SNumber MetaData {..}) ->
343362
s A..= defaultValue
363+
(SomePropertyKeyWithMetaData SInteger MetaData {..}) ->
364+
s A..= defaultValue
344365
(SomePropertyKeyWithMetaData SString MetaData {..}) ->
345366
s A..= defaultValue
346367
(SomePropertyKeyWithMetaData SBoolean MetaData {..}) ->
@@ -366,6 +387,13 @@ toVSCodeExtensionSchema prefix (Properties p) =
366387
"default" A..= defaultValue,
367388
"scope" A..= A.String "resource"
368389
]
390+
(SomePropertyKeyWithMetaData SInteger MetaData {..}) ->
391+
A.object
392+
[ "type" A..= A.String "integer",
393+
"markdownDescription" A..= description,
394+
"default" A..= defaultValue,
395+
"scope" A..= A.String "resource"
396+
]
369397
(SomePropertyKeyWithMetaData SString MetaData {..}) ->
370398
A.object
371399
[ "type" A..= A.String "string",

plugins/hls-tactics-plugin/src/Wingman/LanguageServer.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,12 @@ runStaleIde state nfp a = MaybeT $ runIde state $ useWithStale a nfp
7878
------------------------------------------------------------------------------
7979

8080
properties :: Properties
81-
'[ 'PropertyKey
82-
"max_use_ctor_actions" 'TNumber,
81+
'[ 'PropertyKey "max_use_ctor_actions" 'TInteger,
8382
'PropertyKey "features" 'TString]
8483
properties = emptyProperties
8584
& defineStringProperty #features
8685
"Feature set used by Wingman" ""
87-
& defineNumberProperty #max_use_ctor_actions
86+
& defineIntegerProperty #max_use_ctor_actions
8887
"Maximum number of `Use constructor <x>` code actions that can appear" 5
8988

9089

0 commit comments

Comments
 (0)