@@ -25,6 +25,7 @@ pub struct ParseError {
2525 pub error : parser:: ParseErrorType ,
2626 pub raw_location : ruff_text_size:: TextRange ,
2727 pub location : SourceLocation ,
28+ pub end_location : SourceLocation ,
2829 pub source_path : String ,
2930}
3031
@@ -44,13 +45,26 @@ pub enum CompileError {
4445
4546impl CompileError {
4647 pub fn from_ruff_parse_error ( error : parser:: ParseError , source_file : & SourceFile ) -> Self {
47- let location = source_file
48- . to_source_code ( )
49- . source_location ( error. location . start ( ) , PositionEncoding :: Utf8 ) ;
48+ let source_code = source_file. to_source_code ( ) ;
49+ let location = source_code. source_location ( error. location . start ( ) , PositionEncoding :: Utf8 ) ;
50+ let mut end_location =
51+ source_code. source_location ( error. location . end ( ) , PositionEncoding :: Utf8 ) ;
52+
53+ // If the error range ends at the start of a new line (column 1),
54+ // adjust it to the end of the previous line
55+ if end_location. character_offset . get ( ) == 1 && end_location. line > location. line {
56+ // Get the end of the previous line
57+ let prev_line_end = error. location . end ( ) - ruff_text_size:: TextSize :: from ( 1 ) ;
58+ end_location = source_code. source_location ( prev_line_end, PositionEncoding :: Utf8 ) ;
59+ // Adjust column to be after the last character
60+ end_location. character_offset = end_location. character_offset . saturating_add ( 1 ) ;
61+ }
62+
5063 Self :: Parse ( ParseError {
5164 error : error. error ,
5265 raw_location : error. location ,
5366 location,
67+ end_location,
5468 source_path : source_file. name ( ) . to_owned ( ) ,
5569 } )
5670 }
@@ -70,6 +84,16 @@ impl CompileError {
7084 }
7185 }
7286
87+ pub fn python_end_location ( & self ) -> Option < ( usize , usize ) > {
88+ match self {
89+ CompileError :: Codegen ( _) => None ,
90+ CompileError :: Parse ( parse_error) => Some ( (
91+ parse_error. end_location . line . get ( ) ,
92+ parse_error. end_location . character_offset . get ( ) ,
93+ ) ) ,
94+ }
95+ }
96+
7397 pub fn source_path ( & self ) -> & str {
7498 match self {
7599 Self :: Codegen ( codegen_error) => & codegen_error. source_path ,
0 commit comments