Skip to content

Commit 47c83d8

Browse files
authored
Update LSP to latest spec (#325)
1 parent 4a296ec commit 47c83d8

File tree

10 files changed

+1573
-846
lines changed

10 files changed

+1573
-846
lines changed

generator/lsp.json

Lines changed: 609 additions & 287 deletions
Large diffs are not rendered by default.

generator/plugins/dotnet/dotnet_classes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def get_converter(type_def: model.LSP_TYPE_SPEC, type_name: str) -> Optional[str
176176
elif type_def.kind == "reference" and type_def.name in [
177177
"Pattern",
178178
"ChangeAnnotationIdentifier",
179+
"RegularExpressionEngineKind",
179180
]:
180181
return f"[JsonConverter(typeof(CustomStringConverter<{type_def.name}>))]"
181182
elif type_def.kind == "reference" and type_def.name == "DocumentSelector":

generator/plugins/dotnet/dotnet_special_classes.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"LSPArray",
1616
"ChangeAnnotationIdentifier",
1717
"Pattern",
18+
"RegularExpressionEngineKind",
1819
"DocumentSelector",
1920
"InitializedParams",
2021
]
@@ -88,33 +89,38 @@ def generate_special_class(
8889
),
8990
)
9091

91-
if type_def.name == "Pattern":
92-
inner = [
93-
"private string pattern;",
94-
"public Pattern(string value){pattern = value;}",
95-
"public static implicit operator Pattern(string value) => new Pattern(value);",
96-
"public static implicit operator string(Pattern pattern) => pattern.pattern;",
97-
"public override string ToString() => pattern;",
98-
]
99-
lines = namespace_wrapper(
100-
NAMESPACE,
101-
get_usings(["JsonConverter", "DataContract"]),
102-
class_wrapper(
103-
type_def,
104-
inner,
105-
None,
106-
[f"[JsonConverter(typeof(CustomStringConverter<{type_def.name}>))]"],
107-
),
108-
)
92+
if type_def.name in [
93+
"Pattern",
94+
"RegularExpressionEngineKind",
95+
"ChangeAnnotationIdentifier",
96+
]:
97+
if type_def.name == "Pattern":
98+
inner = [
99+
"private string pattern;",
100+
"public Pattern(string value){pattern = value;}",
101+
"public static implicit operator Pattern(string value) => new Pattern(value);",
102+
"public static implicit operator string(Pattern pattern) => pattern.pattern;",
103+
"public override string ToString() => pattern;",
104+
]
105+
106+
if type_def.name == "ChangeAnnotationIdentifier":
107+
inner = [
108+
"private string identifier;",
109+
"public ChangeAnnotationIdentifier(string value){identifier = value;}",
110+
"public static implicit operator ChangeAnnotationIdentifier(string value) => new ChangeAnnotationIdentifier(value);",
111+
"public static implicit operator string(ChangeAnnotationIdentifier identifier) => identifier.identifier;",
112+
"public override string ToString() => identifier;",
113+
]
114+
115+
if type_def.name == "RegularExpressionEngineKind":
116+
inner = [
117+
"private string engineKind;",
118+
"public RegularExpressionEngineKind(string value){engineKind = value;}",
119+
"public static implicit operator RegularExpressionEngineKind(string value) => new RegularExpressionEngineKind(value);",
120+
"public static implicit operator string(RegularExpressionEngineKind engineKind) => engineKind.engineKind;",
121+
"public override string ToString() => engineKind;",
122+
]
109123

110-
if type_def.name == "ChangeAnnotationIdentifier":
111-
inner = [
112-
"private string identifier;",
113-
"public ChangeAnnotationIdentifier(string value){identifier = value;}",
114-
"public static implicit operator ChangeAnnotationIdentifier(string value) => new ChangeAnnotationIdentifier(value);",
115-
"public static implicit operator string(ChangeAnnotationIdentifier identifier) => identifier.identifier;",
116-
"public override string ToString() => identifier;",
117-
]
118124
lines = namespace_wrapper(
119125
NAMESPACE,
120126
get_usings(["JsonConverter", "DataContract"]),

generator/plugins/python/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
PACKAGE_NAME = "lsprotocol"
1717

1818
# These are special type aliases to preserve backward compatibility.
19-
custom_request_params_aliases = []
19+
CUSTOM_REQUEST_PARAMS_ALIASES = []
20+
21+
# Special enums with duplicates
22+
SPECIAL_ENUMS = ["LanguageKind", "ErrorCodes", "LSPErrorCodes"]
2023

2124

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

389392
def _add_enum(self, enum_def: model.Enum) -> None:
390393
code_lines = [
391-
"" if "ErrorCodes" in enum_def.name else "@enum.unique",
394+
"" if enum_def.name in SPECIAL_ENUMS else "@enum.unique",
392395
]
393396
if enum_def.type.name == "string":
394397
code_lines += [f"class {enum_def.name}(str, enum.Enum):"]
@@ -810,7 +813,7 @@ def _add_requests(self, lsp_mode: model.LSPModel) -> None:
810813
if request.params:
811814
if (
812815
request.params.kind == "reference"
813-
and f"{class_name}Params" in custom_request_params_aliases
816+
and f"{class_name}Params" in CUSTOM_REQUEST_PARAMS_ALIASES
814817
):
815818
params_type = f"{class_name}Params"
816819

noxfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def lint(session: nox.Session):
3636

3737
session.log("Linting: generator and generated Python code.")
3838
session.install("mypy", "ruff")
39+
session.run("ruff", "--version")
40+
session.run("mypy", "--version")
41+
3942
session.run("ruff", "check", ".")
4043
session.run("ruff", "check", "--select=I001", ".")
4144
session.run("ruff", "format", "--check", ".")
@@ -55,6 +58,7 @@ def format(session: nox.Session):
5558

5659
def _format_code(session: nox.Session):
5760
session.install("ruff")
61+
session.run("ruff", "--version")
5862

5963
session.run("ruff", "check", "--fix", "--select=I001", ".")
6064
session.run("ruff", "format", ".")

packages/python/lsprotocol/_hooks.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _filter(p: Tuple[str, object]) -> bool:
2727
items = list(filter(_filter, lsp_types.ALL_TYPES_MAP.items()))
2828
for _, value in items:
2929
if isinstance(value, type):
30-
attrs.resolve_types(value, lsp_types.ALL_TYPES_MAP, {}) # type: ignore
30+
attrs.resolve_types(value, lsp_types.ALL_TYPES_MAP, {})
3131
_resolved_forward_references = True
3232

3333

@@ -722,6 +722,18 @@ def _notebook_sync_registration_option_selector_hook(
722722
lsp_types.NotebookDocumentFilterWithCells,
723723
)
724724

725+
def _language_kind_hook(
726+
object_: Any, _: type
727+
) -> Union[
728+
lsp_types.LanguageKind,
729+
OptionalPrimitive,
730+
]:
731+
if object_ is None:
732+
return None
733+
if isinstance(object_, (bool, int, str, float)):
734+
return object_
735+
return converter.structure(object_, lsp_types.LanguageKind)
736+
725737
structure_hooks = [
726738
(
727739
Optional[
@@ -1063,6 +1075,10 @@ def _notebook_sync_registration_option_selector_hook(
10631075
],
10641076
_symbol_list_hook,
10651077
),
1078+
(
1079+
Union[lsp_types.LanguageKind, str],
1080+
_language_kind_hook,
1081+
),
10661082
]
10671083
for type_, hook in structure_hooks:
10681084
converter.register_structure_hook(type_, hook)
@@ -1229,7 +1245,7 @@ def _with_custom_unstructure(cls: type) -> Any:
12291245
)
12301246
for a in attrs.fields(cls)
12311247
}
1232-
return cattrs.gen.make_dict_unstructure_fn(cls, converter, **attributes)
1248+
return cattrs.gen.make_dict_unstructure_fn(cls, converter, **attributes) # type: ignore
12331249

12341250
def _with_custom_structure(cls: type) -> Any:
12351251
attributes = {
@@ -1239,7 +1255,7 @@ def _with_custom_structure(cls: type) -> Any:
12391255
)
12401256
for a in attrs.fields(cls)
12411257
}
1242-
return cattrs.gen.make_dict_structure_fn(cls, converter, **attributes)
1258+
return cattrs.gen.make_dict_structure_fn(cls, converter, **attributes) # type: ignore
12431259

12441260
converter.register_unstructure_hook_factory(attrs.has, _with_custom_unstructure)
12451261
converter.register_structure_hook_factory(attrs.has, _with_custom_structure)

0 commit comments

Comments
 (0)