Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
896 changes: 609 additions & 287 deletions generator/lsp.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions generator/plugins/dotnet/dotnet_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def get_converter(type_def: model.LSP_TYPE_SPEC, type_name: str) -> Optional[str
elif type_def.kind == "reference" and type_def.name in [
"Pattern",
"ChangeAnnotationIdentifier",
"RegularExpressionEngineKind",
]:
return f"[JsonConverter(typeof(CustomStringConverter<{type_def.name}>))]"
elif type_def.kind == "reference" and type_def.name == "DocumentSelector":
Expand Down
58 changes: 32 additions & 26 deletions generator/plugins/dotnet/dotnet_special_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"LSPArray",
"ChangeAnnotationIdentifier",
"Pattern",
"RegularExpressionEngineKind",
"DocumentSelector",
"InitializedParams",
]
Expand Down Expand Up @@ -88,33 +89,38 @@ def generate_special_class(
),
)

if type_def.name == "Pattern":
inner = [
"private string pattern;",
"public Pattern(string value){pattern = value;}",
"public static implicit operator Pattern(string value) => new Pattern(value);",
"public static implicit operator string(Pattern pattern) => pattern.pattern;",
"public override string ToString() => pattern;",
]
lines = namespace_wrapper(
NAMESPACE,
get_usings(["JsonConverter", "DataContract"]),
class_wrapper(
type_def,
inner,
None,
[f"[JsonConverter(typeof(CustomStringConverter<{type_def.name}>))]"],
),
)
if type_def.name in [
"Pattern",
"RegularExpressionEngineKind",
"ChangeAnnotationIdentifier",
]:
if type_def.name == "Pattern":
inner = [
"private string pattern;",
"public Pattern(string value){pattern = value;}",
"public static implicit operator Pattern(string value) => new Pattern(value);",
"public static implicit operator string(Pattern pattern) => pattern.pattern;",
"public override string ToString() => pattern;",
]

if type_def.name == "ChangeAnnotationIdentifier":
inner = [
"private string identifier;",
"public ChangeAnnotationIdentifier(string value){identifier = value;}",
"public static implicit operator ChangeAnnotationIdentifier(string value) => new ChangeAnnotationIdentifier(value);",
"public static implicit operator string(ChangeAnnotationIdentifier identifier) => identifier.identifier;",
"public override string ToString() => identifier;",
]

if type_def.name == "RegularExpressionEngineKind":
inner = [
"private string engineKind;",
"public RegularExpressionEngineKind(string value){engineKind = value;}",
"public static implicit operator RegularExpressionEngineKind(string value) => new RegularExpressionEngineKind(value);",
"public static implicit operator string(RegularExpressionEngineKind engineKind) => engineKind.engineKind;",
"public override string ToString() => engineKind;",
]

if type_def.name == "ChangeAnnotationIdentifier":
inner = [
"private string identifier;",
"public ChangeAnnotationIdentifier(string value){identifier = value;}",
"public static implicit operator ChangeAnnotationIdentifier(string value) => new ChangeAnnotationIdentifier(value);",
"public static implicit operator string(ChangeAnnotationIdentifier identifier) => identifier.identifier;",
"public override string ToString() => identifier;",
]
lines = namespace_wrapper(
NAMESPACE,
get_usings(["JsonConverter", "DataContract"]),
Expand Down
9 changes: 6 additions & 3 deletions generator/plugins/python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
PACKAGE_NAME = "lsprotocol"

# These are special type aliases to preserve backward compatibility.
custom_request_params_aliases = []
CUSTOM_REQUEST_PARAMS_ALIASES = []

# Special enums with duplicates
SPECIAL_ENUMS = ["LanguageKind", "ErrorCodes", "LSPErrorCodes"]


def generate_from_spec(spec: model.LSPModel, output_dir: str, test_dir: str) -> None:
Expand Down Expand Up @@ -388,7 +391,7 @@ def _add_type_code(self, type_name: str, code: List[str]) -> None:

def _add_enum(self, enum_def: model.Enum) -> None:
code_lines = [
"" if "ErrorCodes" in enum_def.name else "@enum.unique",
"" if enum_def.name in SPECIAL_ENUMS else "@enum.unique",
]
if enum_def.type.name == "string":
code_lines += [f"class {enum_def.name}(str, enum.Enum):"]
Expand Down Expand Up @@ -810,7 +813,7 @@ def _add_requests(self, lsp_mode: model.LSPModel) -> None:
if request.params:
if (
request.params.kind == "reference"
and f"{class_name}Params" in custom_request_params_aliases
and f"{class_name}Params" in CUSTOM_REQUEST_PARAMS_ALIASES
):
params_type = f"{class_name}Params"

Expand Down
4 changes: 4 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def lint(session: nox.Session):

session.log("Linting: generator and generated Python code.")
session.install("mypy", "ruff")
session.run("ruff", "--version")
session.run("mypy", "--version")

session.run("ruff", "check", ".")
session.run("ruff", "check", "--select=I001", ".")
session.run("ruff", "format", "--check", ".")
Expand All @@ -55,6 +58,7 @@ def format(session: nox.Session):

def _format_code(session: nox.Session):
session.install("ruff")
session.run("ruff", "--version")

session.run("ruff", "check", "--fix", "--select=I001", ".")
session.run("ruff", "format", ".")
Expand Down
22 changes: 19 additions & 3 deletions packages/python/lsprotocol/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _filter(p: Tuple[str, object]) -> bool:
items = list(filter(_filter, lsp_types.ALL_TYPES_MAP.items()))
for _, value in items:
if isinstance(value, type):
attrs.resolve_types(value, lsp_types.ALL_TYPES_MAP, {}) # type: ignore
attrs.resolve_types(value, lsp_types.ALL_TYPES_MAP, {})
_resolved_forward_references = True


Expand Down Expand Up @@ -722,6 +722,18 @@ def _notebook_sync_registration_option_selector_hook(
lsp_types.NotebookDocumentFilterWithCells,
)

def _language_kind_hook(
object_: Any, _: type
) -> Union[
lsp_types.LanguageKind,
OptionalPrimitive,
]:
if object_ is None:
return None
if isinstance(object_, (bool, int, str, float)):
return object_
return converter.structure(object_, lsp_types.LanguageKind)

structure_hooks = [
(
Optional[
Expand Down Expand Up @@ -1063,6 +1075,10 @@ def _notebook_sync_registration_option_selector_hook(
],
_symbol_list_hook,
),
(
Union[lsp_types.LanguageKind, str],
_language_kind_hook,
),
]
for type_, hook in structure_hooks:
converter.register_structure_hook(type_, hook)
Expand Down Expand Up @@ -1229,7 +1245,7 @@ def _with_custom_unstructure(cls: type) -> Any:
)
for a in attrs.fields(cls)
}
return cattrs.gen.make_dict_unstructure_fn(cls, converter, **attributes)
return cattrs.gen.make_dict_unstructure_fn(cls, converter, **attributes) # type: ignore

def _with_custom_structure(cls: type) -> Any:
attributes = {
Expand All @@ -1239,7 +1255,7 @@ def _with_custom_structure(cls: type) -> Any:
)
for a in attrs.fields(cls)
}
return cattrs.gen.make_dict_structure_fn(cls, converter, **attributes)
return cattrs.gen.make_dict_structure_fn(cls, converter, **attributes) # type: ignore

converter.register_unstructure_hook_factory(attrs.has, _with_custom_unstructure)
converter.register_structure_hook_factory(attrs.has, _with_custom_structure)
Expand Down
Loading