@@ -139,9 +139,21 @@ impl<'a> Translator<'a> {
139
139
) ;
140
140
}
141
141
}
142
- pub fn emit_location_token ( & mut self , label : Label < generated:: Token > , token : & SyntaxToken ) {
143
- let ( start, end) = self . location ( token. text_range ( ) ) ;
144
- self . trap . emit_location ( self . label , label, start, end)
142
+ pub fn emit_location_token (
143
+ & mut self ,
144
+ label : Label < generated:: Token > ,
145
+ parent : & impl ast:: AstNode ,
146
+ token : & SyntaxToken ,
147
+ ) {
148
+ let parent_range = parent. syntax ( ) . text_range ( ) ;
149
+ let token_range = token. text_range ( ) ;
150
+ if let Some ( clipped_range) = token_range. intersect ( parent_range) {
151
+ if let Some ( parent_range2) = self . text_range_for_node ( parent) {
152
+ let token_range = clipped_range + parent_range2. start ( ) - parent_range. start ( ) ;
153
+ let ( start, end) = self . location ( token_range) ;
154
+ self . trap . emit_location ( self . label , label, start, end)
155
+ }
156
+ }
145
157
}
146
158
pub fn emit_diagnostic (
147
159
& mut self ,
@@ -175,31 +187,41 @@ impl<'a> Translator<'a> {
175
187
location,
176
188
) ;
177
189
}
178
- pub fn emit_parse_error ( & mut self , err : & SyntaxError ) {
179
- let location = self . location ( err. range ( ) ) ;
180
- let message = err. to_string ( ) ;
181
- self . emit_diagnostic (
182
- DiagnosticSeverity :: Warning ,
183
- "parse_error" . to_owned ( ) ,
184
- message. clone ( ) ,
185
- message,
186
- location,
187
- ) ;
190
+ pub fn emit_parse_error ( & mut self , owner : & impl ast:: AstNode , err : & SyntaxError ) {
191
+ let owner_range: TextRange = owner. syntax ( ) . text_range ( ) ;
192
+ let err_range = err. range ( ) ;
193
+ if let Some ( owner_range2) = self . text_range_for_node ( owner) {
194
+ let location = if let Some ( clipped_range) = err_range. intersect ( owner_range) {
195
+ let err_range = clipped_range + owner_range2. start ( ) - owner_range. start ( ) ;
196
+ self . location ( err_range)
197
+ } else {
198
+ self . location ( owner_range2)
199
+ } ;
200
+ let message = err. to_string ( ) ;
201
+ self . emit_diagnostic (
202
+ DiagnosticSeverity :: Warning ,
203
+ "parse_error" . to_owned ( ) ,
204
+ message. clone ( ) ,
205
+ message,
206
+ location,
207
+ ) ;
208
+ }
188
209
}
189
210
pub fn emit_tokens (
190
211
& mut self ,
191
- parent : Label < generated:: AstNode > ,
212
+ parent_node : & impl ast:: AstNode ,
213
+ parent_label : Label < generated:: AstNode > ,
192
214
children : SyntaxElementChildren ,
193
215
) {
194
216
for child in children {
195
217
if let NodeOrToken :: Token ( token) = child {
196
218
if token. kind ( ) == SyntaxKind :: COMMENT {
197
219
let label = self . trap . emit ( generated:: Comment {
198
220
id : TrapId :: Star ,
199
- parent,
221
+ parent : parent_label ,
200
222
text : token. text ( ) . to_owned ( ) ,
201
223
} ) ;
202
- self . emit_location_token ( label. into ( ) , & token) ;
224
+ self . emit_location_token ( label. into ( ) , parent_node , & token) ;
203
225
}
204
226
}
205
227
}
@@ -230,11 +252,11 @@ impl<'a> Translator<'a> {
230
252
. get_erased ( err. span ( ) . anchor . ast_id )
231
253
. text_range ( )
232
254
. start ( ) ;
233
- self . emit_parse_error ( & SyntaxError :: new ( message, location) ) ;
255
+ self . emit_parse_error ( mcall , & SyntaxError :: new ( message, location) ) ;
234
256
} ;
235
257
}
236
258
for err in value. value . iter ( ) {
237
- self . emit_parse_error ( err) ;
259
+ self . emit_parse_error ( mcall , err) ;
238
260
}
239
261
}
240
262
let expand_to = ra_ap_hir_expand:: ExpandTo :: from_call_site ( mcall) ;
@@ -261,7 +283,7 @@ impl<'a> Translator<'a> {
261
283
MacroCall :: emit_expanded ( label, value, & mut self . trap . writer ) ;
262
284
} else {
263
285
let range = self . text_range_for_node ( mcall) ;
264
- self . emit_parse_error ( & SyntaxError :: new (
286
+ self . emit_parse_error ( mcall , & SyntaxError :: new (
265
287
format ! (
266
288
"macro expansion failed: the macro '{}' expands to {:?} but a {:?} was expected" ,
267
289
mcall. path( ) . map( |p| p. to_string( ) ) . unwrap_or_default( ) ,
@@ -273,13 +295,16 @@ impl<'a> Translator<'a> {
273
295
} else {
274
296
let range = self . text_range_for_node ( mcall) ;
275
297
276
- self . emit_parse_error ( & SyntaxError :: new (
277
- format ! (
278
- "macro expansion failed: could not resolve macro '{}'" ,
279
- mcall. path( ) . map( |p| p. to_string( ) ) . unwrap_or_default( )
298
+ self . emit_parse_error (
299
+ mcall,
300
+ & SyntaxError :: new (
301
+ format ! (
302
+ "macro expansion failed: could not resolve macro '{}'" ,
303
+ mcall. path( ) . map( |p| p. to_string( ) ) . unwrap_or_default( )
304
+ ) ,
305
+ range. unwrap_or_else ( || TextRange :: empty ( TextSize :: from ( 0 ) ) ) ,
280
306
) ,
281
- range. unwrap_or_else ( || TextRange :: empty ( TextSize :: from ( 0 ) ) ) ,
282
- ) ) ;
307
+ ) ;
283
308
}
284
309
}
285
310
}
0 commit comments