Skip to content

Commit

Permalink
lsp: Generalize some renaming related functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
hunger committed Dec 20, 2024
1 parent a259cff commit ff504bd
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions tools/lsp/common/rename_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ fn symbol_export_names(document_node: &syntax_nodes::Document, type_name: &str)
result
}

fn replace_element_types(
fn replace_in_all_elements(
document_cache: &common::DocumentCache,
element: &syntax_nodes::Element,
old_type: &str,
new_type: &str,
action: &mut dyn FnMut(&syntax_nodes::Element, &mut Vec<common::SingleTextEdit>),
edits: &mut Vec<common::SingleTextEdit>,
) {
// HACK: We inject an ignored component into the live preview. Do not
Expand All @@ -63,53 +62,58 @@ fn replace_element_types(
if common::is_element_node_ignored(element) {
return;
}
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"),
)
}
}
action(element, edits);

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);
replace_in_all_elements(document_cache, &e.Element(), action, edits);
}
SyntaxKind::RepeatedElement => {
let e: syntax_nodes::RepeatedElement = c.into();
replace_element_types(
document_cache,
&e.SubElement().Element(),
old_type,
new_type,
edits,
);
replace_in_all_elements(document_cache, &e.SubElement().Element(), action, edits);
}
SyntaxKind::ConditionalElement => {
let e: syntax_nodes::ConditionalElement = c.into();
replace_element_types(
document_cache,
&e.SubElement().Element(),
old_type,
new_type,
edits,
);
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 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(
document_cache: &common::DocumentCache,
exporter_path: &Path,
Expand Down

0 comments on commit ff504bd

Please sign in to comment.