Skip to content

Commit fa355d6

Browse files
committed
Rename back to highlight and check event's again highlight range
1 parent 8f6f864 commit fa355d6

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
lines changed

crates/ra_ide/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,14 @@ impl Analysis {
425425
self.with_db(|db| runnables::runnables(db, file_id))
426426
}
427427

428-
/// Computes syntax highlighting for the given file.
428+
/// Computes syntax highlighting for the given file
429429
pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> {
430-
self.with_db(|db| syntax_highlighting::highlight(db, file_id))
430+
self.with_db(|db| syntax_highlighting::highlight(db, file_id, None))
431431
}
432432

433433
/// Computes syntax highlighting for the given file range.
434434
pub fn highlight_range(&self, frange: FileRange) -> Cancelable<Vec<HighlightedRange>> {
435-
self.with_db(|db| {
436-
syntax_highlighting::highlight_range(db, frange.file_id, Some(frange.range))
437-
})
435+
self.with_db(|db| syntax_highlighting::highlight(db, frange.file_id, Some(frange.range)))
438436
}
439437

440438
/// Computes syntax highlighting for the given file.

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,12 @@ fn is_control_keyword(kind: SyntaxKind) -> bool {
6767
}
6868
}
6969

70-
pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> {
71-
let _p = profile("highlight");
72-
highlight_range(db, file_id, None)
73-
}
74-
75-
pub(crate) fn highlight_range(
70+
pub(crate) fn highlight(
7671
db: &RootDatabase,
7772
file_id: FileId,
7873
range: Option<TextRange>,
7974
) -> Vec<HighlightedRange> {
80-
let _p = profile("highlight_range");
75+
let _p = profile("highlight");
8176

8277
let parse = db.parse(file_id);
8378
let root = parse.tree().syntax().clone();
@@ -89,31 +84,56 @@ pub(crate) fn highlight_range(
8984

9085
let mut in_macro_call = None;
9186

92-
// Determine the root based on the range
93-
let root = match range {
94-
Some(range) => match root.covering_element(range) {
87+
// Determine the root based on the given range.
88+
let (root, highlight_range) = if let Some(range) = range {
89+
let root = match root.covering_element(range) {
9590
NodeOrToken::Node(node) => node,
9691
NodeOrToken::Token(token) => token.parent(),
97-
},
98-
None => root,
92+
};
93+
(root, range)
94+
} else {
95+
(root.clone(), root.text_range())
9996
};
10097

10198
for event in root.preorder_with_tokens() {
10299
match event {
103-
WalkEvent::Enter(node) => match node.kind() {
104-
MACRO_CALL => {
105-
in_macro_call = Some(node.clone());
106-
if let Some(range) = highlight_macro(InFile::new(file_id.into(), node)) {
107-
res.push(HighlightedRange { range, tag: tags::MACRO, binding_hash: None });
108-
}
100+
WalkEvent::Enter(node) => {
101+
if node.text_range().intersection(&highlight_range).is_none() {
102+
continue;
109103
}
110-
_ if in_macro_call.is_some() => {
111-
if let Some(token) = node.as_token() {
112-
if let Some((tag, binding_hash)) = highlight_token_tree(
104+
105+
match node.kind() {
106+
MACRO_CALL => {
107+
in_macro_call = Some(node.clone());
108+
if let Some(range) = highlight_macro(InFile::new(file_id.into(), node)) {
109+
res.push(HighlightedRange {
110+
range,
111+
tag: tags::MACRO,
112+
binding_hash: None,
113+
});
114+
}
115+
}
116+
_ if in_macro_call.is_some() => {
117+
if let Some(token) = node.as_token() {
118+
if let Some((tag, binding_hash)) = highlight_token_tree(
119+
&mut sb,
120+
&analyzer,
121+
&mut bindings_shadow_count,
122+
InFile::new(file_id.into(), token.clone()),
123+
) {
124+
res.push(HighlightedRange {
125+
range: node.text_range(),
126+
tag,
127+
binding_hash,
128+
});
129+
}
130+
}
131+
}
132+
_ => {
133+
if let Some((tag, binding_hash)) = highlight_node(
113134
&mut sb,
114-
&analyzer,
115135
&mut bindings_shadow_count,
116-
InFile::new(file_id.into(), token.clone()),
136+
InFile::new(file_id.into(), node.clone()),
117137
) {
118138
res.push(HighlightedRange {
119139
range: node.text_range(),
@@ -123,17 +143,12 @@ pub(crate) fn highlight_range(
123143
}
124144
}
125145
}
126-
_ => {
127-
if let Some((tag, binding_hash)) = highlight_node(
128-
&mut sb,
129-
&mut bindings_shadow_count,
130-
InFile::new(file_id.into(), node.clone()),
131-
) {
132-
res.push(HighlightedRange { range: node.text_range(), tag, binding_hash });
133-
}
134-
}
135-
},
146+
}
136147
WalkEvent::Leave(node) => {
148+
if node.text_range().intersection(&highlight_range).is_none() {
149+
continue;
150+
}
151+
137152
if let Some(m) = in_macro_call.as_ref() {
138153
if *m == node {
139154
in_macro_call = None;
@@ -284,7 +299,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
284299
)
285300
}
286301

287-
let mut ranges = highlight(db, file_id);
302+
let mut ranges = highlight(db, file_id, None);
288303
ranges.sort_by_key(|it| it.range.start());
289304
// quick non-optimal heuristic to intersect token ranges and highlighted ranges
290305
let mut frontier = 0;
@@ -509,10 +524,11 @@ fn bar() {
509524
}"#,
510525
);
511526

527+
// The "x"
512528
let highlights = &analysis
513529
.highlight_range(FileRange {
514530
file_id,
515-
range: TextRange::offset_len(82.into(), 1.into()), // "x"
531+
range: TextRange::offset_len(82.into(), 1.into()),
516532
})
517533
.unwrap();
518534

0 commit comments

Comments
 (0)