@@ -67,17 +67,12 @@ fn is_control_keyword(kind: SyntaxKind) -> bool {
67
67
}
68
68
}
69
69
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 (
76
71
db : & RootDatabase ,
77
72
file_id : FileId ,
78
73
range : Option < TextRange > ,
79
74
) -> Vec < HighlightedRange > {
80
- let _p = profile ( "highlight_range " ) ;
75
+ let _p = profile ( "highlight " ) ;
81
76
82
77
let parse = db. parse ( file_id) ;
83
78
let root = parse. tree ( ) . syntax ( ) . clone ( ) ;
@@ -89,31 +84,56 @@ pub(crate) fn highlight_range(
89
84
90
85
let mut in_macro_call = None ;
91
86
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) {
95
90
NodeOrToken :: Node ( node) => node,
96
91
NodeOrToken :: Token ( token) => token. parent ( ) ,
97
- } ,
98
- None => root,
92
+ } ;
93
+ ( root, range)
94
+ } else {
95
+ ( root. clone ( ) , root. text_range ( ) )
99
96
} ;
100
97
101
98
for event in root. preorder_with_tokens ( ) {
102
99
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 ;
109
103
}
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 (
113
134
& mut sb,
114
- & analyzer,
115
135
& mut bindings_shadow_count,
116
- InFile :: new ( file_id. into ( ) , token . clone ( ) ) ,
136
+ InFile :: new ( file_id. into ( ) , node . clone ( ) ) ,
117
137
) {
118
138
res. push ( HighlightedRange {
119
139
range : node. text_range ( ) ,
@@ -123,17 +143,12 @@ pub(crate) fn highlight_range(
123
143
}
124
144
}
125
145
}
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
+ }
136
147
WalkEvent :: Leave ( node) => {
148
+ if node. text_range ( ) . intersection ( & highlight_range) . is_none ( ) {
149
+ continue ;
150
+ }
151
+
137
152
if let Some ( m) = in_macro_call. as_ref ( ) {
138
153
if * m == node {
139
154
in_macro_call = None ;
@@ -284,7 +299,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
284
299
)
285
300
}
286
301
287
- let mut ranges = highlight ( db, file_id) ;
302
+ let mut ranges = highlight ( db, file_id, None ) ;
288
303
ranges. sort_by_key ( |it| it. range . start ( ) ) ;
289
304
// quick non-optimal heuristic to intersect token ranges and highlighted ranges
290
305
let mut frontier = 0 ;
@@ -509,10 +524,11 @@ fn bar() {
509
524
}"# ,
510
525
) ;
511
526
527
+ // The "x"
512
528
let highlights = & analysis
513
529
. highlight_range ( FileRange {
514
530
file_id,
515
- range : TextRange :: offset_len ( 82 . into ( ) , 1 . into ( ) ) , // "x"
531
+ range : TextRange :: offset_len ( 82 . into ( ) , 1 . into ( ) ) ,
516
532
} )
517
533
. unwrap ( ) ;
518
534
0 commit comments