lsp: Generalize some renaming related functionality

This commit is contained in:
Tobias Hunger 2024-12-17 15:21:17 +01:00 committed by Tobias Hunger
parent 6b4db19fb0
commit 7893c5a00c

View file

@ -47,11 +47,10 @@ fn symbol_export_names(document_node: &syntax_nodes::Document, type_name: &str)
result result
} }
fn replace_element_types( fn replace_in_all_elements(
document_cache: &common::DocumentCache, document_cache: &common::DocumentCache,
element: &syntax_nodes::Element, element: &syntax_nodes::Element,
old_type: &str, action: &mut dyn FnMut(&syntax_nodes::Element, &mut Vec<common::SingleTextEdit>),
new_type: &str,
edits: &mut Vec<common::SingleTextEdit>, edits: &mut Vec<common::SingleTextEdit>,
) { ) {
// HACK: We inject an ignored component into the live preview. Do not // HACK: We inject an ignored component into the live preview. Do not
@ -63,6 +62,38 @@ fn replace_element_types(
if common::is_element_node_ignored(element) { if common::is_element_node_ignored(element) {
return; return;
} }
action(element, edits);
for c in element.children() {
match c.kind() {
SyntaxKind::SubElement => {
let e: syntax_nodes::SubElement = c.into();
replace_in_all_elements(document_cache, &e.Element(), action, edits);
}
SyntaxKind::RepeatedElement => {
let e: syntax_nodes::RepeatedElement = c.into();
replace_in_all_elements(document_cache, &e.SubElement().Element(), action, edits);
}
SyntaxKind::ConditionalElement => {
let e: syntax_nodes::ConditionalElement = c.into();
replace_in_all_elements(document_cache, &e.SubElement().Element(), action, edits);
}
_ => { /* do nothing */ }
}
}
}
fn replace_element_types(
document_cache: &common::DocumentCache,
element: &syntax_nodes::Element,
old_type: &str,
new_type: &str,
edits: &mut Vec<common::SingleTextEdit>,
) {
replace_in_all_elements(
document_cache,
element,
&mut |element, edits| {
if let Some(name) = element.QualifiedName() { if let Some(name) = element.QualifiedName() {
if name.text().to_string().trim() == old_type { if name.text().to_string().trim() == old_type {
edits.push( edits.push(
@ -78,36 +109,9 @@ fn replace_element_types(
) )
} }
} }
},
for c in element.children() {
match c.kind() {
SyntaxKind::SubElement => {
let e: syntax_nodes::SubElement = c.into();
replace_element_types(document_cache, &e.Element(), old_type, new_type, edits);
}
SyntaxKind::RepeatedElement => {
let e: syntax_nodes::RepeatedElement = c.into();
replace_element_types(
document_cache,
&e.SubElement().Element(),
old_type,
new_type,
edits, edits,
); )
}
SyntaxKind::ConditionalElement => {
let e: syntax_nodes::ConditionalElement = c.into();
replace_element_types(
document_cache,
&e.SubElement().Element(),
old_type,
new_type,
edits,
);
}
_ => { /* do nothing */ }
}
}
} }
fn fix_imports( fn fix_imports(