1
- use std:: collections:: { HashMap , HashSet } ;
2
- use std:: {
3
- iter:: { once, FromIterator } ,
4
- sync:: Mutex ,
5
- } ;
1
+ use std:: iter:: once;
6
2
7
3
use hir:: {
8
4
db:: DefDatabase , Adt , AsAssocItem , AsName , AssocItemContainer , AttrDef , Crate , Documentation ,
9
5
FieldSource , HasSource , HirDisplay , Hygiene , ItemInNs , ModPath , Module , ModuleDef ,
10
6
ModuleSource , Semantics ,
11
7
} ;
12
8
use itertools:: Itertools ;
13
- use once_cell:: sync:: Lazy ;
14
9
use pulldown_cmark:: { CowStr , Event , Options , Parser , Tag } ;
15
10
use pulldown_cmark_to_cmark:: cmark;
16
11
use ra_db:: SourceDatabase ;
@@ -402,10 +397,9 @@ fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) ->
402
397
try_resolve_path ( db, definition, & target) . map ( |target| ( target, title. to_string ( ) ) )
403
398
} ) ;
404
399
405
- if let Some ( ( target, title) ) = resolved {
406
- ( target, title)
407
- } else {
408
- ( target. to_string ( ) , title. to_string ( ) )
400
+ match resolved {
401
+ Some ( ( target, title) ) => ( target, title) ,
402
+ None => ( target. to_string ( ) , title. to_string ( ) ) ,
409
403
}
410
404
}
411
405
} ) ;
@@ -421,65 +415,61 @@ enum Namespace {
421
415
Macros ,
422
416
}
423
417
424
- static NS_MAP : Lazy <
425
- HashMap < Namespace , ( HashSet < & ' static & ' static str > , HashSet < & ' static & ' static str > ) > ,
426
- > = Lazy :: new ( || {
427
- let mut map = HashMap :: new ( ) ;
428
- map. insert ( Namespace :: Types , ( HashSet :: new ( ) , HashSet :: new ( ) ) ) ;
429
- map. insert (
430
- Namespace :: Values ,
431
- (
432
- HashSet :: from_iter (
433
- [ "value" , "function" , "fn" , "method" , "const" , "static" , "mod" , "module" ] . iter ( ) ,
434
- ) ,
435
- HashSet :: from_iter ( [ "()" ] . iter ( ) ) ,
436
- ) ,
437
- ) ;
438
- map. insert (
439
- Namespace :: Macros ,
440
- ( HashSet :: from_iter ( [ "macro" ] . iter ( ) ) , HashSet :: from_iter ( [ "!" ] . iter ( ) ) ) ,
441
- ) ;
442
- map
443
- } ) ;
418
+ static TYPES : ( [ & str ; 7 ] , [ & str ; 0 ] ) =
419
+ ( [ "type" , "struct" , "enum" , "mod" , "trait" , "union" , "module" ] , [ ] ) ;
420
+ static VALUES : ( [ & str ; 8 ] , [ & str ; 1 ] ) =
421
+ ( [ "value" , "function" , "fn" , "method" , "const" , "static" , "mod" , "module" ] , [ "()" ] ) ;
422
+ static MACROS : ( [ & str ; 1 ] , [ & str ; 1 ] ) = ( [ "macro" ] , [ "!" ] ) ;
444
423
445
424
impl Namespace {
446
425
/// Extract the specified namespace from an intra-doc-link if one exists.
447
426
fn from_intra_spec ( s : & str ) -> Option < Self > {
448
- NS_MAP
449
- . iter ( )
450
- . filter ( |( _ns, ( prefixes, suffixes) ) | {
451
- prefixes
452
- . iter ( )
453
- . map ( |prefix| {
454
- s. starts_with ( * prefix)
427
+ [
428
+ ( Namespace :: Types , ( TYPES . 0 . iter ( ) , TYPES . 1 . iter ( ) ) ) ,
429
+ ( Namespace :: Values , ( VALUES . 0 . iter ( ) , VALUES . 1 . iter ( ) ) ) ,
430
+ ( Namespace :: Macros , ( MACROS . 0 . iter ( ) , MACROS . 1 . iter ( ) ) ) ,
431
+ ]
432
+ . iter ( )
433
+ . filter ( |( _ns, ( prefixes, suffixes) ) | {
434
+ prefixes
435
+ . clone ( )
436
+ . map ( |prefix| {
437
+ s. starts_with ( * prefix)
438
+ && s. chars ( )
439
+ . nth ( prefix. len ( ) + 1 )
440
+ . map ( |c| c == '@' || c == ' ' )
441
+ . unwrap_or ( false )
442
+ } )
443
+ . any ( |cond| cond)
444
+ || suffixes
445
+ . clone ( )
446
+ . map ( |suffix| {
447
+ s. starts_with ( * suffix)
455
448
&& s. chars ( )
456
- . nth ( prefix . len ( ) + 1 )
449
+ . nth ( suffix . len ( ) + 1 )
457
450
. map ( |c| c == '@' || c == ' ' )
458
451
. unwrap_or ( false )
459
452
} )
460
453
. any ( |cond| cond)
461
- || suffixes
462
- . iter ( )
463
- . map ( |suffix| {
464
- s. starts_with ( * suffix)
465
- && s. chars ( )
466
- . nth ( suffix. len ( ) + 1 )
467
- . map ( |c| c == '@' || c == ' ' )
468
- . unwrap_or ( false )
469
- } )
470
- . any ( |cond| cond)
471
- } )
472
- . map ( |( ns, ( _, _) ) | * ns)
473
- . next ( )
454
+ } )
455
+ . map ( |( ns, ( _, _) ) | * ns)
456
+ . next ( )
474
457
}
475
458
}
476
459
477
460
// Strip prefixes, suffixes, and inline code marks from the given string.
478
461
fn strip_prefixes_suffixes ( mut s : & str ) -> & str {
479
462
s = s. trim_matches ( '`' ) ;
480
- NS_MAP . iter ( ) . for_each ( |( _, ( prefixes, suffixes) ) | {
481
- prefixes. iter ( ) . for_each ( |prefix| s = s. trim_start_matches ( * prefix) ) ;
482
- suffixes. iter ( ) . for_each ( |suffix| s = s. trim_end_matches ( * suffix) ) ;
463
+
464
+ [
465
+ ( TYPES . 0 . iter ( ) , TYPES . 1 . iter ( ) ) ,
466
+ ( VALUES . 0 . iter ( ) , VALUES . 1 . iter ( ) ) ,
467
+ ( MACROS . 0 . iter ( ) , MACROS . 1 . iter ( ) ) ,
468
+ ]
469
+ . iter ( )
470
+ . for_each ( |( prefixes, suffixes) | {
471
+ prefixes. clone ( ) . for_each ( |prefix| s = s. trim_start_matches ( * prefix) ) ;
472
+ suffixes. clone ( ) . for_each ( |suffix| s = s. trim_end_matches ( * suffix) ) ;
483
473
} ) ;
484
474
s. trim_start_matches ( "@" ) . trim ( )
485
475
}
@@ -493,6 +483,8 @@ fn try_resolve_intra(
493
483
link_text : & str ,
494
484
link_target : & str ,
495
485
) -> Option < ( String , String ) > {
486
+ eprintln ! ( "resolving intra" ) ;
487
+
496
488
// Set link_target for implied shortlinks
497
489
let link_target =
498
490
if link_target. is_empty ( ) { link_text. trim_matches ( '`' ) } else { link_target } ;
@@ -551,6 +543,8 @@ fn try_resolve_intra(
551
543
552
544
/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
553
545
fn try_resolve_path ( db : & RootDatabase , definition : & Definition , link : & str ) -> Option < String > {
546
+ eprintln ! ( "resolving path" ) ;
547
+
554
548
if !link. contains ( "#" ) && !link. contains ( ".html" ) {
555
549
return None ;
556
550
}
0 commit comments