Skip to content

Commit aef8359

Browse files
committed
cleanup: remove unused code
1 parent 437f7d3 commit aef8359

5 files changed

Lines changed: 18 additions & 61 deletions

File tree

utils/generate_notifications.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import TYPE_CHECKING
44
from utils.helpers import format_type
55
from utils.helpers import indentation
6-
from utils.helpers import StructureKind
76

87
if TYPE_CHECKING:
98
from lsp_schema import Notification
@@ -40,7 +39,7 @@ def generate_notification(notification: Notification) -> tuple[str, str]:
4039
definition = f'class {name}(TypedDict):\n'
4140
definition += f"{indentation}method: Literal['{method}']\n"
4241
if params:
43-
definition += f'{indentation}params: {format_type(params, {"root_symbol_name": ""}, StructureKind.Class)}'
42+
definition += f'{indentation}params: {format_type(params, {"root_symbol_name": ""})}'
4443
else:
4544
definition += f'{indentation}params: None'
4645
return (name, definition)

utils/generate_requests_and_responses.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import TYPE_CHECKING
44
from utils.helpers import format_type
55
from utils.helpers import indentation
6-
from utils.helpers import StructureKind
76

87
if TYPE_CHECKING:
98
from lsp_schema import Request
@@ -59,7 +58,7 @@ def generate_request(request: Request) -> tuple[str, str]:
5958
definition = f'class {name}(TypedDict):\n'
6059
definition += f"{indentation}method: Literal['{method}']\n"
6160
if params:
62-
definition += f'{indentation}params: {format_type(params, {"root_symbol_name": ""}, StructureKind.Class)}'
61+
definition += f'{indentation}params: {format_type(params, {"root_symbol_name": ""})}'
6362
else:
6463
definition += f'{indentation}params: None'
6564
return (name, definition)
@@ -74,7 +73,7 @@ def generate_response(request: Request) -> tuple[str, str]:
7473
definition = f'class {name}(TypedDict):\n'
7574
definition += f"{indentation}method: Literal['{method}']\n"
7675
if request['messageDirection'] == 'serverToClient':
77-
typ = format_type(params, {'root_symbol_name': ''}, StructureKind.Class) if params else None
76+
typ = format_type(params, {'root_symbol_name': ''}) if params else None
7877
definition += f'{indentation}params: {typ}\n'
79-
definition += f'{indentation}result: {format_type(result, {"root_symbol_name": ""}, StructureKind.Class)}'
78+
definition += f'{indentation}result: {format_type(result, {"root_symbol_name": ""})}'
8079
return (name, definition)

utils/generate_structures.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ def to_string(structure: Structure) -> str:
2222
return [to_string(structure) for structure in structures if not structure['name'].startswith('_')]
2323

2424

25-
def get_additional_properties(
26-
for_structure: Structure, structures: list[Structure], structure_kind: StructureKind
27-
) -> list[FormattedProperty]:
25+
def get_additional_properties(for_structure: Structure, structures: list[Structure]) -> list[FormattedProperty]:
2826
"""Return properties from extended and mixin types."""
2927
result: list[FormattedProperty] = []
3028
additional_structures = for_structure.get('extends') or []
@@ -35,16 +33,16 @@ def get_additional_properties(
3533
raise Exception(error, additional_structure['kind'])
3634
structure = next(structure for structure in structures if structure['name'] == additional_structure['name'])
3735
if structure:
38-
properties = get_formatted_properties(structure['properties'], structure['name'], structure_kind)
36+
properties = get_formatted_properties(structure['properties'], structure['name'])
3937
result.extend(properties)
4038
return result
4139

4240

4341
def generate_structure(structure: Structure, structures: list[Structure], structure_kind: StructureKind) -> str:
4442
result = ''
4543
symbol_name = structure['name']
46-
properties = get_formatted_properties(structure['properties'], structure['name'], structure_kind)
47-
additional_properties = get_additional_properties(structure, structures, structure_kind)
44+
properties = get_formatted_properties(structure['properties'], structure['name'])
45+
additional_properties = get_additional_properties(structure, structures)
4846

4947
# add extended properties
5048
taken_property_names = [p['name'] for p in properties]

utils/generate_type_aliases.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import TYPE_CHECKING
44
from utils.helpers import format_comment
55
from utils.helpers import format_type
6-
from utils.helpers import StructureKind
76

87
if TYPE_CHECKING:
98
from lsp_schema import TypeAlias
@@ -16,7 +15,7 @@ def to_string(type_alias: TypeAlias) -> str:
1615
if symbol_name in overrides:
1716
value = overrides[symbol_name]
1817
else:
19-
value = format_type(type_alias['type'], {'root_symbol_name': symbol_name}, StructureKind.Class)
18+
value = format_type(type_alias['type'], {'root_symbol_name': symbol_name})
2019
result = f"""
2120
{symbol_name}: TypeAlias = {value}"""
2221
if documentation:

utils/helpers.py

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

33
from enum import Enum
44
from typing import Any
5-
from typing import ClassVar
65
from typing import TYPE_CHECKING
76
from typing import TypedDict
87
import keyword
@@ -41,30 +40,12 @@ def format_comment(text: str | None, indent: str = '') -> str:
4140
new_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-
6143
def get_new_literal_structures() -> list[str]:
6244
return sorted(new_literal_structures)
6345

6446

6547
def reset_new_literal_structures() -> None:
6648
new_literal_structures.clear()
67-
SymbolNameTracker.clear()
6849

6950

7051
class 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

Comments
 (0)