Skip to content

Commit

Permalink
lsp: Extract common code needed to rename a DeclaredIdentifier
Browse files Browse the repository at this point in the history
  • Loading branch information
hunger committed Dec 20, 2024
1 parent 2edb59d commit a259cff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
18 changes: 17 additions & 1 deletion tools/lsp/common/rename_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,22 @@ pub fn rename_component_from_definition(
document_cache: &common::DocumentCache,
identifier: &syntax_nodes::DeclaredIdentifier,
new_name: &str,
) -> crate::Result<lsp_types::WorkspaceEdit> {
rename_declared_identifier(document_cache, identifier, new_name, &change_local_element_type)
}

/// Helper function to rename a `DeclaredIdentifier`.
fn rename_declared_identifier(
document_cache: &common::DocumentCache,
identifier: &syntax_nodes::DeclaredIdentifier,
new_name: &str,
fixup_local_use: &dyn Fn(
&common::DocumentCache,
&syntax_nodes::Document,
&str,
&str,
&mut Vec<common::SingleTextEdit>,
),
) -> crate::Result<lsp_types::WorkspaceEdit> {
let source_file = identifier.source_file().expect("Identifier had no source file");
let document = document_cache
Expand Down Expand Up @@ -344,7 +360,7 @@ pub fn rename_component_from_definition(
);

// Change all local usages:
change_local_element_type(document_cache, document_node, &component_type, new_name, &mut edits);
fixup_local_use(document_cache, document_node, &component_type, new_name, &mut edits);

// Change exports
fix_exports(document_cache, document_node, &component_type, new_name, &mut edits);
Expand Down
28 changes: 17 additions & 11 deletions tools/lsp/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,18 +498,24 @@ pub fn register_request_handlers(rh: &mut RequestHandler) {
return Ok(Some(common::create_workspace_edit(uri, version, edits)));
}
match p.kind() {
SyntaxKind::DeclaredIdentifier => {
common::rename_component::rename_component_from_definition(
&document_cache,
&p.into(),
&params.new_name,
)
.map(Some)
.map_err(|e| LspError {
SyntaxKind::DeclaredIdentifier => match p.parent().map(|gp| gp.kind()) {
Some(SyntaxKind::Component) => {
common::rename_component::rename_component_from_definition(
&document_cache,
&p.into(),
&params.new_name,
)
.map(Some)
.map_err(|e| LspError {
code: LspErrorCode::RequestFailed,
message: e.to_string(),
})
}
None | Some(_) => Err(LspError {
code: LspErrorCode::RequestFailed,
message: e.to_string(),
})
}
message: "This symbol cannot be renamed.".into(),
}),
},
_ => Err(LspError {
code: LspErrorCode::RequestFailed,
message: "This symbol cannot be renamed.".into(),
Expand Down

0 comments on commit a259cff

Please sign in to comment.