From b3639bc811633ec1ef7965945f3ff92433ebd8f2 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Tue, 11 Jun 2024 10:17:23 -0700 Subject: [PATCH 1/2] Add customization to CompletionItemKind --- generator/plugins/python/utils.py | 12 ++++++++++++ packages/python/lsprotocol/types.py | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/generator/plugins/python/utils.py b/generator/plugins/python/utils.py index 9c2eaa7..e616af9 100644 --- a/generator/plugins/python/utils.py +++ b/generator/plugins/python/utils.py @@ -22,7 +22,19 @@ SPECIAL_ENUMS = ["LanguageKind", "ErrorCodes", "LSPErrorCodes"] +def customizations(spec: model.LSPModel) -> model.LSPModel: + # https://github.com/microsoft/lsprotocol/issues/344 + # Allow CompletionItemKind to support custom values + for enum in spec.enumerations: + if enum.name == "CompletionItemKind": + enum.supportsCustomValues = True + break + + return spec + + def generate_from_spec(spec: model.LSPModel, output_dir: str, test_dir: str) -> None: + spec = customizations(spec) code = TypesCodeGenerator(spec).get_code() output_path = pathlib.Path(output_dir, PACKAGE_NAME) diff --git a/packages/python/lsprotocol/types.py b/packages/python/lsprotocol/types.py index 443f61e..b6dbb62 100644 --- a/packages/python/lsprotocol/types.py +++ b/packages/python/lsprotocol/types.py @@ -3239,7 +3239,7 @@ class CompletionItem: @since 3.17.0""" # Since: 3.17.0 - kind: Optional[CompletionItemKind] = attrs.field(default=None) + kind: Optional[Union[CompletionItemKind, int]] = attrs.field(default=None) """The kind of this completion item. Based of the kind an icon is chosen by the editor.""" @@ -9125,7 +9125,9 @@ class ClientCompletionItemOptionsKind: # Since: 3.18.0 - value_set: Optional[Sequence[CompletionItemKind]] = attrs.field(default=None) + value_set: Optional[Sequence[Union[CompletionItemKind, int]]] = attrs.field( + default=None + ) """The completion item kind values the client supports. When this property exists the client also guarantees that it will handle values outside its set gracefully and falls back From 10eb28780228318fe475e3e425b5bc9109da3bec Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Tue, 11 Jun 2024 10:26:51 -0700 Subject: [PATCH 2/2] Fix hooks --- packages/python/lsprotocol/_hooks.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/python/lsprotocol/_hooks.py b/packages/python/lsprotocol/_hooks.py index e505bfe..6bf869b 100644 --- a/packages/python/lsprotocol/_hooks.py +++ b/packages/python/lsprotocol/_hooks.py @@ -745,6 +745,15 @@ def _text_edit_hook( return converter.structure(object_, lsp_types.AnnotatedTextEdit) return converter.structure(object_, lsp_types.TextEdit) + def _completion_item_kind_hook( + object_: Any, _: type + ) -> Union[lsp_types.CompletionItemKind, OptionalPrimitive]: + if object_ is None: + return None + if isinstance(object_, (bool, int, str, float)): + return object_ + return converter.structure(object_, lsp_types.CompletionItemKind) + structure_hooks = [ ( Optional[ @@ -1098,6 +1107,14 @@ def _text_edit_hook( ], _text_edit_hook, ), + ( + Optional[Union[lsp_types.CompletionItemKind, int]], + _completion_item_kind_hook, + ), + ( + Union[lsp_types.CompletionItemKind, int], + _completion_item_kind_hook, + ), ] for type_, hook in structure_hooks: converter.register_structure_hook(type_, hook)