@@ -213,24 +213,26 @@ pub fn goto_str_tpl_ref_definition(
213
213
}
214
214
_ => params. get ( string_token_idx) ,
215
215
} ?;
216
- if let Some ( LuaType :: StrTplRef ( str_tpl) ) = target_param. 1 . clone ( ) {
217
- let prefix = str_tpl. get_prefix ( ) ;
218
- let suffix = str_tpl. get_suffix ( ) ;
219
- let type_decl_id = LuaTypeDeclId :: new ( format ! ( "{}{}{}" , prefix, name, suffix) . as_str ( ) ) ;
220
- let type_decl = semantic_model
221
- . get_db ( )
222
- . get_type_index ( )
223
- . get_type_decl ( & type_decl_id) ?;
224
- let mut locations = Vec :: new ( ) ;
225
- for lua_location in type_decl. get_locations ( ) {
226
- let document = semantic_model. get_document_by_file_id ( lua_location. file_id ) ?;
227
- let location = document. to_lsp_location ( lua_location. range ) ?;
228
- locations. push ( location) ;
229
- }
230
-
216
+ // 首先尝试直接匹配StrTplRef类型
217
+ if let Some ( locations) =
218
+ try_extract_str_tpl_ref_locations ( semantic_model, & target_param. 1 , & name)
219
+ {
231
220
return Some ( GotoDefinitionResponse :: Array ( locations) ) ;
232
221
}
233
222
223
+ // 如果参数类型是union,尝试从中提取StrTplRef类型
224
+ if let Some ( LuaType :: Union ( union_type) ) = target_param. 1 . clone ( ) {
225
+ for union_member in union_type. get_types ( ) {
226
+ if let Some ( locations) = try_extract_str_tpl_ref_locations (
227
+ semantic_model,
228
+ & Some ( union_member. clone ( ) ) ,
229
+ & name,
230
+ ) {
231
+ return Some ( GotoDefinitionResponse :: Array ( locations) ) ;
232
+ }
233
+ }
234
+ }
235
+
234
236
None
235
237
}
236
238
@@ -336,3 +338,27 @@ fn get_decl_location(semantic_model: &SemanticModel, decl_id: &LuaDeclId) -> Opt
336
338
let location = document. to_lsp_location ( decl. get_range ( ) ) ?;
337
339
Some ( location)
338
340
}
341
+
342
+ fn try_extract_str_tpl_ref_locations (
343
+ semantic_model : & SemanticModel ,
344
+ param_type : & Option < LuaType > ,
345
+ name : & str ,
346
+ ) -> Option < Vec < Location > > {
347
+ if let Some ( LuaType :: StrTplRef ( str_tpl) ) = param_type {
348
+ let prefix = str_tpl. get_prefix ( ) ;
349
+ let suffix = str_tpl. get_suffix ( ) ;
350
+ let type_decl_id = LuaTypeDeclId :: new ( format ! ( "{}{}{}" , prefix, name, suffix) . as_str ( ) ) ;
351
+ let type_decl = semantic_model
352
+ . get_db ( )
353
+ . get_type_index ( )
354
+ . get_type_decl ( & type_decl_id) ?;
355
+ let mut locations = Vec :: new ( ) ;
356
+ for lua_location in type_decl. get_locations ( ) {
357
+ let document = semantic_model. get_document_by_file_id ( lua_location. file_id ) ?;
358
+ let location = document. to_lsp_location ( lua_location. range ) ?;
359
+ locations. push ( location) ;
360
+ }
361
+ return Some ( locations) ;
362
+ }
363
+ None
364
+ }
0 commit comments