22
33from enum import Enum
44from typing import Any
5- from typing import ClassVar
65from typing import TYPE_CHECKING
76from typing import TypedDict
87import keyword
@@ -41,30 +40,12 @@ def format_comment(text: str | None, indent: str = '') -> str:
4140new_literal_structures : set [str ] = set ()
4241
4342
44- class SymbolNameTracker :
45- symbols : ClassVar [dict [str , int ]] = {
46- # key: symbol name
47- # value: symbol count
48- }
49-
50- @classmethod
51- def get_symbol_id (cls , symbol_name : str ) -> int :
52- count = SymbolNameTracker .symbols .get (symbol_name ) or 1
53- SymbolNameTracker .symbols [symbol_name ] = count + 1
54- return count
55-
56- @classmethod
57- def clear (cls ) -> None :
58- SymbolNameTracker .symbols .clear ()
59-
60-
6143def get_new_literal_structures () -> list [str ]:
6244 return sorted (new_literal_structures )
6345
6446
6547def reset_new_literal_structures () -> None :
6648 new_literal_structures .clear ()
67- SymbolNameTracker .clear ()
6849
6950
7051class StructureKind (Enum ):
@@ -76,50 +57,33 @@ class FormatTypeContext(TypedDict):
7657 root_symbol_name : str
7758
7859
79- def format_type (typ : EveryType , context : FormatTypeContext , preferred_structure_kind : StructureKind ) -> str :
60+ def format_type (typ : EveryType , context : FormatTypeContext ) -> str :
8061 result = 'Any'
8162 if typ ['kind' ] == 'base' :
8263 return format_base_types (typ )
8364 if typ ['kind' ] == 'reference' :
8465 literal_symbol_name = typ ['name' ]
8566 return f"'{ literal_symbol_name } '"
8667 if typ ['kind' ] == 'array' :
87- literal_symbol_name = format_type (typ ['element' ], context , preferred_structure_kind )
68+ literal_symbol_name = format_type (typ ['element' ], context )
8869 return f'List[{ literal_symbol_name } ]'
8970 if typ ['kind' ] == 'map' :
9071 key = format_base_types (typ ['key' ])
91- value = format_type (typ ['value' ], {'root_symbol_name' : key }, preferred_structure_kind )
72+ value = format_type (typ ['value' ], {'root_symbol_name' : key })
9273 return f'Dict[{ key } , { value } ]'
9374 if typ ['kind' ] == 'and' :
9475 pass
9576 elif typ ['kind' ] == 'or' :
96- union = [format_type (item , context , preferred_structure_kind ) for item in typ ['items' ]]
77+ union = [format_type (item , context ) for item in typ ['items' ]]
9778 return f'Union[{ ", " .join (union )} ]'
9879 elif typ ['kind' ] == 'tuple' :
99- union = [format_type (item , context , preferred_structure_kind ) for item in typ ['items' ]]
80+ union = [format_type (item , context ) for item in typ ['items' ]]
10081 return f'list[{ " | " .join (set (union ))} ]'
10182 elif typ ['kind' ] == 'literal' :
10283 if not typ ['value' ]['properties' ]:
10384 return 'Dict[str, LSPAny]'
104- root_symbol_name = capitalize (context ['root_symbol_name' ])
105- literal_symbol_name = f'__{ root_symbol_name } _Type'
106- symbol_id = SymbolNameTracker .get_symbol_id (literal_symbol_name )
107- literal_symbol_name += f'_{ symbol_id } '
108- properties = get_formatted_properties (typ ['value' ]['properties' ], root_symbol_name , preferred_structure_kind )
109- if preferred_structure_kind == StructureKind .Function :
110- formatted_properties = format_dict_properties (properties )
111- new_literal_structures .add (f"""
112- { literal_symbol_name } = TypedDict('{ literal_symbol_name } ', {{
113- { indentation } { formatted_properties }
114- }})
115- """ )
116- else :
117- formatted_properties = format_class_properties (properties )
118- new_literal_structures .add (f"""
119- class { literal_symbol_name } (TypedDict):
120- { indentation } { formatted_properties or 'pass' }
121- """ )
122- return f"'{ literal_symbol_name } '"
85+ msg = 'Unsupported case, none of the cases in LSP schema need this currently!'
86+ raise Exception (msg )
12387 elif typ ['kind' ] == 'stringLiteral' :
12488 return f"Literal['{ typ ['value' ]} ']"
12589 elif typ ['kind' ] == 'integerLiteral' or typ ['kind' ] == 'booleanLiteral' :
@@ -147,13 +111,11 @@ class FormattedProperty(TypedDict):
147111 documentation : str
148112
149113
150- def get_formatted_properties (
151- properties : list [Property ], root_symbol_name : str , preferred_structure_kind : StructureKind
152- ) -> list [FormattedProperty ]:
114+ def get_formatted_properties (properties : list [Property ], root_symbol_name : str ) -> list [FormattedProperty ]:
153115 result : list [FormattedProperty ] = []
154116 for p in properties :
155117 key = p ['name' ]
156- value = format_type (p ['type' ], {'root_symbol_name' : root_symbol_name + '_' + key }, preferred_structure_kind )
118+ value = format_type (p ['type' ], {'root_symbol_name' : root_symbol_name + '_' + key })
157119 if p .get ('optional' ):
158120 value = f'NotRequired[{ value } ]'
159121 documentation = p .get ('documentation' ) or ''
0 commit comments