Skip to content

Commit 41d447e

Browse files
committed
Fix rename handle
1 parent 9dabcb2 commit 41d447e

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

crates/emmylua_ls/src/handlers/rename/rename_decl.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use std::collections::HashMap;
22

33
use emmylua_code_analysis::{LuaCompilation, LuaDeclId, SemanticModel};
4+
use emmylua_parser::{
5+
LuaAst, LuaAstNode, LuaAstToken, LuaClosureExpr, LuaCommentOwner, LuaDocTagParam, LuaStat,
6+
LuaTableField,
7+
};
48
use lsp_types::Uri;
59

610
pub fn rename_decl_references(
@@ -37,6 +41,10 @@ pub fn rename_decl_references(
3741
.or_insert_with(HashMap::new)
3842
.insert(decl_range, new_name.clone());
3943

44+
if decl.is_param() {
45+
rename_doc_param(semantic_model, decl_id, new_name, result);
46+
}
47+
4048
return Some(());
4149
} else {
4250
let name = decl.get_name();
@@ -80,3 +88,49 @@ fn get_decl_name_token_lsp_range(
8088
let document = semantic_model.get_document_by_file_id(decl_id.file_id)?;
8189
document.to_lsp_range(decl.get_range())
8290
}
91+
92+
fn rename_doc_param(
93+
semantic_model: &SemanticModel,
94+
decl_id: LuaDeclId,
95+
new_name: String,
96+
result: &mut HashMap<Uri, HashMap<lsp_types::Range, String>>,
97+
) -> Option<()> {
98+
let decl = semantic_model
99+
.get_db()
100+
.get_decl_index()
101+
.get_decl(&decl_id)?;
102+
let name = decl.get_name();
103+
let syntax_id = decl.get_syntax_id();
104+
let root = semantic_model.get_root();
105+
let param_node = LuaAst::cast(syntax_id.to_node_from_root(root.syntax())?)?;
106+
let closure_expr = param_node.ancestors::<LuaClosureExpr>().next()?;
107+
let comments = if let Some(table_field) = closure_expr.get_parent::<LuaTableField>() {
108+
table_field.get_comments()
109+
} else if let Some(stat) = closure_expr.ancestors::<LuaStat>().next() {
110+
stat.get_comments()
111+
} else {
112+
return None;
113+
};
114+
115+
let document = semantic_model.get_document();
116+
let uri = document.get_uri();
117+
for comment in comments {
118+
for tag_doc in comment.get_doc_tags() {
119+
if let Some(doc_param) = LuaDocTagParam::cast(tag_doc.syntax().clone()) {
120+
if let Some(name_token) = doc_param.get_name_token() {
121+
if name_token.get_text() != name {
122+
continue;
123+
}
124+
125+
let range = document.to_lsp_range(name_token.get_range())?;
126+
result
127+
.entry(uri.clone())
128+
.or_insert_with(HashMap::new)
129+
.insert(range, new_name.clone());
130+
}
131+
}
132+
}
133+
}
134+
135+
Some(())
136+
}

crates/emmylua_parser/src/syntax/node/lua/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod test;
66
use crate::{
77
kind::{LuaSyntaxKind, LuaTokenKind},
88
syntax::traits::{LuaAstChildren, LuaAstNode, LuaAstToken},
9-
LuaSyntaxNode,
9+
LuaCommentOwner, LuaSyntaxNode,
1010
};
1111

1212
pub use expr::*;
@@ -247,6 +247,8 @@ impl LuaAstNode for LuaTableField {
247247
}
248248
}
249249

250+
impl LuaCommentOwner for LuaTableField {}
251+
250252
impl LuaTableField {
251253
pub fn is_assign_field(&self) -> bool {
252254
self.syntax().kind() == LuaSyntaxKind::TableFieldAssign.into()

0 commit comments

Comments
 (0)