@@ -5,7 +5,7 @@ mod rename_type;
55use std:: collections:: HashMap ;
66
77use emmylua_code_analysis:: { LuaCompilation , LuaSemanticDeclId , SemanticDeclLevel , SemanticModel } ;
8- use emmylua_parser:: { LuaAstNode , LuaSyntaxToken , LuaTokenKind } ;
8+ use emmylua_parser:: { LuaAstNode , LuaLiteralExpr , LuaSyntaxToken , LuaTableField , LuaTokenKind } ;
99use lsp_types:: {
1010 ClientCapabilities , OneOf , PrepareRenameResponse , RenameOptions , RenameParams ,
1111 ServerCapabilities , TextDocumentPositionParams , WorkspaceEdit ,
@@ -29,37 +29,7 @@ pub async fn on_rename_handler(
2929 let analysis = context. analysis . read ( ) . await ;
3030 let file_id = analysis. get_file_id ( & uri) ?;
3131 let position = params. text_document_position . position ;
32- let mut semantic_model = analysis. compilation . get_semantic_model ( file_id) ?;
33- let root = semantic_model. get_root ( ) ;
34- let position_offset = {
35- let document = semantic_model. get_document ( ) ;
36- document. get_offset ( position. line as usize , position. character as usize ) ?
37- } ;
38-
39- if position_offset > root. syntax ( ) . text_range ( ) . end ( ) {
40- return None ;
41- }
42-
43- let token = match root. syntax ( ) . token_at_offset ( position_offset) {
44- TokenAtOffset :: Single ( token) => token,
45- TokenAtOffset :: Between ( left, right) => {
46- if left. kind ( ) == LuaTokenKind :: TkName . into ( ) {
47- left
48- } else {
49- right
50- }
51- }
52- TokenAtOffset :: None => {
53- return None ;
54- }
55- } ;
56-
57- rename_references (
58- & mut semantic_model,
59- & analysis. compilation ,
60- token,
61- params. new_name ,
62- )
32+ rename ( & analysis, file_id, position, params. new_name )
6333}
6434
6535pub async fn on_prepare_rename_handler (
@@ -94,7 +64,6 @@ pub async fn on_prepare_rename_handler(
9464 return None ;
9565 }
9666 } ;
97-
9867 if matches ! (
9968 token. kind( ) . into( ) ,
10069 LuaTokenKind :: TkName | LuaTokenKind :: TkInt | LuaTokenKind :: TkString
@@ -107,14 +76,55 @@ pub async fn on_prepare_rename_handler(
10776 }
10877}
10978
79+ pub fn rename (
80+ analysis : & emmylua_code_analysis:: EmmyLuaAnalysis ,
81+ file_id : emmylua_code_analysis:: FileId ,
82+ position : lsp_types:: Position ,
83+ new_name : String ,
84+ ) -> Option < WorkspaceEdit > {
85+ let mut semantic_model = analysis. compilation . get_semantic_model ( file_id) ?;
86+ let root = semantic_model. get_root ( ) ;
87+ let position_offset = {
88+ let document = semantic_model. get_document ( ) ;
89+ document. get_offset ( position. line as usize , position. character as usize ) ?
90+ } ;
91+
92+ if position_offset > root. syntax ( ) . text_range ( ) . end ( ) {
93+ return None ;
94+ }
95+
96+ let token = match root. syntax ( ) . token_at_offset ( position_offset) {
97+ TokenAtOffset :: Single ( token) => token,
98+ TokenAtOffset :: Between ( left, right) => {
99+ if left. kind ( ) == LuaTokenKind :: TkName . into ( ) {
100+ left
101+ } else {
102+ right
103+ }
104+ }
105+ TokenAtOffset :: None => {
106+ return None ;
107+ }
108+ } ;
109+
110+ rename_references ( & mut semantic_model, & analysis. compilation , token, new_name)
111+ }
112+
110113fn rename_references (
111114 semantic_model : & SemanticModel ,
112115 compilation : & LuaCompilation ,
113116 token : LuaSyntaxToken ,
114117 new_name : String ,
115118) -> Option < WorkspaceEdit > {
116119 let mut result = HashMap :: new ( ) ;
117- let semantic_decl = semantic_model. find_decl ( token. into ( ) , SemanticDeclLevel :: NoTrace ) ?;
120+ let semantic_decl = match try_get_table_field ( token. clone ( ) ) {
121+ Some ( table_field) => semantic_model. find_decl (
122+ table_field. syntax ( ) . clone ( ) . into ( ) ,
123+ SemanticDeclLevel :: NoTrace ,
124+ ) ,
125+ None => semantic_model. find_decl ( token. into ( ) , SemanticDeclLevel :: NoTrace ) ,
126+ } ?;
127+
118128 match semantic_decl {
119129 LuaSemanticDeclId :: LuaDecl ( decl_id) => {
120130 rename_decl_references ( semantic_model, compilation, decl_id, new_name, & mut result) ;
@@ -159,6 +169,12 @@ fn rename_references(
159169 } )
160170}
161171
172+ fn try_get_table_field ( token : LuaSyntaxToken ) -> Option < LuaTableField > {
173+ let parent = token. parent ( ) ?;
174+ let literal_expr = LuaLiteralExpr :: cast ( parent) ?;
175+ literal_expr. get_parent :: < LuaTableField > ( )
176+ }
177+
162178pub struct RenameCapabilities ;
163179
164180impl RegisterCapabilities for RenameCapabilities {
0 commit comments