Skip to content

Commit d3dafa6

Browse files
John Van Schultzmeta-codesync[bot]
authored andcommitted
Add function that filters kwargs to only those belonging to function definition.
Summary: In a previous diff a function which added which collects all local kwargs by a given name. This diff creates another helper function which takes those found kwargs and find the function where they are defined. Based on this definition, we are able to select only the keyword arguments that belong to the proper function. This means that if there are multiple kwargs with the same name that belong to different functions, we will be able to narrow the collection of kwargs to only the ones that we intend to rename. An example might look something like this: ```ptyhon def main_func(x: int): pass def other_func(x: int): pass main_func(x=100) ``` In the above case, if we rename the x kwarg where `main_func` is called, we would expect that only the x that is part of main_func should be updated. The x that is part of main_func should be left alone even though it shares the same name as the kwarg we are renaming. This diff also adds a few tests to ensure this this functionality is working properly and we can properly filter kwargs to only those that belong to the function that defines them. Reviewed By: kinto0 Differential Revision: D86868380 fbshipit-source-id: 385fc8c25f28b767922aba3ed87db5c5af641831
1 parent 9a548b2 commit d3dafa6

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

pyrefly/lib/state/lsp.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,6 @@ impl<'a> Transaction<'a> {
21192119
.unwrap_or_default()
21202120
}
21212121

2122-
#[allow(dead_code)]
21232122
pub(self) fn collect_local_keyword_arguments_by_name(
21242123
&self,
21252124
handle: &Handle,
@@ -2153,6 +2152,47 @@ impl<'a> Transaction<'a> {
21532152
results
21542153
}
21552154

2155+
#[allow(dead_code)]
2156+
pub(crate) fn local_keyword_argument_references_from_parameter_definition(
2157+
&self,
2158+
handle: &Handle,
2159+
definition_range: TextRange,
2160+
expected_name: &Name,
2161+
) -> Option<Vec<TextRange>> {
2162+
let ast = self.get_ast(handle)?;
2163+
let keyword_args = self.collect_local_keyword_arguments_by_name(handle, expected_name);
2164+
let mut references = Vec::new();
2165+
2166+
let definition_module = self.get_module_info(handle)?;
2167+
2168+
for (kw_identifier, callee_kind) in keyword_args {
2169+
let callee_locations =
2170+
self.get_callee_location(handle, &callee_kind, &FindPreference::default());
2171+
2172+
for TextRangeWithModule {
2173+
module,
2174+
range: callee_def_range,
2175+
} in callee_locations
2176+
{
2177+
if module.path() == definition_module.path() {
2178+
// Refine to get the actual parameter location
2179+
if let Some(param_range) = self.refine_param_location_for_callee(
2180+
ast.as_ref(),
2181+
callee_def_range,
2182+
&kw_identifier,
2183+
) {
2184+
// If the parameter location matches our definition, this is a valid reference
2185+
if param_range == definition_range {
2186+
references.push(kw_identifier.range);
2187+
}
2188+
}
2189+
}
2190+
}
2191+
}
2192+
2193+
Some(references)
2194+
}
2195+
21562196
fn local_variable_references_from_local_definition(
21572197
&self,
21582198
handle: &Handle,

0 commit comments

Comments
 (0)