Class: Tailor::Rulers::IndentationSpacesRuler
- Inherits:
-
Tailor::Ruler
- Object
- Tailor::Ruler
- Tailor::Rulers::IndentationSpacesRuler
- Defined in:
- lib/tailor/rulers/indentation_spaces_ruler.rb,
lib/tailor/rulers/indentation_spaces_ruler/indentation_manager.rb
Defined Under Namespace
Classes: IndentationManager
Instance Attribute Summary
Attributes inherited from Tailor::Ruler
Instance Method Summary collapse
- #comment_update(token, lexed_line, file_text, lineno, column) ⇒ Object
- #embexpr_beg_update(lexed_line, lineno, column) ⇒ Object
-
#embexpr_end_update(current_lexed_line, lineno, column) ⇒ Object
Due to a Ripper bug that was fixed in ruby 2.0.0-p0, this will not get triggered if you’re using 1.9.x.
- #ignored_nl_update(current_lexed_line, lineno, column) ⇒ Object
-
#in_embexpr? ⇒ Boolean
Since Ripper in Ruby 1.9.x parses the } in a #{} as :on_rbrace instead of :on_embexpr_end, this works around that by using +@embexpr_beg to track the state of that event.
- #in_tstring? ⇒ Boolean
-
#initialize(config, options) ⇒ IndentationSpacesRuler
constructor
A new instance of IndentationSpacesRuler.
- #kw_update(token, lexed_line, lineno, column) ⇒ Object
- #lbrace_update(lexed_line, lineno, column) ⇒ Object
- #lbracket_update(lexed_line, lineno, column) ⇒ Object
- #lparen_update(lineno, column) ⇒ Object
-
#measure(lineno, column) ⇒ Object
Checks if the line’s indentation level is appropriate.
- #nl_update(current_lexed_line, lineno, column) ⇒ Object
- #rbrace_update(current_lexed_line, lineno, column) ⇒ Object
- #rbracket_update(current_lexed_line, lineno, column) ⇒ Object
- #rparen_update(current_lexed_line, lineno, column) ⇒ Object
- #tstring_beg_update(lexed_line, lineno) ⇒ Object
- #tstring_end_update(current_line) ⇒ Object
Methods inherited from Tailor::Ruler
#add_child_ruler, #problem_type, #problems
Methods included from Logger::Mixin
Constructor Details
#initialize(config, options) ⇒ IndentationSpacesRuler
Returns a new instance of IndentationSpacesRuler.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 9 def initialize(config, ) super(config, ) add_lexer_observers( :comment, :embexpr_beg, :embexpr_end, :ignored_nl, :kw, :lbrace, :lbracket, :lparen, :nl, :rbrace, :rbracket, :rparen, :tstring_beg, :tstring_end ) @manager = IndentationManager.new(@config) @embexpr_nesting = [] @tstring_nesting = [] end |
Instance Method Details
#comment_update(token, lexed_line, file_text, lineno, column) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 32 def comment_update(token, lexed_line, file_text, lineno, column) if token.fake_backslash_line_end? log "Line was altered by tailor to accommodate trailing backslash" @manager.add_indent_reason(:trailing_backslash, :trailing_backslash, lineno) end # trailing comment? if token.ends_with_newline? log "Comment ends with newline. Removing comment..." log "Old lexed line: #{lexed_line.inspect}" new_lexed_line = lexed_line.remove_trailing_comment(file_text) log "New lexed line: #{new_lexed_line.inspect}" if new_lexed_line.ends_with_ignored_nl? log "New lexed line ends with :on_ignored_nl." ignored_nl_update(new_lexed_line, lineno, column) elsif new_lexed_line.ends_with_nl? log "New lexed line ends with :on_nl." nl_update(new_lexed_line, lineno, column) end end end |
#embexpr_beg_update(lexed_line, lineno, column) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 58 def embexpr_beg_update(lexed_line, lineno, column) @embexpr_nesting << true token = Tailor::Lexer::Token.new('{') @manager.update_for_opening_reason(:on_embexpr_beg, token, lineno) end |
#embexpr_end_update(current_lexed_line, lineno, column) ⇒ Object
Due to a Ripper bug that was fixed in ruby 2.0.0-p0, this will not get triggered if you’re using 1.9.x. More info: bugs.ruby-lang.org/issues/6211
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 68 def embexpr_end_update(current_lexed_line, lineno, column) @embexpr_nesting.pop if @manager.multi_line_braces?(lineno) log "End of multi-line braces!" if current_lexed_line.only_embexpr_end? @manager.amount_to_change_this -= 1 msg = "lonely embexpr_end. " msg << "change_this -= 1 -> #{@manager.amount_to_change_this}" log msg end end @manager.update_for_closing_reason(:on_embexpr_end, current_lexed_line) end |
#ignored_nl_update(current_lexed_line, lineno, column) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 85 def ignored_nl_update(current_lexed_line, lineno, column) log "indent reasons on entry: #{@manager.indent_reasons}" if current_lexed_line.only_spaces? log "Line of only spaces. Moving on." # todo: maybe i shouldn't return here? ...do transitions? return end if @manager.line_ends_with_single_token_indenter?(current_lexed_line) log "Line ends with single-token indent token." unless @manager.in_an_enclosure? && current_lexed_line.ends_with_comma? log "Line-ending single-token indenter found." token_event = current_lexed_line.last_non_line_feed_event unless @manager.line_ends_with_same_as_last token_event msg = "Line ends with different type of single-token " msg << "indenter: #{token_event}" log msg @manager.add_indent_reason(token_event[1], token_event.last, lineno) end end end @manager.update_actual_indentation(current_lexed_line) @manager.set_up_line_transition measure(lineno, column) log "indent reasons on exit: #{@manager.indent_reasons}" # prep for next line @manager.transition_lines end |
#in_embexpr? ⇒ Boolean
Since Ripper in Ruby 1.9.x parses the } in a #{} as :on_rbrace instead of :on_embexpr_end, this works around that by using +@embexpr_beg to track the state of that event. As such, this should only be called from #rbrace_update.
191 192 193 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 191 def in_embexpr? !@embexpr_nesting.empty? end |
#in_tstring? ⇒ Boolean
269 270 271 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 269 def in_tstring? !@tstring_nesting.empty? end |
#kw_update(token, lexed_line, lineno, column) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 121 def kw_update(token, lexed_line, lineno, column) if lexed_line.keyword_is_symbol? log "Keyword is prefaced by a :, indicating it's really a Symbol." return end if token == "end" @manager.update_for_closing_reason(:on_kw, lexed_line) return end if token.continuation_keyword? log "Continuation keyword found: '#{token}'." @manager.update_for_continuation_reason(token, lexed_line, lineno) return end if token.keyword_to_indent? log "Indent keyword found: '#{token}'." @manager.update_for_opening_reason(:on_kw, token, lineno) end end |
#lbrace_update(lexed_line, lineno, column) ⇒ Object
144 145 146 147 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 144 def lbrace_update(lexed_line, lineno, column) token = Tailor::Lexer::Token.new('{') @manager.update_for_opening_reason(:on_lbrace, token, lineno) end |
#lbracket_update(lexed_line, lineno, column) ⇒ Object
149 150 151 152 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 149 def lbracket_update(lexed_line, lineno, column) token = Tailor::Lexer::Token.new('[') @manager.update_for_opening_reason(:on_lbracket, token, lineno) end |
#lparen_update(lineno, column) ⇒ Object
154 155 156 157 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 154 def lparen_update(lineno, column) token = Tailor::Lexer::Token.new('(') @manager.update_for_opening_reason(:on_lparen, token, lineno) end |
#measure(lineno, column) ⇒ Object
Checks if the line’s indentation level is appropriate.
277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 277 def measure(lineno, column) log "Measuring..." if @manager.actual_indentation != @manager.should_be_at msg = "Line is indented to column #{@manager.actual_indentation}, " msg << "but should be at #{@manager.should_be_at}." @problems << Problem.new(problem_type, lineno, @manager.actual_indentation, msg, @options[:level]) end end |
#nl_update(current_lexed_line, lineno, column) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 159 def nl_update(current_lexed_line, lineno, column) log "indent reasons on entry: #{@manager.indent_reasons}" @manager.update_actual_indentation(current_lexed_line) if @manager.last_indent_reason_type != :on_kw && @manager.last_indent_reason_type != :on_lbrace && @manager.last_indent_reason_type != :on_lbracket && @manager.last_indent_reason_type != :on_lparen && !@manager.last_indent_reason_type.nil? log "last indent reason type: #{@manager.last_indent_reason_type}" log "I think this is a single-token closing line..." @manager.update_for_closing_reason(@manager.indent_reasons. last[:event_type], current_lexed_line) end @manager.set_up_line_transition unless current_lexed_line.end_of_multi_line_string? measure(lineno, column) end log "indent reasons on exit: #{@manager.indent_reasons}" @manager.transition_lines end |
#rbrace_update(current_lexed_line, lineno, column) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 195 def rbrace_update(current_lexed_line, lineno, column) # Is this an rbrace that should've been parsed as an embexpr_end? if in_embexpr? && RUBY_VERSION < '2.0.0' msg = "Got :rbrace and @embexpr_beg is true. " msg << " Must be at an @embexpr_end." log msg @embexpr_nesting.pop @manager.update_for_closing_reason(:on_embexpr_end, current_lexed_line) return end if @manager.multi_line_braces?(lineno) log "End of multi-line braces!" if current_lexed_line.only_rbrace? @manager.amount_to_change_this -= 1 msg = "lonely rbrace. " msg << "change_this -= 1 -> #{@manager.amount_to_change_this}" log msg end end @manager.update_for_closing_reason(:on_rbrace, current_lexed_line) end |
#rbracket_update(current_lexed_line, lineno, column) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 220 def rbracket_update(current_lexed_line, lineno, column) if @manager.multi_line_brackets?(lineno) log "End of multi-line brackets!" if current_lexed_line.only_rbracket? @manager.amount_to_change_this -= 1 msg = "lonely rbracket. " msg << "change_this -= 1 -> #{@manager.amount_to_change_this}" log msg end end @manager.update_for_closing_reason(:on_rbracket, current_lexed_line) end |
#rparen_update(current_lexed_line, lineno, column) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 235 def rparen_update(current_lexed_line, lineno, column) if @manager.multi_line_parens?(lineno) log "End of multi-line parens!" if current_lexed_line.only_rparen? @manager.amount_to_change_this -= 1 msg = "lonely rparen. " msg << "change_this -= 1 -> #{@manager.amount_to_change_this}" log msg end end @manager.update_for_closing_reason(:on_rparen, current_lexed_line) end |
#tstring_beg_update(lexed_line, lineno) ⇒ Object
250 251 252 253 254 255 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 250 def tstring_beg_update(lexed_line, lineno) @tstring_nesting << lineno @manager.update_actual_indentation(lexed_line) log "tstring_nesting is now: #{@tstring_nesting}" @manager.stop end |
#tstring_end_update(current_line) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/tailor/rulers/indentation_spaces_ruler.rb', line 257 def tstring_end_update(current_line) unless @tstring_nesting.empty? tstring_start_line = @tstring_nesting.pop if tstring_start_line < current_line measure(tstring_start_line, @manager.actual_indentation) end end @manager.start unless in_tstring? end |