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,53 +62,58 @@ fn replace_element_types(
if common::is_element_node_ignored(element) { if common::is_element_node_ignored(element) {
return; return;
} }
if let Some(name) = element.QualifiedName() { action(element, edits);
if name.text().to_string().trim() == old_type {
edits.push(
common::SingleTextEdit::from_path(
document_cache,
element.source_file.path(),
lsp_types::TextEdit {
range: util::node_to_lsp_range(&name),
new_text: new_type.to_string(),
},
)
.expect("URL conversion can not fail here"),
)
}
}
for c in element.children() { for c in element.children() {
match c.kind() { match c.kind() {
SyntaxKind::SubElement => { SyntaxKind::SubElement => {
let e: syntax_nodes::SubElement = c.into(); let e: syntax_nodes::SubElement = c.into();
replace_element_types(document_cache, &e.Element(), old_type, new_type, edits); replace_in_all_elements(document_cache, &e.Element(), action, edits);
} }
SyntaxKind::RepeatedElement => { SyntaxKind::RepeatedElement => {
let e: syntax_nodes::RepeatedElement = c.into(); let e: syntax_nodes::RepeatedElement = c.into();
replace_element_types( replace_in_all_elements(document_cache, &e.SubElement().Element(), action, edits);
document_cache,
&e.SubElement().Element(),
old_type,
new_type,
edits,
);
} }
SyntaxKind::ConditionalElement => { SyntaxKind::ConditionalElement => {
let e: syntax_nodes::ConditionalElement = c.into(); let e: syntax_nodes::ConditionalElement = c.into();
replace_element_types( replace_in_all_elements(document_cache, &e.SubElement().Element(), action, edits);
document_cache,
&e.SubElement().Element(),
old_type,
new_type,
edits,
);
} }
_ => { /* do nothing */ } _ => { /* 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 name.text().to_string().trim() == old_type {
edits.push(
common::SingleTextEdit::from_path(
document_cache,
element.source_file.path(),
lsp_types::TextEdit {
range: util::node_to_lsp_range(&name),
new_text: new_type.to_string(),
},
)
.expect("URL conversion can not fail here"),
)
}
}
},
edits,
)
}
fn fix_imports( fn fix_imports(
document_cache: &common::DocumentCache, document_cache: &common::DocumentCache,
exporter_path: &Path, exporter_path: &Path,