Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle doc-like comments when forcing a leading space #25

Merged
merged 1 commit into from
Jan 4, 2025
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
27 changes: 24 additions & 3 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,11 @@ fn gen_comment<'a>(comment: SyntaxToken, context: &mut Context<'a>) -> PrintItem
items.extend({
if context.config.comment_force_leading_space {
let info = get_comment_text_info(comment.text());
let after_hash_text = &comment.text()[info.leading_hashes_count..].trim_end();
let after_hash_text = &comment.text()[info.start_text_index..].trim_end();
let mut text = "#".repeat(info.leading_hashes_count);
if info.has_exclamation_point {
text.push('!');
}
if !after_hash_text.is_empty() {
if !info.has_leading_whitespace {
text.push(' ');
Expand All @@ -416,15 +419,31 @@ fn gen_comment<'a>(comment: SyntaxToken, context: &mut Context<'a>) -> PrintItem

struct CommentTextInfo {
pub has_leading_whitespace: bool,
pub has_exclamation_point: bool,
pub leading_hashes_count: usize,
pub start_text_index: usize,
}

fn get_comment_text_info(text: &str) -> CommentTextInfo {
let mut leading_hashes_count = 0;
let mut has_leading_whitespace = false;
for c in text.chars() {
let mut has_exclamation_point = false;
let mut start_text_index = 0;
let mut chars = text.char_indices();
for (index, c) in chars.by_ref() {
match c {
'#' => leading_hashes_count += 1,
'#' if !has_exclamation_point => {
leading_hashes_count += 1;
start_text_index = index + 1;
}
'!' if leading_hashes_count == 1 => {
has_exclamation_point = true;
start_text_index = index + 1;
if matches!(chars.next(), Some((_, ' ' | '\t'))) {
has_leading_whitespace = true;
}
break;
}
' ' | '\t' => {
has_leading_whitespace = true;
break;
Expand All @@ -434,7 +453,9 @@ fn get_comment_text_info(text: &str) -> CommentTextInfo {
}
CommentTextInfo {
leading_hashes_count,
has_exclamation_point,
has_leading_whitespace,
start_text_index,
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/specs/Comments/Comments_ForceLeadingSpace_False.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@

[expect]
# test

== should not force when is #! ==
#! Test
#!Test
#!## Test

[expect]
#! Test
#!Test
#!## Test
10 changes: 10 additions & 0 deletions tests/specs/Comments/Comments_ForceLeadingSpace_True.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@
# test
# test
# test

== should force when is #! ==
#! Test
#!Test
#!## Test

[expect]
#! Test
#! Test
#! ## Test
Loading