Class: TerseRuby::Format
- Inherits:
-
Object
- Object
- TerseRuby::Format
- Defined in:
- lib/terse_ruby/format.rb
Overview
Simple class for formatting the list of lines
Class Method Summary collapse
-
.indent(lines) ⇒ Object
Apply indentation It is easier to do this once the full list of newlines is assembled.
-
.space_lines(lines) ⇒ Object
Insert empty lines in between the end of a method and the start of the next (same for classes & modules).
Class Method Details
.indent(lines) ⇒ Object
Apply indentation It is easier to do this once the full list of newlines is assembled
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/terse_ruby/format.rb', line 8 def self.indent lines indented_lines = [] indent_level = 0 keys = %w{ class module def if when case for } first_word_regex = /^([a-zA-Z0-9_]+)(\s+|$)/ lines.each do |l| l.strip! l =~ first_word_regex # set the $ variables for inspection # check for "end" before indenting indent_level -= 1 if $1 == "end" indent_level.times do l.insert(0, "\t") end indented_lines << l # puts indented_lines # check for indent-increasing keywords after indenting indent_level += 1 if keys.include?($1) end indented_lines end |
.space_lines(lines) ⇒ Object
Insert empty lines in between the end of a method and the start of the next (same for classes & modules)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/terse_ruby/format.rb', line 32 def self.space_lines lines spaced_lines = [] keys = %w{ class module def } first_word_regex = /^\s*([a-zA-Z0-9_]+)(\s+|$)/ for i in 0 ... lines.size - 1 first_line = lines[i] next_line = lines[i + 1] spaced_lines << first_line first_line =~ first_word_regex # set the $ variables for inspection # We only care about line-pairs where the first line is an end, and the next one matches one of our keys if $1 == "end" next_line =~ first_word_regex spaced_lines << "" if keys.include?($1) end end # Remember to add the last line! spaced_lines << lines[-1] spaced_lines end |