Skip to content

Commit 162ff35

Browse files
committed
Added leading collection types to `SqlDocBuilder
1 parent 4ffa5b2 commit 162ff35

3 files changed

Lines changed: 67 additions & 22 deletions

File tree

src/comments.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl Comments {
387387
Self::new(comments)
388388
}
389389

390-
/// Collapse this collection separate with `\n` into a single a single comment.
390+
/// Collapse this collection of comments and separate each comment with `\n` as a single [`Comment`].
391391
#[must_use]
392392
pub fn collapse_comments(self) -> Option<Comment> {
393393
let mut iter = self.comments.into_iter();

src/docs.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use std::path::{Path, PathBuf};
55

66
use sqlparser::ast::{Ident, ObjectName, ObjectNamePart, Spanned, Statement};
77

8-
use crate::{ast::ParsedSqlFile, comments::Comments, error::DocError};
8+
use crate::{
9+
ast::ParsedSqlFile,
10+
comments::{Comments, LeadingCommentCapture},
11+
error::DocError,
12+
};
913

1014
/// Structure for containing the `name` of the `Column` and an [`Option`] for
1115
/// the comment as a [`String`]
@@ -214,7 +218,11 @@ impl SqlFileDoc {
214218
/// # Errors
215219
/// - Returns [`DocError::InvalidObjectName`] if the table name has no identifier components.
216220
/// - May also propagate other [`DocError`] variants from lower layers in the future.
217-
pub fn from_parsed_file(file: &ParsedSqlFile, comments: &Comments) -> Result<Self, DocError> {
221+
pub fn from_parsed_file(
222+
file: &ParsedSqlFile,
223+
comments: &Comments,
224+
capture: LeadingCommentCapture,
225+
) -> Result<Self, DocError> {
218226
let mut tables = Vec::new();
219227
for statement in file.statements() {
220228
#[allow(clippy::single_match)]
@@ -224,7 +232,8 @@ impl SqlFileDoc {
224232
let mut column_docs = Vec::new();
225233
for column in &table.columns {
226234
let column_start = column.span().start.line;
227-
let column_leading = comments.leading_comment(column_start);
235+
let column_leading =
236+
comments.leading_comments(column_start, capture).collapse_comments();
228237
let column_name = column.name.value.clone();
229238
let column_doc = match column_leading {
230239
Some(col_comment) => {
@@ -491,10 +500,11 @@ CREATE TABLE posts (
491500
let set = SqlSource::sql_sources(&base, &[])?;
492501
let parsed_set = ParsedSqlFileSet::parse_all(set)?;
493502
let expected_values = expect_values();
503+
let capture = crate::comments::LeadingCommentCapture::default();
494504

495505
for file in parsed_set.files() {
496506
let comments = Comments::parse_all_comments_from_file(file)?;
497-
let docs = SqlFileDoc::from_parsed_file(file, &comments);
507+
let docs = SqlFileDoc::from_parsed_file(file, &comments, capture);
498508
let filename = file
499509
.file()
500510
.path()

src/sql_doc.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88

99
use 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)]
3840
pub 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

320351
fn 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

Comments
 (0)