@@ -9,6 +9,7 @@ use ra_ap_hir::Semantics;
9
9
use ra_ap_ide_db:: line_index:: { LineCol , LineIndex } ;
10
10
use ra_ap_ide_db:: RootDatabase ;
11
11
use ra_ap_parser:: SyntaxKind ;
12
+ use ra_ap_span:: { EditionedFileId , TextSize } ;
12
13
use ra_ap_syntax:: ast:: RangeItem ;
13
14
use ra_ap_syntax:: {
14
15
ast, AstNode , NodeOrToken , SyntaxElementChildren , SyntaxError , SyntaxToken , TextRange ,
@@ -63,11 +64,13 @@ impl TextValue for ast::RangePat {
63
64
self . op_token ( ) . map ( |x| x. text ( ) . to_string ( ) )
64
65
}
65
66
}
67
+
66
68
pub struct Translator < ' a > {
67
69
pub trap : TrapFile ,
68
70
path : & ' a str ,
69
71
label : trap:: Label ,
70
72
line_index : LineIndex ,
73
+ file_id : Option < EditionedFileId > ,
71
74
pub semi : Option < Semantics < ' a , RootDatabase > > ,
72
75
}
73
76
@@ -77,13 +80,15 @@ impl<'a> Translator<'a> {
77
80
path : & ' a str ,
78
81
label : trap:: Label ,
79
82
line_index : LineIndex ,
83
+ file_id : Option < EditionedFileId > ,
80
84
semi : Option < Semantics < ' a , RootDatabase > > ,
81
85
) -> Translator < ' a > {
82
86
Translator {
83
87
trap,
84
88
path,
85
89
label,
86
90
line_index,
91
+ file_id,
87
92
semi,
88
93
}
89
94
}
@@ -107,18 +112,32 @@ impl<'a> Translator<'a> {
107
112
( start, end)
108
113
}
109
114
110
- pub fn text_range_for_node ( & mut self , node : & impl ast:: AstNode ) -> TextRange {
115
+ pub fn text_range_for_node ( & mut self , node : & impl ast:: AstNode ) -> Option < TextRange > {
111
116
if let Some ( semi) = self . semi . as_ref ( ) {
112
117
let file_range = semi. original_range ( node. syntax ( ) ) ;
113
- file_range. range
118
+ let file_id = self . file_id ?;
119
+ if file_id == file_range. file_id {
120
+ Some ( file_range. range )
121
+ } else {
122
+ None
123
+ }
114
124
} else {
115
- node. syntax ( ) . text_range ( )
125
+ Some ( node. syntax ( ) . text_range ( ) )
116
126
}
117
127
}
118
128
pub fn emit_location < T : TrapClass > ( & mut self , label : Label < T > , node : & impl ast:: AstNode ) {
119
- let range = self . text_range_for_node ( node) ;
120
- let ( start, end) = self . location ( range) ;
121
- self . trap . emit_location ( self . label , label, start, end)
129
+ if let Some ( range) = self . text_range_for_node ( node) {
130
+ let ( start, end) = self . location ( range) ;
131
+ self . trap . emit_location ( self . label , label, start, end)
132
+ } else {
133
+ self . emit_diagnostic (
134
+ DiagnosticSeverity :: Info ,
135
+ "locations" . to_owned ( ) ,
136
+ "missing location for AstNode" . to_owned ( ) ,
137
+ "missing location for AstNode" . to_owned ( ) ,
138
+ ( LineCol { line : 0 , col : 0 } , LineCol { line : 0 , col : 0 } ) ,
139
+ ) ;
140
+ }
122
141
}
123
142
pub fn emit_location_token ( & mut self , label : Label < generated:: Token > , token : & SyntaxToken ) {
124
143
let ( start, end) = self . location ( token. text_range ( ) ) ;
@@ -248,7 +267,7 @@ impl<'a> Translator<'a> {
248
267
mcall. path( ) . map( |p| p. to_string( ) ) . unwrap_or_default( ) ,
249
268
kind, expand_to
250
269
) ,
251
- range,
270
+ range. unwrap_or_else ( || TextRange :: empty ( TextSize :: from ( 0 ) ) ) ,
252
271
) ) ;
253
272
}
254
273
} else {
@@ -259,7 +278,7 @@ impl<'a> Translator<'a> {
259
278
"macro expansion failed: could not resolve macro '{}'" ,
260
279
mcall. path( ) . map( |p| p. to_string( ) ) . unwrap_or_default( )
261
280
) ,
262
- range,
281
+ range. unwrap_or_else ( || TextRange :: empty ( TextSize :: from ( 0 ) ) ) ,
263
282
) ) ;
264
283
}
265
284
}
0 commit comments