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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [unreleased]

- Trim trailing whitespaces of line_continuation when enabled.

## v1.0.0 (21-01-2025)

- Drop support for Ruby < 3.1.
Expand Down
6 changes: 6 additions & 0 deletions lib/wadler/print.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ def text(value, width: value.length)
#
# @see Wadler#break example on `line_continuation`.
def breakable(str = ' ', line_continuation: '', width: str.length)
if config.trim_trailing_whitespaces? && line_continuation
line_continuation = line_continuation.sub(/(?:#{Regexp.escape(whitespace)})+\z/, '')
end
tokens << Oppen.break(str, width:, line_continuation:, offset: current_indent)
self
end
Expand All @@ -370,6 +373,9 @@ def breakable(str = ' ', line_continuation: '', width: str.length)
#
# @return [self]
def break(line_continuation: '')
if line_continuation && config.trim_trailing_whitespaces?
line_continuation = line_continuation.sub(/(?:#{Regexp.escape(whitespace)})+\z/, '')
end
tokens << Oppen.line_break(line_continuation:, offset: current_indent)
self
end
Expand Down
14 changes: 14 additions & 0 deletions test/trim_trailing_whitespaces_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@
},
expected: "#{whitespace}a\n\n#{whitespace}a\n\n",
},
{
title: "trims `#{whitespace}` from line_continuation",
block: proc { |printer|
printer.text('Hello World!')
printer.break(line_continuation: "#{whitespace}^_^#{whitespace}")
printer.text('How are you doing?')
printer.breakable(line_continuation: "#{whitespace}^_^#{whitespace}")
},
expected: <<~LANG.chomp,
Hello World!#{whitespace}^_^
How are you doing?#{whitespace}^_^

LANG
},
].each do |test|
it test[:title] do
printer = Oppen::Wadler.new(width: 5, whitespace:)
Expand Down