Skip to content

Commit 7bd48a6

Browse files
committed
use static data for namespace recognition
1 parent cbdb6bd commit 7bd48a6

File tree

3 files changed

+49
-56
lines changed

3 files changed

+49
-56
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_ide/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ url = "*"
2121
pulldown-cmark-to-cmark = "4.0.2"
2222
pulldown-cmark = "0.7.0"
2323
oorandom = "11.1.2"
24-
once_cell = "1"
2524

2625
stdx = { path = "../stdx" }
2726

crates/ra_ide/src/hover.rs

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
use std::collections::{HashMap, HashSet};
2-
use std::{
3-
iter::{once, FromIterator},
4-
sync::Mutex,
5-
};
1+
use std::iter::once;
62

73
use hir::{
84
db::DefDatabase, Adt, AsAssocItem, AsName, AssocItemContainer, AttrDef, Crate, Documentation,
95
FieldSource, HasSource, HirDisplay, Hygiene, ItemInNs, ModPath, Module, ModuleDef,
106
ModuleSource, Semantics,
117
};
128
use itertools::Itertools;
13-
use once_cell::sync::Lazy;
149
use pulldown_cmark::{CowStr, Event, Options, Parser, Tag};
1510
use pulldown_cmark_to_cmark::cmark;
1611
use ra_db::SourceDatabase;
@@ -402,10 +397,9 @@ fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) ->
402397
try_resolve_path(db, definition, &target).map(|target| (target, title.to_string()))
403398
});
404399

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()),
409403
}
410404
}
411405
});
@@ -421,65 +415,61 @@ enum Namespace {
421415
Macros,
422416
}
423417

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"], ["!"]);
444423

445424
impl Namespace {
446425
/// Extract the specified namespace from an intra-doc-link if one exists.
447426
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)
455448
&& s.chars()
456-
.nth(prefix.len() + 1)
449+
.nth(suffix.len() + 1)
457450
.map(|c| c == '@' || c == ' ')
458451
.unwrap_or(false)
459452
})
460453
.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()
474457
}
475458
}
476459

477460
// Strip prefixes, suffixes, and inline code marks from the given string.
478461
fn strip_prefixes_suffixes(mut s: &str) -> &str {
479462
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));
483473
});
484474
s.trim_start_matches("@").trim()
485475
}
@@ -493,6 +483,8 @@ fn try_resolve_intra(
493483
link_text: &str,
494484
link_target: &str,
495485
) -> Option<(String, String)> {
486+
eprintln!("resolving intra");
487+
496488
// Set link_target for implied shortlinks
497489
let link_target =
498490
if link_target.is_empty() { link_text.trim_matches('`') } else { link_target };
@@ -551,6 +543,8 @@ fn try_resolve_intra(
551543

552544
/// Try to resolve path to local documentation via path-based links (i.e. `../gateway/struct.Shard.html`).
553545
fn try_resolve_path(db: &RootDatabase, definition: &Definition, link: &str) -> Option<String> {
546+
eprintln!("resolving path");
547+
554548
if !link.contains("#") && !link.contains(".html") {
555549
return None;
556550
}

0 commit comments

Comments
 (0)