|
1 | 1 | use std::collections::HashMap; |
2 | 2 |
|
3 | 3 | use emmylua_code_analysis::{LuaCompilation, LuaDeclId, SemanticModel}; |
| 4 | +use emmylua_parser::{ |
| 5 | + LuaAst, LuaAstNode, LuaAstToken, LuaClosureExpr, LuaCommentOwner, LuaDocTagParam, LuaStat, |
| 6 | + LuaTableField, |
| 7 | +}; |
4 | 8 | use lsp_types::Uri; |
5 | 9 |
|
6 | 10 | pub fn rename_decl_references( |
@@ -37,6 +41,10 @@ pub fn rename_decl_references( |
37 | 41 | .or_insert_with(HashMap::new) |
38 | 42 | .insert(decl_range, new_name.clone()); |
39 | 43 |
|
| 44 | + if decl.is_param() { |
| 45 | + rename_doc_param(semantic_model, decl_id, new_name, result); |
| 46 | + } |
| 47 | + |
40 | 48 | return Some(()); |
41 | 49 | } else { |
42 | 50 | let name = decl.get_name(); |
@@ -80,3 +88,49 @@ fn get_decl_name_token_lsp_range( |
80 | 88 | let document = semantic_model.get_document_by_file_id(decl_id.file_id)?; |
81 | 89 | document.to_lsp_range(decl.get_range()) |
82 | 90 | } |
| 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 | +} |
0 commit comments