@@ -376,8 +376,7 @@ fn build_signature_ret_type(
376376 i : usize ,
377377) -> String {
378378 let type_expansion_count = builder. get_type_expansion_count ( ) ;
379- let type_text =
380- hover_type ( builder, & ret_info. type_ref , Some ( RenderLevel :: Simple ) ) . unwrap_or_default ( ) ;
379+ let type_text = hover_type ( builder, & ret_info. type_ref , Some ( RenderLevel :: Simple ) ) ;
381380 if builder. get_type_expansion_count ( ) > type_expansion_count {
382381 // 重新设置`type_expansion`
383382 if let Some ( pop_type_expansion) =
@@ -419,37 +418,30 @@ pub fn hover_type(
419418 builder : & mut HoverBuilder ,
420419 ty : & LuaType ,
421420 fallback_level : Option < RenderLevel > , // 当有值时, 若获取类型描述为空会回退到使用`humanize_type()`
422- ) -> Option < String > {
421+ ) -> String {
423422 let db = builder. semantic_model . get_db ( ) ;
424- let type_text = match ty {
423+ match ty {
425424 LuaType :: Ref ( type_decl_id) => {
426- let type_decl = db. get_type_index ( ) . get_type_decl ( type_decl_id) ?;
427-
428- if type_decl. is_alias ( ) {
429- let origin = type_decl. get_alias_origin ( db, None ) ;
430- match origin {
431- Some ( LuaType :: MultiLineUnion ( multi_union) ) => hover_multi_line_union_type (
425+ if let Some ( type_decl) = db. get_type_index ( ) . get_type_decl ( type_decl_id) {
426+ if let Some ( LuaType :: MultiLineUnion ( multi_union) ) =
427+ type_decl. get_alias_origin ( db, None )
428+ {
429+ return hover_multi_line_union_type (
432430 builder,
433431 db,
434432 multi_union. as_ref ( ) ,
435433 Some ( type_decl. get_full_name ( ) ) ,
436- ) ,
437- _ => None ,
434+ )
435+ . unwrap_or_default ( ) ;
438436 }
439- } else {
440- None
441437 }
438+ humanize_type ( db, ty, fallback_level. unwrap_or ( RenderLevel :: Simple ) )
442439 }
443440 LuaType :: MultiLineUnion ( multi_union) => {
444- hover_multi_line_union_type ( builder, db, multi_union. as_ref ( ) , None )
441+ hover_multi_line_union_type ( builder, db, multi_union. as_ref ( ) , None ) . unwrap_or_default ( )
445442 }
446- LuaType :: Union ( union) => Some ( hover_union_type ( builder, union, RenderLevel :: Detailed ) ) ,
447- _ => None ,
448- } ;
449- match ( fallback_level, type_text) {
450- ( Some ( level) , Some ( text) ) if text. is_empty ( ) => Some ( humanize_type ( db, ty, level) ) ,
451- ( Some ( level) , None ) => Some ( humanize_type ( db, ty, level) ) ,
452- ( _, text) => text,
443+ LuaType :: Union ( union) => hover_union_type ( builder, union, RenderLevel :: Detailed ) ,
444+ _ => humanize_type ( db, ty, fallback_level. unwrap_or ( RenderLevel :: Simple ) ) ,
453445 }
454446}
455447
@@ -468,17 +460,34 @@ fn hover_union_type(
468460 return "union<...>" . to_string ( ) ;
469461 }
470462 } ;
471- let type_str = types
472- . iter ( )
473- . take ( num)
474- . filter_map ( |ty| hover_type ( builder, ty, None ) )
475- . collect :: < Vec < _ > > ( )
476- . join ( "|" ) ;
477- if type_str. is_empty ( ) {
478- return "" . to_string ( ) ;
463+ // 需要确保顺序
464+ let mut seen = HashSet :: new ( ) ;
465+ let mut type_strings = Vec :: new ( ) ;
466+ let mut has_nil = false ;
467+ for ty in types. iter ( ) {
468+ if ty. is_nil ( ) {
469+ has_nil = true ;
470+ continue ;
471+ }
472+ let type_str = hover_type ( builder, ty, Some ( level. next_level ( ) ) ) ;
473+ if seen. insert ( type_str. clone ( ) ) {
474+ type_strings. push ( type_str) ;
475+ }
476+ }
477+ // 取指定数量的类型
478+ let display_types: Vec < _ > = type_strings. into_iter ( ) . take ( num) . collect ( ) ;
479+ let type_str = display_types. join ( "|" ) ;
480+ let dots = if display_types. len ( ) < types. len ( ) {
481+ "..."
482+ } else {
483+ ""
484+ } ;
485+
486+ if display_types. len ( ) == 1 {
487+ format ! ( "{}{}" , type_str, if has_nil { "?" } else { "" } )
488+ } else {
489+ format ! ( "({}{}){}" , type_str, dots, if has_nil { "?" } else { "" } )
479490 }
480- let dots = if types. len ( ) > num { "..." } else { "" } ;
481- format ! ( "({}{})" , type_str, dots)
482491}
483492
484493fn hover_multi_line_union_type (
0 commit comments