Module: Rvim::Reformat
- Defined in:
- lib/rvim/reformat.rb
Class Method Summary collapse
-
.wrap(lines, width) ⇒ Object
Word-wrap a sequence of lines to a column width.
- .wrap_paragraph(lines, width) ⇒ Object
Class Method Details
.wrap(lines, width) ⇒ Object
Word-wrap a sequence of lines to a column width. Treats every blank line as a paragraph separator: blank lines pass through and each non-blank chunk is rewrapped independently.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rvim/reformat.rb', line 8 def self.wrap(lines, width) return lines.dup if width.nil? || width <= 0 out = [] buffer = [] lines.each do |line| if line.to_s.strip.empty? out.concat(wrap_paragraph(buffer, width)) unless buffer.empty? out << '' buffer = [] else buffer << line end end out.concat(wrap_paragraph(buffer, width)) unless buffer.empty? out end |
.wrap_paragraph(lines, width) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rvim/reformat.rb', line 26 def self.wrap_paragraph(lines, width) text = lines.map(&:to_s).join(' ') words = text.split(/\s+/).reject(&:empty?) return [] if words.empty? result = [] current = +'' words.each do |w| if current.empty? current = w.dup elsif current.length + 1 + w.length <= width current << ' ' << w else result << current current = w.dup end end result << current unless current.empty? result end |