@@ -229,17 +229,15 @@ impl Comments {
229229 /// - Will return [`CommentError::UnmatchedBlockCommentStart`] if a comment does not have an opening `/*`
230230 pub fn parse_all_comments_from_file ( file : & ParsedSqlFile ) -> CommentResult < Self > {
231231 let src = file. content ( ) ;
232- let mut comments = Vec :: new ( ) ;
233- let mut current_location = Location :: default ( ) ;
234-
235- Ok ( Self { comments } )
232+ let comments = Self :: scan_comments ( src) ?;
233+ Ok ( comments)
236234 }
237235
238236 /// Scans the raw file and collects all comments
239237 ///
240238 /// # Parameters
241239 /// - `src` which is the `SQL` file content as a [`str`]
242- fn scan_comments ( src : & str ) -> CommentResult < Self > {
240+ pub fn scan_comments ( src : & str ) -> CommentResult < Self > {
243241 let mut comments = Vec :: new ( ) ;
244242 let mut current_line = 0u64 ;
245243 let mut current_column = 0u64 ;
@@ -250,46 +248,62 @@ impl Comments {
250248 let mut single_line = String :: new ( ) ;
251249 let mut multi_line = String :: new ( ) ;
252250
253- let mut current_comment = String :: new ( ) ;
254- //let mut src_state_machine = src.chars().peekable();
255- let mut lines = src. lines ( ) . into_iter ( ) ;
256- for line in lines {
257- for ( i, c) in line. chars ( ) . into_iter ( ) . enumerate ( ) {
251+ let mut src_state_machine = src. chars ( ) . peekable ( ) ;
252+ while !src_state_machine. peek ( ) . is_none ( ) {
253+ if let Some ( c) = src_state_machine. next ( ) {
258254 match c {
259255 '-' => {
260- match i {
261- 0 => {
262- single_line. push ( c) ;
263- start_column = i as u64 ;
264- } ,
265- 1 => {
266- match single_line. is_empty ( ) {
267- false => {
268- if single_line. len ( ) == 1 {
269- single_line. push ( c) ;
270- }
271- } ,
272- _ => { }
273- }
274- }
256+ if single_line. is_empty ( ) {
257+ single_line. push ( c) ;
258+ start_column = current_column;
259+ start_line = current_line;
260+ } else if single_line. chars ( ) . last ( ) == Some ( '-' ) {
261+ single_line. push ( c) ;
262+ }
263+ } ,
264+ '/' => {
265+ if multi_line. is_empty ( ) {
266+ multi_line. push ( c) ;
267+ start_column = current_column;
268+ start_line = current_line;
269+ } else if multi_line. chars ( ) . last ( ) == Some ( '*' ) {
270+ multi_line. push ( c) ;
271+ comments. push ( Comment { kind : CommentKind :: MultiLine ( multi_line. clone ( ) ) , span : Span { start : Location { line : start_line, column : start_column } , end : Location { line : current_line, column : current_column+1 } } } ) ;
272+ multi_line. clear ( ) ;
273+ }
274+ } ,
275+ '*' => {
276+ if !multi_line. is_empty ( ) {
277+ multi_line. push ( c) ;
278+ }
279+ } ,
280+ '\n' => {
281+ if !single_line. is_empty ( ) {
282+ comments. push ( Comment { kind : CommentKind :: SingleLine ( single_line. clone ( ) ) , span : Span { start : Location { line : start_line, column : start_column+1 } , end : Location { line : current_line, column : current_column } } } ) ;
283+ single_line. clear ( ) ;
284+ } else if !multi_line. is_empty ( ) {
285+ multi_line. push ( c) ;
275286 }
276287 } ,
277- '/' => ,
278- '*' => ,
279288 _ => {
280289 if !single_line. is_empty ( ) {
281290 single_line. push ( c) ;
291+ } else if !multi_line. is_empty ( ) {
292+ multi_line. push ( c) ;
282293 }
283294 }
284295 }
296+ if c == '\n' {
297+ current_column = 0 ;
298+ current_line += 1 ;
299+ } else {
300+ current_column += 1 ;
301+ }
285302 }
286-
287- start_line += 1 ;
288303 }
289304
290305 Ok ( Self { comments } )
291306 }
292-
293307 /// Parse single line comments
294308
295309 /// Parse multi line comments
0 commit comments