@@ -240,11 +240,9 @@ pub(super) fn keyword(
240
240
let parent = token. parent ( ) ?;
241
241
let famous_defs = FamousDefs ( sema, sema. scope ( & parent) . krate ( ) ) ;
242
242
243
- // some keywords get fancy type tooltips if they are apart of an expression, which require some extra work
244
- // panic safety: we just checked that token is a keyword, and we have it's parent in scope, so it must have a parent
245
- let KeywordHint { description, documentation, actions } = keyword_hints ( sema, token) ;
243
+ let KeywordHint { description, keyword_mod, actions } = keyword_hints ( sema, token, parent) ;
246
244
247
- let doc_owner = find_std_module ( & famous_defs, & documentation ) ?;
245
+ let doc_owner = find_std_module ( & famous_defs, & keyword_mod ) ?;
248
246
let docs = doc_owner. attrs ( sema. db ) . docs ( ) ?;
249
247
let markup = process_markup (
250
248
sema. db ,
@@ -501,29 +499,28 @@ fn local(db: &RootDatabase, it: hir::Local) -> Option<Markup> {
501
499
502
500
struct KeywordHint {
503
501
description : String ,
504
- documentation : String ,
502
+ keyword_mod : String ,
505
503
actions : Vec < HoverAction > ,
506
504
}
507
505
508
506
impl KeywordHint {
509
- fn new ( description : String , documentation : String ) -> Self {
510
- Self { description, documentation , actions : Vec :: default ( ) }
507
+ fn new ( description : String , keyword_mod : String ) -> Self {
508
+ Self { description, keyword_mod , actions : Vec :: default ( ) }
511
509
}
512
510
}
513
511
514
- /// Panics
515
- /// ------
516
- /// `token` is assumed to:
517
- /// - have a parent, and
518
- /// - be a keyword
519
- fn keyword_hints < ' t > ( sema : & Semantics < RootDatabase > , token : & ' t SyntaxToken ) -> KeywordHint {
520
- let parent = token. parent ( ) . expect ( "token was assumed to have a parent, but had none" ) ;
521
-
522
- macro_rules! create_hint {
523
- ( $ty_info: expr, $doc: expr) => { {
524
- let documentation = $doc;
525
- match $ty_info {
526
- Some ( ty) => {
512
+ fn keyword_hints (
513
+ sema : & Semantics < RootDatabase > ,
514
+ token : & SyntaxToken ,
515
+ parent : syntax:: SyntaxNode ,
516
+ ) -> KeywordHint {
517
+ match token. kind ( ) {
518
+ T ! [ await ] | T ! [ loop ] | T ! [ match ] | T ! [ unsafe ] | T ! [ as ] | T ! [ try] | T ! [ if ] | T ! [ else] => {
519
+ let keyword_mod = format ! ( "{}_keyword" , token. text( ) ) ;
520
+
521
+ match ast:: Expr :: cast ( parent) . and_then ( |site| sema. type_of_expr ( & site) ) {
522
+ // ignore the unit type ()
523
+ Some ( ty) if !ty. adjusted . as_ref ( ) . unwrap_or ( & ty. original ) . is_unit ( ) => {
527
524
let mut targets: Vec < hir:: ModuleDef > = Vec :: new ( ) ;
528
525
let mut push_new_def = |item : hir:: ModuleDef | {
529
526
if !targets. contains ( & item) {
@@ -537,40 +534,16 @@ fn keyword_hints<'t>(sema: &Semantics<RootDatabase>, token: &'t SyntaxToken) ->
537
534
538
535
KeywordHint {
539
536
description,
540
- documentation ,
537
+ keyword_mod ,
541
538
actions : vec ! [ HoverAction :: goto_type_from_targets( sema. db, targets) ] ,
542
539
}
543
540
}
544
- None => KeywordHint {
541
+ _ => KeywordHint {
545
542
description : token. text ( ) . to_string ( ) ,
546
- documentation ,
543
+ keyword_mod ,
547
544
actions : Vec :: new ( ) ,
548
545
} ,
549
546
}
550
- } } ;
551
- }
552
-
553
- match token. kind ( ) {
554
- T ! [ await ] | T ! [ loop ] | T ! [ match ] | T ! [ unsafe ] => {
555
- let ty = ast:: Expr :: cast ( parent) . and_then ( |site| sema. type_of_expr ( & site) ) ;
556
- create_hint ! ( ty, format!( "{}_keyword" , token. text( ) ) )
557
- }
558
-
559
- T ! [ if ] | T ! [ else] => {
560
- fn if_has_else ( site : & ast:: IfExpr ) -> bool {
561
- match site. else_branch ( ) {
562
- Some ( ast:: ElseBranch :: IfExpr ( inner) ) => if_has_else ( & inner) ,
563
- Some ( ast:: ElseBranch :: Block ( _) ) => true ,
564
- None => false ,
565
- }
566
- }
567
-
568
- // only include the type if there is an else branch; it isn't worth annotating
569
- // an expression that always returns `()`, is it?
570
- let ty = ast:: IfExpr :: cast ( parent)
571
- . and_then ( |site| if_has_else ( & site) . then ( || site) )
572
- . and_then ( |site| sema. type_of_expr ( & ast:: Expr :: IfExpr ( site) ) ) ;
573
- create_hint ! ( ty, format!( "{}_keyword" , token. text( ) ) )
574
547
}
575
548
576
549
T ! [ fn ] => {
@@ -582,10 +555,6 @@ fn keyword_hints<'t>(sema: &Semantics<RootDatabase>, token: &'t SyntaxToken) ->
582
555
KeywordHint :: new ( token. text ( ) . to_string ( ) , module)
583
556
}
584
557
585
- kind if kind. is_keyword ( ) => {
586
- KeywordHint :: new ( token. text ( ) . to_string ( ) , format ! ( "{}_keyword" , token. text( ) ) )
587
- }
588
-
589
- _ => panic ! ( "{} was assumed to be a keyword, but it wasn't" , token) ,
558
+ _ => KeywordHint :: new ( token. text ( ) . to_string ( ) , format ! ( "{}_keyword" , token. text( ) ) ) ,
590
559
}
591
560
}
0 commit comments