11use anyhow:: { Context , Result } ;
22use colored:: Colorize ;
3- use serde:: { Deserialize , Serialize } ;
43use std:: fs;
54use std:: path:: Path ;
65
76use crate :: rule:: { CheckResult , ConvertResult , Rule } ;
87use crate :: utils:: file_io:: { read_file, write_file} ;
98
10- #[ derive( Debug , Serialize , Deserialize ) ]
11- struct ErrorLocation {
12- row : usize ,
13- column : usize ,
14- byte_offset : usize ,
15- size : usize ,
16- }
17-
18- #[ derive( Debug , Serialize , Deserialize ) ]
19- struct ParseError {
20- filename : String ,
21- title : String ,
22- message : String ,
23- location : ErrorLocation ,
24- }
25-
269pub struct DivWhitespaceConverter { }
2710
2811impl DivWhitespaceConverter {
2912 pub fn new ( ) -> Result < Self > {
3013 Ok ( Self { } )
3114 }
3215
33- /// Parse a file and get error locations as JSON
34- fn get_parse_errors ( & self , file_path : & Path ) -> Result < Vec < ParseError > > {
16+ /// Parse a file and get diagnostic messages
17+ fn get_parse_errors ( & self , file_path : & Path ) -> Result < Vec < quarto_error_reporting :: DiagnosticMessage > > {
3518 let content = fs:: read_to_string ( file_path)
3619 . with_context ( || format ! ( "Failed to read file: {}" , file_path. display( ) ) ) ?;
3720
@@ -48,31 +31,15 @@ impl DivWhitespaceConverter {
4831
4932 match result {
5033 Ok ( _) => Ok ( Vec :: new ( ) ) , // No errors
51- Err ( error_messages) => {
52- // Parse the JSON error output
53- // The error messages come as a single JSON array string
54- if error_messages. is_empty ( ) {
55- return Ok ( Vec :: new ( ) ) ;
56- }
57-
58- let json_str = error_messages. join ( "" ) ;
59-
60- // Try to parse as JSON array
61- match serde_json:: from_str :: < Vec < ParseError > > ( & json_str) {
62- Ok ( errors) => Ok ( errors) ,
63- Err ( _) => {
64- // If parsing fails, the messages are likely plain text warnings/debug messages
65- // rather than actual syntax errors. These don't indicate div whitespace issues,
66- // so we can safely ignore them for this specific rule.
67- Ok ( Vec :: new ( ) )
68- }
69- }
34+ Err ( diagnostics) => {
35+ // Return diagnostic messages directly
36+ Ok ( diagnostics)
7037 }
7138 }
7239 }
7340
7441 /// Find div fence errors that need whitespace fixes
75- fn find_div_whitespace_errors ( & self , content : & str , errors : & [ ParseError ] ) -> Vec < usize > {
42+ fn find_div_whitespace_errors ( & self , content : & str , errors : & [ quarto_error_reporting :: DiagnosticMessage ] ) -> Vec < usize > {
7643 let mut fix_positions = Vec :: new ( ) ;
7744 let lines: Vec < & str > = content. lines ( ) . collect ( ) ;
7845
@@ -85,12 +52,18 @@ impl DivWhitespaceConverter {
8552 continue ;
8653 }
8754
55+ // Extract row from location (if available)
56+ // SourceInfo uses 0-indexed rows, div_whitespace uses them too
57+ let error_row = error. location . as_ref ( )
58+ . map ( |loc| loc. range . start . row )
59+ . unwrap_or ( 0 ) ;
60+
8861 // The error might be on the line itself or the line before (for div fences)
8962 // Check both the current line and the previous line
90- let lines_to_check = if error . location . row > 0 {
91- vec ! [ error . location . row - 1 , error . location . row ]
63+ let lines_to_check = if error_row > 0 {
64+ vec ! [ error_row - 1 , error_row ]
9265 } else {
93- vec ! [ error . location . row ]
66+ vec ! [ error_row ]
9467 } ;
9568
9669 for & line_idx in & lines_to_check {
0 commit comments