Skip to content

Commit 6ae6c14

Browse files
bors[bot]matklad
andauthored
Merge #5420
5420: Unclutter NavigationTarget API r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents f157a09 + d7548a3 commit 6ae6c14

File tree

11 files changed

+166
-198
lines changed

11 files changed

+166
-198
lines changed

crates/ra_ide/src/call_hierarchy.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ mod tests {
154154
let nav = navs.pop().unwrap();
155155
nav.assert_match(expected);
156156

157-
let item_pos = FilePosition { file_id: nav.file_id(), offset: nav.range().start() };
157+
let item_pos =
158+
FilePosition { file_id: nav.file_id, offset: nav.focus_or_full_range().start() };
158159
let incoming_calls = analysis.incoming_calls(item_pos).unwrap().unwrap();
159160
assert_eq!(incoming_calls.len(), expected_incoming.len());
160161

crates/ra_ide/src/display/navigation_target.rs

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,28 @@ use super::short_label::ShortLabel;
2222
/// code, like a function or a struct, but this is not strictly required.
2323
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2424
pub struct NavigationTarget {
25-
// FIXME: use FileRange?
26-
file_id: FileId,
27-
full_range: TextRange,
28-
name: SmolStr,
29-
kind: SyntaxKind,
30-
focus_range: Option<TextRange>,
31-
container_name: Option<SmolStr>,
32-
description: Option<String>,
33-
docs: Option<String>,
25+
pub file_id: FileId,
26+
/// Range which encompasses the whole element.
27+
///
28+
/// Should include body, doc comments, attributes, etc.
29+
///
30+
/// Clients should use this range to answer "is the cursor inside the
31+
/// element?" question.
32+
pub full_range: TextRange,
33+
/// A "most interesting" range withing the `full_range`.
34+
///
35+
/// Typically, `full_range` is the whole syntax node, including doc
36+
/// comments, and `focus_range` is the range of the identifier. "Most
37+
/// interesting" range within the full range, typically the range of
38+
/// identifier.
39+
///
40+
/// Clients should place the cursor on this range when navigating to this target.
41+
pub focus_range: Option<TextRange>,
42+
pub name: SmolStr,
43+
pub kind: SyntaxKind,
44+
pub container_name: Option<SmolStr>,
45+
pub description: Option<String>,
46+
pub docs: Option<String>,
3447
}
3548

3649
pub(crate) trait ToNav {
@@ -42,44 +55,9 @@ pub(crate) trait TryToNav {
4255
}
4356

4457
impl NavigationTarget {
45-
/// When `focus_range` is specified, returns it. otherwise
46-
/// returns `full_range`
47-
pub fn range(&self) -> TextRange {
58+
pub fn focus_or_full_range(&self) -> TextRange {
4859
self.focus_range.unwrap_or(self.full_range)
4960
}
50-
/// A "most interesting" range withing the `full_range`.
51-
///
52-
/// Typically, `full_range` is the whole syntax node,
53-
/// including doc comments, and `focus_range` is the range of the identifier.
54-
pub fn focus_range(&self) -> Option<TextRange> {
55-
self.focus_range
56-
}
57-
pub fn full_range(&self) -> TextRange {
58-
self.full_range
59-
}
60-
pub fn file_id(&self) -> FileId {
61-
self.file_id
62-
}
63-
64-
pub fn name(&self) -> &SmolStr {
65-
&self.name
66-
}
67-
68-
pub fn container_name(&self) -> Option<&SmolStr> {
69-
self.container_name.as_ref()
70-
}
71-
72-
pub fn kind(&self) -> SyntaxKind {
73-
self.kind
74-
}
75-
76-
pub fn docs(&self) -> Option<&str> {
77-
self.docs.as_deref()
78-
}
79-
80-
pub fn description(&self) -> Option<&str> {
81-
self.description.as_deref()
82-
}
8361

8462
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
8563
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
@@ -107,17 +85,12 @@ impl NavigationTarget {
10785

10886
#[cfg(test)]
10987
pub(crate) fn debug_render(&self) -> String {
110-
let mut buf = format!(
111-
"{} {:?} {:?} {:?}",
112-
self.name(),
113-
self.kind(),
114-
self.file_id(),
115-
self.full_range()
116-
);
117-
if let Some(focus_range) = self.focus_range() {
88+
let mut buf =
89+
format!("{} {:?} {:?} {:?}", self.name, self.kind, self.file_id, self.full_range);
90+
if let Some(focus_range) = self.focus_range {
11891
buf.push_str(&format!(" {:?}", focus_range))
11992
}
120-
if let Some(container_name) = self.container_name() {
93+
if let Some(container_name) = &self.container_name {
12194
buf.push_str(&format!(" {}", container_name))
12295
}
12396
buf

crates/ra_ide/src/goto_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ mod tests {
130130
assert_eq!(navs.len(), 1);
131131

132132
let nav = navs.pop().unwrap();
133-
assert_eq!(expected, FileRange { file_id: nav.file_id(), range: nav.range() });
133+
assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() });
134134
}
135135

136136
#[test]

crates/ra_ide/src/goto_implementation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ mod tests {
9898

9999
let mut actual = navs
100100
.into_iter()
101-
.map(|nav| FileRange { file_id: nav.file_id(), range: nav.range() })
101+
.map(|nav| FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() })
102102
.collect::<Vec<_>>();
103103
actual.sort_by_key(key);
104104

crates/ra_ide/src/goto_type_definition.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ mod tests {
6767
let mut navs = analysis.goto_type_definition(position).unwrap().unwrap().info;
6868
assert_eq!(navs.len(), 1);
6969
let nav = navs.pop().unwrap();
70-
assert_eq!(expected, FileRange { file_id: nav.file_id(), range: nav.range() });
70+
assert_eq!(expected, FileRange { file_id: nav.file_id, range: nav.focus_or_full_range() });
7171
}
7272

7373
#[test]

0 commit comments

Comments
 (0)