Module: RubyBeautify
- Extended by:
- RubyBeautify
- Included in:
- RubyBeautify
- Defined in:
- lib/ruby-beautify.rb,
lib/ruby-beautify/version.rb
Constant Summary collapse
- OPEN_BLOCK_START =
["module", "class", "begin", "def", 'if', 'while', 'unless', 'case']
- BOTH_BLOCK =
["else", "elsif", 'rescue', 'when']
- OPEN_BLOCK_DO =
['do', '{']
- CLOSE_BLOCK =
['end', '}']
- OPEN_BRACKETS =
[:on_lparen, :on_lbracket, :on_lbrace, :on_embexpr_beg, :on_tlambeg]
- CLOSE_BRACKETS =
[:on_rparen, :on_rbracket, :on_rbrace, :on_embexpr_end]
- NEW_LINES =
[:on_nl, :on_ignored_nl, :on_comment, :on_embdoc_end]
- VERSION =
"0.97.4"
Instance Method Summary collapse
-
#both_block?(line_lex) ⇒ Boolean
is the first word one of our ‘both’ keywords?.
-
#closing_assignment?(line_lex) ⇒ Boolean
…
-
#closing_block?(line_lex) ⇒ Boolean
kinda complex, we count open/close to determine if we ultimately have close a hanging line.
-
#contains_block_assignment?(line_lex) ⇒ Boolean
test for assignment from a block.
-
#opening_assignment?(line_lex) ⇒ Boolean
same trick as opening_block.
-
#opening_block?(line_lex) ⇒ Boolean
kinda complex, we count open/close to determine if we ultimately have a hanging line.
- #pretty_string(content, indent_token: "\t", indent_count: 1) ⇒ Object
-
#starts_block?(line_lex) ⇒ Boolean
is the first word a key word?.
-
#syntax_ok?(string) ⇒ Boolean
check the syntax of a string, will pipe it through the ruby bin to see if it has a valid syntax.
Instance Method Details
#both_block?(line_lex) ⇒ Boolean
is the first word one of our ‘both’ keywords?
98 99 100 101 102 103 104 105 |
# File 'lib/ruby-beautify.rb', line 98 def both_block?(line_lex) line_lex.each do |x| # search for a first non-space token if not x[1] == :on_sp return x[1] == :on_kw && BOTH_BLOCK.include?(x[2]) end end end |
#closing_assignment?(line_lex) ⇒ Boolean
…
69 70 71 72 73 74 |
# File 'lib/ruby-beautify.rb', line 69 def closing_assignment?(line_lex) opens = opening_assignment_count line_lex closes = closing_assignment_count line_lex return false if opens == closes return true if closes > opens end |
#closing_block?(line_lex) ⇒ Boolean
kinda complex, we count open/close to determine if we ultimately have close a hanging line. Always true if it’s a both_block.
119 120 121 122 123 124 125 126 |
# File 'lib/ruby-beautify.rb', line 119 def closing_block?(line_lex) return true if both_block? line_lex opens = starts_block?(line_lex) ? 1 : 0 opens += opening_block_count line_lex closes = closing_block_count line_lex return false if opens == closes return true if opens < closes end |
#contains_block_assignment?(line_lex) ⇒ Boolean
test for assignment from a block
77 78 79 80 81 82 83 84 |
# File 'lib/ruby-beautify.rb', line 77 def contains_block_assignment?(line_lex) compacted_line = line_lex.reject{|x| x[1] == :on_sp} #remove spaces idx = compacted_line.rindex{|x| ['=', '||='].include? x[2]} #find last equal if idx return OPEN_BLOCK_START.include?(compacted_line[idx+1][2]) #check for if/begin block end return false end |
#opening_assignment?(line_lex) ⇒ Boolean
same trick as opening_block
61 62 63 64 65 66 |
# File 'lib/ruby-beautify.rb', line 61 def opening_assignment?(line_lex) opens = opening_assignment_count line_lex closes = closing_assignment_count line_lex return false if opens == closes return true if opens > closes end |
#opening_block?(line_lex) ⇒ Boolean
kinda complex, we count open/close to determine if we ultimately have a hanging line. Always true if it’s a both_block.
109 110 111 112 113 114 115 |
# File 'lib/ruby-beautify.rb', line 109 def opening_block?(line_lex) opens = (starts_block?(line_lex) || both_block?(line_lex)) ? 1 : 0 opens += opening_block_count line_lex closes = closing_block_count line_lex return false if opens == closes return true if opens > closes end |
#pretty_string(content, indent_token: "\t", indent_count: 1) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ruby-beautify.rb', line 18 def pretty_string(content, indent_token: "\t", indent_count: 1) output_string = "" raise "Bad Syntax" unless syntax_ok? content lex = ::Ripper.lex(content) indent_level = 0 line_lex = [] # walk through line tokens lex.each do |token| line_lex << token if NEW_LINES.include? token[1] # if type of this token is a new line # did we just close something? if so, lets bring it down a level. if closing_block?(line_lex) || closing_assignment?(line_lex) indent_level -= 1 if indent_level > 0 end # print our line, in place. line_string = line_lex.map {|l| l.last}.join output_string += indented_line(indent_level, indent_token, indent_count, line_string) # oh, we opened something did we? lets indent for the next run. if opening_block?(line_lex) || opening_assignment?(line_lex) indent_level += 1 end line_lex.clear end end return output_string end |
#starts_block?(line_lex) ⇒ Boolean
is the first word a key word?
87 88 89 90 91 92 93 94 95 |
# File 'lib/ruby-beautify.rb', line 87 def starts_block?(line_lex) return true if contains_block_assignment? line_lex line_lex.each do |x| # search for a first non-space token if not x[1] == :on_sp return x[1] == :on_kw && OPEN_BLOCK_START.include?(x[2]) end end end |
#syntax_ok?(string) ⇒ Boolean
check the syntax of a string, will pipe it through the ruby bin to see if it has a valid syntax.
54 55 56 57 58 |
# File 'lib/ruby-beautify.rb', line 54 def syntax_ok?(string) out, err, status = Open3.capture3("ruby -c -", stdin_data:string ) return false unless err.empty? return true end |