Skip to content

Commit 96f4b13

Browse files
rchlpredragnikolic
andauthored
Separate custom types into its own file and format (#33)
* Separate custom types into its own file and format * sort * Update README.md Co-authored-by: Предраг Николић <idmpepe@gmail.com> * Update README.md Co-authored-by: Предраг Николић <idmpepe@gmail.com> * only format * add ignores --------- Co-authored-by: Предраг Николић <idmpepe@gmail.com>
1 parent 2d96611 commit 96f4b13

7 files changed

Lines changed: 1142 additions & 853 deletions

File tree

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@ Requires py3.8 and up.
44

55
## Setup
66

7+
Use UV to manage virtual environment and run scripts.
8+
79
Install dependecies:
810
```sh
9-
pip install -r requirements.txt
11+
uv venv
1012
```
1113

1214
## Update types
1315

1416
Download the latest json schema:
1517
```sh
16-
python ./download_schemas.py
18+
uv run download_schemas.py
1719
```
1820

19-
Generate the types:
21+
Generate the types and fix some issues by triggering formatting:
2022
```sh
21-
python ./generate.py
23+
uv run generate.py
24+
uv run ruff format
2225
```
26+
2327
Copy the `lsp_types.py` file to your project.
2428

2529
NOTE: Do not import types that begin with `__`. These types are internal types and are not meant to be used.

generate.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from operator import itemgetter
56
from pathlib import Path
67
from typing import cast
78
from typing import Literal
@@ -42,7 +43,7 @@
4243
ALIAS_OVERRIDES: dict[str, str] = {'LSPArray': "Sequence['LSPAny']", 'LSPObject': 'Mapping[str, Any]'}
4344

4445

45-
def generate(output: str) -> None:
46+
def generate_protocol(output: str) -> None:
4647
reset_new_literal_structures()
4748

4849
schema = Path('./lsprotocol/lsp.json').read_text(encoding='utf-8')
@@ -54,10 +55,20 @@ def generate(output: str) -> None:
5455
'# ruff: noqa: E501, UP006, UP007',
5556
'# Code generated. DO NOT EDIT.',
5657
f'# LSP v{specification_version}\n',
57-
'from __future__ import annotations',
58-
'from enum import IntEnum, IntFlag, StrEnum',
59-
'from typing import Any, Dict, List, Literal, Mapping, Sequence, TypedDict, Union',
60-
'from typing_extensions import NotRequired, TypeAlias\n\n',
58+
'from __future__ import annotations\n',
59+
'from enum import IntEnum',
60+
'from enum import IntFlag',
61+
'from enum import StrEnum',
62+
'from typing import Any',
63+
'from typing import Dict',
64+
'from typing import List',
65+
'from typing import Literal',
66+
'from typing import Mapping',
67+
'from typing import Sequence',
68+
'from typing import TypedDict',
69+
'from typing import Union',
70+
'from typing_extensions import NotRequired',
71+
'from typing_extensions import TypeAlias\n',
6172
'URI = str',
6273
'DocumentUri = str',
6374
'Uint = int',
@@ -71,12 +82,45 @@ def generate(output: str) -> None:
7182
content += '\n'.join(generate_type_aliases(lsp_json['typeAliases'], ALIAS_OVERRIDES))
7283
content += '\n\n\n'
7384
content += '\n\n\n'.join(generate_structures(lsp_json['structures']))
85+
content += '\n'
86+
content += '\n'.join(get_new_literal_structures())
87+
88+
# Remove trailing spaces.
89+
lines = content.split('\n')
90+
lines = [line.rstrip() for line in lines]
91+
content = '\n'.join(lines)
92+
93+
Path(output).write_text(content, encoding='utf-8')
94+
95+
96+
def generate_custom(output: str) -> None:
97+
reset_new_literal_structures()
98+
99+
schema = Path('./lsprotocol/lsp.json').read_text(encoding='utf-8')
100+
lsp_json = cast('MetaModel', json.loads(schema))
101+
102+
content = '\n'.join( # noqa: FLY002
103+
[
104+
'# ruff: noqa: UP006, UP007',
105+
'from __future__ import annotations\n',
106+
'from .lsp_types import *',
107+
'from typing import List',
108+
'from typing import Literal',
109+
'from typing import TypedDict',
110+
'from typing import Union',
111+
'from typing_extensions import TypeAlias',
112+
]
113+
)
114+
115+
# Sort by method name to avoid unstable order.
116+
requests = sorted(lsp_json['requests'], key=itemgetter('typeName'))
117+
notifications = sorted(lsp_json['notifications'], key=itemgetter('typeName'))
118+
74119
content += '\n\n\n'
75-
content += '\n\n\n'.join(generate_requests_and_responses(lsp_json['requests']))
120+
content += '\n\n\n'.join(generate_requests_and_responses(requests))
76121
content += '\n\n\n'
77-
content += '\n\n\n'.join(generate_notifications(lsp_json['notifications']))
122+
content += '\n\n\n'.join(generate_notifications(notifications))
78123
content += '\n'
79-
content += '\n'.join(get_new_literal_structures())
80124

81125
# Remove trailing spaces.
82126
lines = content.split('\n')
@@ -86,4 +130,5 @@ def generate(output: str) -> None:
86130
Path(output).write_text(content, encoding='utf-8')
87131

88132

89-
generate(output='./generated/lsp_types.py')
133+
generate_protocol(output='./generated/lsp_types.py')
134+
generate_custom(output='./generated/custom.py')

0 commit comments

Comments
 (0)