Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions crates/squawk_fmt/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rowan::Direction;
use squawk_line_index::find_newline;
use squawk_line_index::{UniversalNewlines, find_newline};
use squawk_syntax::{SyntaxElement, SyntaxKind, SyntaxToken};
use tiny_pretty::Doc;

Expand All @@ -8,7 +8,26 @@ pub(crate) fn is_line_comment(token: &SyntaxToken) -> bool {
}

pub(crate) fn build_comment<'a>(token: &SyntaxToken) -> Doc<'a> {
let line = |text: &str| Doc::text(text.trim_end_matches([' ', '\t']).to_string());
let align_stars = !is_line_comment(token)
&& find_newline(token.text()).is_some()
&& token
.text()
.universal_newlines()
.skip(1)
.all(|line| line.as_str().trim_start().starts_with('*'));
let line = |text: &str| {
let text = text.trim_end_matches([' ', '\t']);
if is_line_comment(token) {
if let Some(content) = text.strip_prefix("--") {
if !content.is_empty() && !content.starts_with(' ') && !content.starts_with('\t') {
return Doc::text(format!("-- {content}"));
}
}
} else if align_stars && text.trim_start().starts_with('*') {
return Doc::text(format!(" {}", text.trim()));
}
Doc::text(text.to_string())
};
let mut docs = vec![];
let mut text = token.text();
while let Some((position, line_ending)) = find_newline(text) {
Expand Down
38 changes: 33 additions & 5 deletions crates/squawk_fmt/src/token_compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,46 @@ fn meaningful_tokens(text: &str) -> Vec<(TokenKind, &str)> {
tokens
}

fn line_comments_equivalent(before: &str, after: &str) -> bool {
let before = before.trim_end_matches([' ', '\t']);
let after = after.trim_end_matches([' ', '\t']);
if before == after {
return true;
}

let Some(content) = before.strip_prefix("--") else {
return false;
};
if content.is_empty() || content.starts_with(' ') || content.starts_with('\t') {
return false;
}

after.strip_prefix("-- ") == Some(content)
}

fn tokens_equivalent(before: (TokenKind, &str), after: (TokenKind, &str)) -> bool {
let (before_kind, before_text) = before;
let (after_kind, after_text) = after;

if before_kind == after_kind {
if matches!(
before_kind,
TokenKind::LineComment | TokenKind::BlockComment { .. }
) {
if before_kind == TokenKind::LineComment {
return line_comments_equivalent(before_text, after_text);
}
if matches!(before_kind, TokenKind::BlockComment { .. }) {
let normalize = |text: &str| {
let align_stars = text
.universal_newlines()
.skip(1)
.all(|line| line.as_str().trim_start().starts_with('*'));
text.universal_newlines()
.map(|line| line.as_str().trim_end_matches([' ', '\t']))
.map(|line| {
let line = line.as_str().trim_end_matches([' ', '\t']);
if align_stars && line.trim_start().starts_with('*') {
format!(" {}", line.trim())
} else {
line.to_string()
}
})
.collect::<Vec<_>>()
.join("\n")
};
Expand Down
22 changes: 22 additions & 0 deletions crates/squawk_fmt/tests/after/comments.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,25 @@ input_file: crates/squawk_fmt/tests/before/comments.sql
-- we have a big gap here, but we should only output a couple lines at most

/* end */

/*
*
*
*
*/
select 1;

/*
*

*
*/
select 1;

/*
* text
*/
select 1;

/*foo*/
select 1;
24 changes: 23 additions & 1 deletion crates/squawk_fmt/tests/before/comments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
*/

-- intentional new line follows, we should keep that
--intentional new line follows, we should keep that

/* bar */
/* followed by another comment */
Expand All @@ -25,3 +25,25 @@


/* end */

/*
*
*
*
*/
select 1;

/*
*

*
*/
select 1;

/*
* text
*/
select 1;

/*foo*/
select 1;
Loading