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
4 changes: 2 additions & 2 deletions fixtures/small/class_comment_expected.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#adsf
# adsf
class Foo
#but this one doesn't get a proceeding newline
# but this one doesn't get a proceeding newline
a
end

Expand Down
40 changes: 40 additions & 0 deletions fixtures/small/comment_at_least_one_leading_space_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#! /usr/bin/env ruby
# frozen_string_literal: true

#### #### AUTHOR: coder204coolgizmos ### ##

##DATE: 2026-06-27
## Cool Gizmos, Inc.

#### ####
###----------
#: asdf
#| asdf
#** doc doc
#
#
#This is a class that frobs a gizmo. It:
# * frobs the sprog
# * then turns the sprog into the gizmo.
#### #
#### #
#Keep this class here.
class Foo
#inner class comment

#method description comment
def self.b(&_blk)
yield
end

#method description comment
def self.a
#some method comment

x = b do
#some comment inside the block
y = 4
# some other comment
end
end
end
40 changes: 40 additions & 0 deletions fixtures/small/comment_at_least_one_leading_space_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#! /usr/bin/env ruby
# frozen_string_literal: true

#### #### AUTHOR: coder204coolgizmos ### ##

## DATE: 2026-06-27
## Cool Gizmos, Inc.

#### ####
###----------
#: asdf
#| asdf
#** doc doc
#
#
# This is a class that frobs a gizmo. It:
# * frobs the sprog
# * then turns the sprog into the gizmo.
#### #
#### #
# Keep this class here.
class Foo
# inner class comment

# method description comment
def self.b(&_blk)
yield
end

# method description comment
def self.a
# some method comment

x = b do
# some comment inside the block
y = 4
# some other comment
end
end
end
8 changes: 4 additions & 4 deletions fixtures/small/empty_comments_expected.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
def foo
#a
#b
#c
#d
# a
# b
# c
# d
end

if a
Expand Down
4 changes: 2 additions & 2 deletions fixtures/small/inline_comments_expected.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ module MathsAndPhysics
# degree_codes: The corresponding JAC codes to the subject(s)
# the teacher completed their degree in.
CONST = [
#maths
# maths
a,
#physics
# physics
b,

# foreign language
Expand Down
8 changes: 4 additions & 4 deletions fixtures/small/rspec_its_expected.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
"a" \
"b"
) do
#hi
# hi
end

it "a" do
#hi
# hi
end

it "a", flag: true do
#hi
# hi
end

it "a", flag: true, other: "b", another: false do
#hi
# hi
end

it("a", flag: true, other: "b", another: false) { 1 }
Expand Down
2 changes: 1 addition & 1 deletion fixtures/small/start_of_file_comments_expected.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env ruby
#frozen_string_literal: true
# frozen_string_literal: true

def foo
a
Expand Down
2 changes: 1 addition & 1 deletion fixtures/small/word_array_trailing_comment_expected.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#c
# c
a = %w[
b
]
Expand Down
46 changes: 46 additions & 0 deletions librubyfmt/src/comment_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ pub struct CommentBlock {
comments: Vec<Cow<'static, [u8]>>,
}

const SPECIAL_CHARS_AFTER_HASH_TO_IGNORE: [u8; 6] = [
b'!', // #! 'shebang' comments cannot be modified since they are Unix directives.
b'=', // #=== is a common delimiting pattern.
b'-', // #--- is a common delimiting pattern.
b':', // #: is used for RBS annotations.
b'|', // #| is used for RBS annotations.
b'*', // #** is used for doxygen comments.
];

impl CommentBlock {
pub fn new(span: Range<LineNumber>, comments: Vec<Cow<'static, [u8]>>) -> Self {
CommentBlock { span, comments }
Expand Down Expand Up @@ -54,6 +63,43 @@ impl CommentBlock {
})
}

pub fn enforce_at_least_one_space_after_comment_symbol(mut self) -> Self {
for comment in &mut self.comments {
// Ignore empty vecs -- these represent blank lines between
// groups of comments
if comment.is_empty() || comment.starts_with(b"=begin") {
continue;
}
if let Some(start_idx) = comment.iter().position(|&c| c == b'#') {
// Allow any amount of '#' after the leading '#'.
let collapse = comment[start_idx..]
.iter()
.take_while(|&&b| b == b'#')
.count();

let anchor = start_idx + collapse;
if anchor == comment.len() {
// No point truncating an empty comment.
continue;
}

if comment[anchor].is_ascii_whitespace() {
// Any amount of spaces after `#` is fine.
continue;
}

let next_char = comment[anchor];
if SPECIAL_CHARS_AFTER_HASH_TO_IGNORE.contains(&next_char) {
continue;
}

// Add one space
comment.to_mut().insert(anchor, b' ');
}
}
self
}

/// Set each comment's leading indent to exactly `indent_depth` spaces
pub fn apply_spaces(mut self, indent_depth: ColNumber) -> Self {
let target = indent_depth as usize;
Expand Down
8 changes: 6 additions & 2 deletions librubyfmt/src/parser_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,11 @@ impl<'src> ParserState<'src> {
}

pub(crate) fn insert_comment_collection(&mut self, comments: CommentBlock) {
self.comments_to_insert
.merge(comments.apply_spaces(self.spaces_after_last_newline));
self.comments_to_insert.merge(
comments
.enforce_at_least_one_space_after_comment_symbol()
.apply_spaces(self.spaces_after_last_newline),
);
}

pub(crate) fn emit_op(&mut self, op: &'src [u8]) {
Expand Down Expand Up @@ -890,6 +893,7 @@ impl<'src> ParserState<'src> {
self.on_line(1);
}
Some(comments) => {
let comments = comments.enforce_at_least_one_space_after_comment_symbol();
let line_count = comments.line_count();
for token in comments.into_line_tokens() {
self.push_concrete_token(token);
Expand Down
Loading