@@ -9,6 +9,7 @@ use ra_ap_hir::Semantics;
99use ra_ap_ide_db:: line_index:: { LineCol , LineIndex } ;
1010use ra_ap_ide_db:: RootDatabase ;
1111use ra_ap_parser:: SyntaxKind ;
12+ use ra_ap_span:: { EditionedFileId , TextSize } ;
1213use ra_ap_syntax:: ast:: RangeItem ;
1314use ra_ap_syntax:: {
1415 ast, AstNode , NodeOrToken , SyntaxElementChildren , SyntaxError , SyntaxToken , TextRange ,
@@ -63,11 +64,13 @@ impl TextValue for ast::RangePat {
6364 self . op_token ( ) . map ( |x| x. text ( ) . to_string ( ) )
6465 }
6566}
67+
6668pub struct Translator < ' a > {
6769 pub trap : TrapFile ,
6870 path : & ' a str ,
6971 label : trap:: Label ,
7072 line_index : LineIndex ,
73+ file_id : Option < EditionedFileId > ,
7174 pub semi : Option < Semantics < ' a , RootDatabase > > ,
7275}
7376
@@ -77,13 +80,15 @@ impl<'a> Translator<'a> {
7780 path : & ' a str ,
7881 label : trap:: Label ,
7982 line_index : LineIndex ,
83+ file_id : Option < EditionedFileId > ,
8084 semi : Option < Semantics < ' a , RootDatabase > > ,
8185 ) -> Translator < ' a > {
8286 Translator {
8387 trap,
8488 path,
8589 label,
8690 line_index,
91+ file_id,
8792 semi,
8893 }
8994 }
@@ -107,18 +112,32 @@ impl<'a> Translator<'a> {
107112 ( start, end)
108113 }
109114
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 > {
111116 if let Some ( semi) = self . semi . as_ref ( ) {
112117 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+ }
114124 } else {
115- node. syntax ( ) . text_range ( )
125+ Some ( node. syntax ( ) . text_range ( ) )
116126 }
117127 }
118128 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+ }
122141 }
123142 pub fn emit_location_token ( & mut self , label : Label < generated:: Token > , token : & SyntaxToken ) {
124143 let ( start, end) = self . location ( token. text_range ( ) ) ;
@@ -248,7 +267,7 @@ impl<'a> Translator<'a> {
248267 mcall. path( ) . map( |p| p. to_string( ) ) . unwrap_or_default( ) ,
249268 kind, expand_to
250269 ) ,
251- range,
270+ range. unwrap_or_else ( || TextRange :: empty ( TextSize :: from ( 0 ) ) ) ,
252271 ) ) ;
253272 }
254273 } else {
@@ -259,7 +278,7 @@ impl<'a> Translator<'a> {
259278 "macro expansion failed: could not resolve macro '{}'" ,
260279 mcall. path( ) . map( |p| p. to_string( ) ) . unwrap_or_default( )
261280 ) ,
262- range,
281+ range. unwrap_or_else ( || TextRange :: empty ( TextSize :: from ( 0 ) ) ) ,
263282 ) ) ;
264283 }
265284 }
0 commit comments