@@ -51,35 +51,61 @@ def rename_if_matching(self, old: str, new: str):
51
51
self .edit (new )
52
52
53
53
54
- def _resolve_conditionals (self ,conditional_parent :ConditionalBlock ,name :str ,original_resolved ):
54
+ @noapidoc
55
+ def _resolve_conditionals (self , conditional_parent : ConditionalBlock , name : str , original_resolved ):
56
+ """Resolves name references within conditional blocks by traversing the conditional chain.
57
+
58
+ This method handles name resolution within conditional blocks (like if/elif/else statements) by:
59
+ 1. Finding the appropriate search boundary based on the conditional block's position
60
+ 2. Handling "fake" conditionals by traversing up the conditional chain
61
+ 3. Yielding resolved names while respecting conditional block boundaries
62
+
63
+ Args:
64
+ conditional_parent (ConditionalBlock): The parent conditional block containing the name reference
65
+ name (str): The name being resolved
66
+ original_resolved: The originally resolved symbol that triggered this resolution
67
+
68
+ Yields:
69
+ Symbol | Import | WildcardImport: Resolved symbols found within the conditional blocks
70
+
71
+ Notes:
72
+ - A "fake" conditional is one where is_true_conditional() returns False
73
+ - The search_limit ensures we don't resolve names that appear after our target
74
+ - The method stops when it either:
75
+ a) Reaches the top of the conditional chain
76
+ b) Returns to the original conditional block
77
+ c) Can't find any more resolutions
78
+ """
55
79
search_limit = conditional_parent .start_byte_for_condition_block
56
- if search_limit >= original_resolved .start_byte :
57
- search_limit = original_resolved .start_byte - 1
80
+ if search_limit >= original_resolved .start_byte :
81
+ search_limit = original_resolved .start_byte - 1
58
82
if not conditional_parent .is_true_conditional (original_resolved ):
59
83
#If it's a fake conditional we must skip any potential enveloping conditionals
60
84
def get_top_of_fake_chain (conditional ,resolved ,search_limit = 0 ):
61
- if skip_fake := conditional .parent_of_type (ConditionalBlock ):
85
+ if skip_fake := conditional .parent_of_type (ConditionalBlock ):
62
86
if skip_fake .is_true_conditional (resolved ):
63
87
return skip_fake .start_byte_for_condition_block
64
88
search_limit = skip_fake .start_byte_for_condition_block
65
89
return get_top_of_fake_chain (skip_fake ,conditional ,search_limit )
66
90
return search_limit
67
- if search_limit := get_top_of_fake_chain (conditional_parent ,original_resolved ):
68
- search_limit = search_limit
91
+ if search_limit := get_top_of_fake_chain (conditional_parent ,original_resolved ):
92
+ search_limit = search_limit
69
93
else :
70
94
return
71
95
72
96
original_conditional = conditional_parent
73
- while next_resolved := next (conditional_parent .resolve_name (name ,start_byte = search_limit ,strict = False ),None ):
97
+ while next_resolved := next (conditional_parent .resolve_name (name ,start_byte = search_limit ,strict = False ),None ):
74
98
yield next_resolved
75
99
next_conditional = next_resolved .parent_of_type (ConditionalBlock )
76
100
if not next_conditional or next_conditional == original_conditional :
77
101
return
78
102
search_limit = next_conditional .start_byte_for_condition_block
79
103
if next_conditional and not next_conditional .is_true_conditional (original_resolved ):
80
104
pass
81
- if search_limit >= next_resolved .start_byte :
82
- search_limit = next_resolved .start_byte - 1
105
+ if search_limit >= next_resolved .start_byte :
106
+ search_limit = next_resolved .start_byte - 1
107
+
108
+
83
109
@noapidoc
84
110
@reader
85
111
def resolve_name (self , name : str , start_byte : int | None = None , strict : bool = True ) -> Generator ["Symbol | Import | WildcardImport" ]:
0 commit comments