-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate.py
executable file
·71 lines (59 loc) · 2.59 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
import json
from lsp_schema import MetaModel
from typing import Dict, Literal
from utils.generate_enumerations import generate_enumerations
from utils.generate_structures import generate_structures
from utils.generate_type_aliases import generate_type_aliases
from utils.helpers import get_new_literal_structures, reset_new_literal_structures, StructureKind
ENUM_OVERRIDES = {
'CodeActionKind': 'StrEnum',
'DocumentDiagnosticReportKind': 'StrEnum',
'FailureHandlingKind': 'StrEnum',
'FileOperationPatternKind': 'StrEnum',
'FoldingRangeKind': 'StrEnum',
'LanguageKind': 'StrEnum',
'MarkupKind': 'StrEnum',
'MonikerKind': 'StrEnum',
'PositionEncodingKind': 'StrEnum',
'ResourceOperationKind': 'StrEnum',
'SemanticTokenModifiers': 'StrEnum',
'SemanticTokenTypes': 'StrEnum',
'TokenFormat': 'StrEnum',
'TraceValue': 'StrEnum',
'UniquenessLevel': 'StrEnum',
'WatchKind': 'IntFlag',
'ApplyKind': 'StrEnum'
} # type: Dict[str, Literal['StrEnum', 'IntFlag']]
def generate(preferred_structure_kind: StructureKind, output: str) -> None:
reset_new_literal_structures()
with open('./lsprotocol/lsp.json') as file:
lsp_json: MetaModel = json.load(file)
specification_version = lsp_json.get('metaData')['version']
content = "\n".join([
"# Code generated. DO NOT EDIT.",
f"# LSP v{specification_version}\n",
"from typing_extensions import NotRequired",
"from typing import Dict, List, Literal, TypedDict, Union",
"from enum import IntEnum, IntFlag, StrEnum\n\n",
"URI = str",
"DocumentUri = str",
"Uint = int",
"RegExp = str",
])
content += '\n\n\n'
content += '\n\n\n'.join(generate_enumerations(lsp_json['enumerations'], ENUM_OVERRIDES))
content += '\n\n'
content += '\n'.join(generate_type_aliases(lsp_json['typeAliases'], preferred_structure_kind))
content += '\n\n\n'
content += '\n\n\n'.join(generate_structures(lsp_json['structures'], preferred_structure_kind))
content += '\n\n'
content += '\n'.join(get_new_literal_structures())
# Remove trailing spaces.
lines = content.split('\n')
lines = [line.rstrip() for line in lines]
content = '\n'.join(lines)
with open(output, "w") as new_file:
new_file.write(content)
generate(preferred_structure_kind=StructureKind.Class, output="./lsp_types.py")
generate(preferred_structure_kind=StructureKind.Function, output="./lsp_types_sublime_text_33.py")