|
1 | 1 | #!/usr/bin/env python3 |
2 | | -import json |
3 | 2 |
|
4 | | -from lsp_schema import MetaModel |
5 | | -from typing import Dict, Literal |
| 3 | +from __future__ import annotations |
| 4 | +from pathlib import Path |
| 5 | +from typing import Literal, cast, TYPE_CHECKING |
6 | 6 | from utils.generate_enumerations import generate_enumerations |
7 | 7 | from utils.generate_structures import generate_structures |
8 | 8 | from utils.generate_type_aliases import generate_type_aliases |
9 | | -from utils.helpers import get_new_literal_structures, reset_new_literal_structures, StructureKind |
| 9 | +from utils.helpers import get_new_literal_structures, reset_new_literal_structures |
| 10 | +import json |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from lsp_schema import MetaModel |
10 | 14 |
|
11 | 15 |
|
12 | | -ENUM_OVERRIDES = { |
| 16 | +ENUM_OVERRIDES: dict[str, Literal['StrEnum', 'IntFlag']] = { |
13 | 17 | 'CodeActionKind': 'StrEnum', |
14 | 18 | 'DocumentDiagnosticReportKind': 'StrEnum', |
15 | 19 | 'FailureHandlingKind': 'StrEnum', |
|
26 | 30 | 'TraceValue': 'StrEnum', |
27 | 31 | 'UniquenessLevel': 'StrEnum', |
28 | 32 | 'WatchKind': 'IntFlag', |
29 | | - 'ApplyKind': 'IntFlag' |
30 | | -} # type: Dict[str, Literal['StrEnum', 'IntFlag']] |
| 33 | + 'ApplyKind': 'IntFlag', |
| 34 | +} |
31 | 35 |
|
32 | 36 |
|
33 | | -def generate(preferred_structure_kind: StructureKind, output: str) -> None: |
| 37 | +def generate(output: str) -> None: |
34 | 38 | reset_new_literal_structures() |
35 | 39 |
|
36 | | - with open('./lsprotocol/lsp.json') as file: |
37 | | - lsp_json: MetaModel = json.load(file) |
38 | | - specification_version = lsp_json.get('metaData')['version'] |
| 40 | + schema = Path('./lsprotocol/lsp.json').read_text(encoding='utf-8') |
| 41 | + lsp_json = cast('MetaModel', json.loads(schema)) |
| 42 | + specification_version = lsp_json.get('metaData')['version'] |
39 | 43 |
|
40 | | - content = "\n".join([ |
41 | | - "# Code generated. DO NOT EDIT.", |
42 | | - f"# LSP v{specification_version}\n", |
43 | | - "from typing_extensions import NotRequired", |
44 | | - "from typing import Dict, List, Literal, TypedDict, Union", |
45 | | - "from enum import IntEnum, IntFlag, StrEnum\n\n", |
46 | | - "URI = str", |
47 | | - "DocumentUri = str", |
48 | | - "Uint = int", |
49 | | - "RegExp = str", |
50 | | - ]) |
| 44 | + content = '\n'.join( |
| 45 | + [ |
| 46 | + '# ruff: noqa: E501, UP006, UP007', |
| 47 | + '# Code generated. DO NOT EDIT.', |
| 48 | + f'# LSP v{specification_version}\n', |
| 49 | + 'from __future__ import annotations', |
| 50 | + 'from enum import IntEnum, IntFlag, StrEnum', |
| 51 | + 'from typing import Dict, List, Literal, TypedDict, Union', |
| 52 | + 'from typing_extensions import NotRequired\n\n', |
| 53 | + 'URI = str', |
| 54 | + 'DocumentUri = str', |
| 55 | + 'Uint = int', |
| 56 | + 'RegExp = str', |
| 57 | + ] |
| 58 | + ) |
51 | 59 |
|
52 | | - content += '\n\n\n' |
53 | | - content += '\n\n\n'.join(generate_enumerations(lsp_json['enumerations'], ENUM_OVERRIDES)) |
54 | | - content += '\n\n' |
55 | | - content += '\n'.join(generate_type_aliases(lsp_json['typeAliases'], preferred_structure_kind)) |
56 | | - content += '\n\n\n' |
57 | | - content += '\n\n\n'.join(generate_structures(lsp_json['structures'], preferred_structure_kind)) |
58 | | - content += '\n\n' |
59 | | - content += '\n'.join(get_new_literal_structures()) |
| 60 | + content += '\n\n\n' |
| 61 | + content += '\n\n\n'.join(generate_enumerations(lsp_json['enumerations'], ENUM_OVERRIDES)) |
| 62 | + content += '\n\n' |
| 63 | + content += '\n'.join(generate_type_aliases(lsp_json['typeAliases'])) |
| 64 | + content += '\n\n\n' |
| 65 | + content += '\n\n\n'.join(generate_structures(lsp_json['structures'])) |
| 66 | + content += '\n\n' |
| 67 | + content += '\n'.join(get_new_literal_structures()) |
60 | 68 |
|
61 | | - # Remove trailing spaces. |
62 | | - lines = content.split('\n') |
63 | | - lines = [line.rstrip() for line in lines] |
64 | | - content = '\n'.join(lines) |
| 69 | + # Remove trailing spaces. |
| 70 | + lines = content.split('\n') |
| 71 | + lines = [line.rstrip() for line in lines] |
| 72 | + content = '\n'.join(lines) |
65 | 73 |
|
66 | | - with open(output, "w") as new_file: |
67 | | - new_file.write(content) |
| 74 | + Path(output).write_text(content) |
68 | 75 |
|
69 | 76 |
|
70 | | -generate(preferred_structure_kind=StructureKind.Class, output="./lsp_types.py") |
71 | | -generate(preferred_structure_kind=StructureKind.Function, output="./lsp_types_sublime_text_33.py") |
| 77 | +generate(output='./generated/lsp_types.py') |
0 commit comments