3535
3636use codespan_reporting:: diagnostic:: { Diagnostic , Label } ;
3737use indoc:: indoc;
38- use tree_sitter:: { Range , Tree } ;
38+ use tree_sitter:: Range ;
3939use unicode_width:: UnicodeWidthStr ;
4040
41- use crate :: {
42- helpers:: { LinesWithPosition , QueryHelper } ,
43- rules:: api:: Rule ,
44- } ;
41+ use crate :: { helpers:: QueryHelper , rules:: api:: Rule } ;
42+
43+ use crate :: rules:: api:: SourceInfo ;
4544
4645/// Amount that wrapped lines must be indented, in columns.
4746const WRAPPED_LINE_INDENT_WIDTH : usize = 2 ;
@@ -106,11 +105,11 @@ const QUERY_STR: &str = indoc! { /* query */ r##"
106105"## } ;
107106
108107impl Rule for Rule02a {
109- fn check ( & self , tree : & Tree , code : & str ) -> Vec < Diagnostic < ( ) > > {
108+ fn check ( & self , SourceInfo { tree , code, lines } : & SourceInfo ) -> Vec < Diagnostic < ( ) > > {
110109 let mut diagnostics = Vec :: new ( ) ;
111110
112111 // Check for lines >80 columns long
113- for ( line, index) in LinesWithPosition :: from ( code ) {
112+ for ( line, index) in lines {
114113 let width = line_width ( line) ;
115114 if width > 80 {
116115 let diagnostic = Diagnostic :: warning ( )
@@ -156,15 +155,15 @@ impl Rule for Rule02a {
156155 }
157156
158157 // Check indentation of wrapped lines and construct list of labels
159- let mut code_lines = LinesWithPosition :: from ( code )
158+ let mut code_lines = lines . iter ( )
160159 . skip ( range. start_point . row )
161160 . take ( range. end_point . row + 1 - range. start_point . row ) ;
162- let ( first_line, first_line_byte_pos) = code_lines. next ( ) . unwrap ( ) ;
161+ let & ( first_line, first_line_byte_pos) = code_lines. next ( ) . unwrap ( ) ;
163162 let first_line_indent = get_indentation ( first_line) ;
164163 let first_line_indent_width = line_width ( first_line_indent) ;
165164 let expected_indent_width = first_line_indent_width + WRAPPED_LINE_INDENT_WIDTH ;
166165 let mut labels = Vec :: new ( ) ;
167- for ( this_line, this_line_pos) in & mut code_lines {
166+ for & ( this_line, this_line_pos) in & mut code_lines {
168167 let this_line_indent = get_indentation ( this_line) ;
169168 let this_line_indent_width = line_width ( this_line_indent) ;
170169 if this_line_indent_width < expected_indent_width {
@@ -224,9 +223,11 @@ mod tests {
224223
225224 use indoc:: indoc;
226225 use pretty_assertions:: assert_eq;
227- use tree_sitter:: Parser ;
228226
229- use crate :: { helpers:: testing:: test_captures, rules:: api:: Rule } ;
227+ use crate :: {
228+ helpers:: testing:: test_captures,
229+ rules:: api:: { Rule , SourceInfo } ,
230+ } ;
230231
231232 use super :: { Rule02a , QUERY_STR } ;
232233
@@ -318,8 +319,6 @@ mod tests {
318319 #[ test]
319320 fn test_rule02a_diagnostics ( ) {
320321 let rule = Rule02a { } ;
321- let mut parser = Parser :: new ( ) ;
322- parser. set_language ( & tree_sitter_c:: LANGUAGE . into ( ) ) . unwrap ( ) ;
323322
324323 macro_rules! test {
325324 ( $code: literal, $ndiag: expr, $nlabels_list: expr) => {
@@ -333,8 +332,8 @@ mod tests {
333332 }
334333 code. push_str( "}\n " ) ;
335334 dbg!( & code) ;
336- let tree = parser . parse ( code. as_bytes ( ) , None ) . unwrap ( ) ;
337- let diagnostics = rule. check( & tree , & code ) ;
335+ let source = SourceInfo :: new ( & code) ;
336+ let diagnostics = rule. check( & source ) ;
338337 assert_eq!( $ndiag, diagnostics. len( ) ) ;
339338 let nlabels_list: & [ usize ] = & $nlabels_list;
340339 assert_eq!(
0 commit comments