@@ -8,7 +8,7 @@ use std::{
88
99use crate :: {
1010 ast:: ParsedSqlFile ,
11- comments:: Comments ,
11+ comments:: { Comments , LeadingCommentCapture } ,
1212 docs:: { SqlFileDoc , TableDoc } ,
1313 error:: DocError ,
1414 files:: SqlFiles ,
@@ -29,14 +29,17 @@ pub struct SqlDocBuilder<'a> {
2929 source : SqlFileDocSource < ' a > ,
3030 /// The list of Paths to be ignored for parsing purposes.
3131 deny : Vec < String > ,
32- /// Struct for tracking the settings for flattening multiline comments
32+ /// Tracks the chosen setting for flattening multiline comments
3333 multiline_flat : MultiFlatten ,
34+ /// Tracks the chosen setting for leading comment collection
35+ leading_type : LeadingCommentCapture ,
3436}
3537
3638/// Enum for multiline comment flattening.
37- #[ derive( Debug , Eq , PartialEq ) ]
39+ #[ derive( Debug , Default , Eq , PartialEq ) ]
3840pub enum MultiFlatten {
3941 /// Default option, retains multiline structure with `\n`
42+ #[ default]
4043 NoFlat ,
4144 /// Sets multiline comments to be flattened and combined without adding formatting
4245 FlattenWithNone ,
@@ -67,15 +70,17 @@ impl SqlDoc {
6770 SqlDocBuilder {
6871 source : SqlFileDocSource :: Dir ( root. as_ref ( ) . to_path_buf ( ) ) ,
6972 deny : Vec :: new ( ) ,
70- multiline_flat : MultiFlatten :: NoFlat ,
73+ multiline_flat : MultiFlatten :: default ( ) ,
74+ leading_type : LeadingCommentCapture :: default ( ) ,
7175 }
7276 }
7377 /// Method for generating builder from a [`Path`] of a single file
7478 pub fn from_path < P : AsRef < Path > + ?Sized > ( path : & P ) -> SqlDocBuilder < ' _ > {
7579 SqlDocBuilder {
7680 source : SqlFileDocSource :: File ( path. as_ref ( ) . to_path_buf ( ) ) ,
7781 deny : Vec :: new ( ) ,
78- multiline_flat : MultiFlatten :: NoFlat ,
82+ multiline_flat : MultiFlatten :: default ( ) ,
83+ leading_type : LeadingCommentCapture :: default ( ) ,
7984 }
8085 }
8186
@@ -86,29 +91,32 @@ impl SqlDoc {
8691 paths. iter ( ) . map ( |p| p. as_ref ( ) . to_path_buf ( ) ) . collect ( ) ,
8792 ) ,
8893 deny : Vec :: new ( ) ,
89- multiline_flat : MultiFlatten :: NoFlat ,
94+ multiline_flat : MultiFlatten :: default ( ) ,
95+ leading_type : LeadingCommentCapture :: default ( ) ,
9096 }
9197 }
9298
9399 /// Creates a builder from SQL text (no filesystem path is associated) from a [`str`]
94100 #[ must_use]
95- pub const fn builder_from_str ( content : & str ) -> SqlDocBuilder < ' _ > {
101+ pub fn builder_from_str ( content : & str ) -> SqlDocBuilder < ' _ > {
96102 SqlDocBuilder {
97103 source : SqlFileDocSource :: FromString ( content) ,
98104 deny : Vec :: new ( ) ,
99- multiline_flat : MultiFlatten :: NoFlat ,
105+ multiline_flat : MultiFlatten :: default ( ) ,
106+ leading_type : LeadingCommentCapture :: default ( ) ,
100107 }
101108 }
102109
103110 /// Creates a builder from a vec of tuples containing the `sql` as [`String`] and the path as [`PathBuf`]
104111 #[ must_use]
105- pub const fn builder_from_strs_with_paths (
112+ pub fn builder_from_strs_with_paths (
106113 string_with_path : & [ ( String , PathBuf ) ] ,
107114 ) -> SqlDocBuilder < ' _ > {
108115 SqlDocBuilder {
109116 source : SqlFileDocSource :: FromStringsWithPaths ( string_with_path) ,
110117 deny : Vec :: new ( ) ,
111- multiline_flat : MultiFlatten :: NoFlat ,
118+ multiline_flat : MultiFlatten :: default ( ) ,
119+ leading_type : LeadingCommentCapture :: default ( ) ,
112120 }
113121 }
114122
@@ -196,25 +204,47 @@ impl SqlDocBuilder<'_> {
196204 self
197205 }
198206
199- /// Method for flattening the multiline comments without additional formatting
207+ /// Flattens the multiline comments without additional formatting
200208 #[ must_use]
201209 pub fn flatten_multiline ( mut self ) -> Self {
202210 self . multiline_flat = MultiFlatten :: FlattenWithNone ;
203211 self
204212 }
205213
206- /// Method for flattening the multiline comments with [`String`] containing additional leading line formatting to add, such as punctuation.
214+ /// Flattens the multiline comments with [`String`] containing additional leading line formatting to add, such as punctuation.
207215 #[ must_use]
208216 pub fn flatten_multiline_with ( mut self , suffix : & str ) -> Self {
209217 self . multiline_flat = MultiFlatten :: Flatten ( suffix. to_owned ( ) ) ;
210218 self
211219 }
212- /// Method to set multiline comments to preserve multiple lines
220+ /// Preserves multiline comments line structure
213221 #[ must_use]
214222 pub fn preserve_multiline ( mut self ) -> Self {
215223 self . multiline_flat = MultiFlatten :: NoFlat ;
216224 self
217225 }
226+
227+ /// Collects only the comment on preceding lines
228+ #[ must_use]
229+ pub const fn collect_single_nearest ( mut self ) -> Self {
230+ self . leading_type = LeadingCommentCapture :: SingleNearest ;
231+ self
232+ }
233+
234+ /// Collects All valid comments on preceding lines
235+ #[ must_use]
236+ pub const fn collect_all_leading ( mut self ) -> Self {
237+ self . leading_type = LeadingCommentCapture :: AllLeading ;
238+ self
239+ }
240+
241+ /// Collects all single line comments at most one multiline comment
242+ #[ must_use]
243+ pub const fn collect_all_single_one_multi ( mut self ) -> Self {
244+ self . leading_type = LeadingCommentCapture :: AllSingleOneMulti ;
245+ self
246+ }
247+
218248 /// Builds the [`SqlDoc`]
219249 ///
220250 ///
@@ -313,15 +343,17 @@ fn generate_docs_from_file<P: AsRef<Path>>(source: P) -> Result<SqlFileDoc, DocE
313343 let file = SqlSource :: from_path ( source. as_ref ( ) ) ?;
314344 let parsed_file = ParsedSqlFile :: parse ( file) ?;
315345 let comments = Comments :: parse_all_comments_from_file ( & parsed_file) ?;
316- let docs = SqlFileDoc :: from_parsed_file ( & parsed_file, & comments) ?;
346+ let docs =
347+ SqlFileDoc :: from_parsed_file ( & parsed_file, & comments, LeadingCommentCapture :: default ( ) ) ?;
317348 Ok ( docs)
318349}
319350
320351fn generate_docs_str ( content : & str , path : Option < PathBuf > ) -> Result < SqlFileDoc , DocError > {
321352 let dummy_file = SqlSource :: from_str ( content. to_owned ( ) , path) ;
322353 let parsed_sql = ParsedSqlFile :: parse ( dummy_file) ?;
323354 let comments = Comments :: parse_all_comments_from_file ( & parsed_sql) ?;
324- let docs = SqlFileDoc :: from_parsed_file ( & parsed_sql, & comments) ?;
355+ let docs =
356+ SqlFileDoc :: from_parsed_file ( & parsed_sql, & comments, LeadingCommentCapture :: default ( ) ) ?;
325357 Ok ( docs)
326358}
327359
@@ -346,6 +378,7 @@ mod tests {
346378
347379 use crate :: {
348380 SqlDoc ,
381+ comments:: LeadingCommentCapture ,
349382 docs:: { ColumnDoc , TableDoc } ,
350383 error:: DocError ,
351384 sql_doc:: { MultiFlatten , SqlDocBuilder } ,
@@ -568,7 +601,8 @@ mod tests {
568601 let expected_builder = SqlDocBuilder {
569602 source : crate :: sql_doc:: SqlFileDocSource :: File ( PathBuf :: from ( "path" ) ) ,
570603 deny : vec ! [ "path1" . to_owned( ) , "path2" . to_owned( ) ] ,
571- multiline_flat : MultiFlatten :: NoFlat ,
604+ multiline_flat : MultiFlatten :: default ( ) ,
605+ leading_type : LeadingCommentCapture :: default ( ) ,
572606 } ;
573607 assert_eq ! ( actual_builder, expected_builder) ;
574608 }
@@ -744,7 +778,8 @@ mod tests {
744778 let expected = SqlDocBuilder {
745779 source : crate :: sql_doc:: SqlFileDocSource :: FromString ( content) ,
746780 deny : vec ! [ ] ,
747- multiline_flat : MultiFlatten :: NoFlat ,
781+ multiline_flat : MultiFlatten :: default ( ) ,
782+ leading_type : LeadingCommentCapture :: default ( ) ,
748783 } ;
749784
750785 assert_eq ! ( actual, expected) ;
0 commit comments