22
33from __future__ import annotations
44
5+ from operator import itemgetter
56from pathlib import Path
67from typing import cast
78from typing import Literal
4243ALIAS_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