Class: Tailor::Rulers::SpacesAfterLbraceRuler
- Inherits:
-
Tailor::Ruler
- Object
- Tailor::Ruler
- Tailor::Rulers::SpacesAfterLbraceRuler
- Defined in:
- lib/tailor/rulers/spaces_after_lbrace_ruler.rb
Overview
Checks for spaces after a ‘as given by @config. It skips checking when:
-
it’s at the end of a line.
-
the next char is a ‘’
-
it’s at the end of a line, followed by a trailing comment.
Instance Attribute Summary
Attributes inherited from Tailor::Ruler
Instance Method Summary collapse
- #check_spaces_after_lbrace(lexed_line, lineno) ⇒ Object
- #comment_update(token, lexed_line, file_text, lineno, column) ⇒ Object
-
#count_spaces(lexed_line, column) ⇒ Fixnum
Counts the number of spaces after the lbrace.
- #ignored_nl_update(lexed_line, lineno, column) ⇒ Object
-
#initialize(config, options) ⇒ SpacesAfterLbraceRuler
constructor
A new instance of SpacesAfterLbraceRuler.
- #lbrace_update(lexed_line, lineno, column) ⇒ Object
-
#measure(actual_spaces, lineno, column) ⇒ Object
Checks to see if the number of spaces after an lbrace equals the value at @config.
- #nl_update(lexed_line, lineno, column) ⇒ Object
Methods inherited from Tailor::Ruler
#add_child_ruler, #problem_type, #problems
Methods included from Logger::Mixin
Constructor Details
#initialize(config, options) ⇒ SpacesAfterLbraceRuler
Returns a new instance of SpacesAfterLbraceRuler.
12 13 14 15 16 |
# File 'lib/tailor/rulers/spaces_after_lbrace_ruler.rb', line 12 def initialize(config, ) super(config, ) add_lexer_observers :comment, :ignored_nl, :lbrace, :nl @lbrace_columns = [] end |
Instance Method Details
#check_spaces_after_lbrace(lexed_line, lineno) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/tailor/rulers/spaces_after_lbrace_ruler.rb', line 53 def check_spaces_after_lbrace(lexed_line, lineno) log "lbraces found at: #{@lbrace_columns}" unless @lbrace_columns.empty? @lbrace_columns.each do |column| actual_spaces = count_spaces(lexed_line, column) next if actual_spaces.nil? if @do_measurement == false log "Skipping measurement." else measure(actual_spaces, lineno, column) end @do_measurement = true end @lbrace_columns.clear end |
#comment_update(token, lexed_line, file_text, lineno, column) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/tailor/rulers/spaces_after_lbrace_ruler.rb', line 18 def comment_update(token, lexed_line, file_text, lineno, column) if token =~ /\n$/ log "Found comment with trailing newline." ignored_nl_update(lexed_line, lineno, column) end end |
#count_spaces(lexed_line, column) ⇒ Fixnum
Counts the number of spaces after the lbrace.
78 79 80 81 82 83 84 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 |
# File 'lib/tailor/rulers/spaces_after_lbrace_ruler.rb', line 78 def count_spaces(lexed_line, column) event_index = lexed_line.event_index(column) if event_index.nil? log "No lbrace in this line. Moving on..." @do_measurement = false return end next_event = lexed_line.at(event_index + 1) if next_event.nil? log "lbrace must be at the end of the line. Moving on." @do_measurement = false return 0 end if next_event[1] == :on_nl || next_event[1] == :on_ignored_nl log "lbrace is followed by a '#{next_event[1]}'. Moving on." @do_measurement = false return 0 end if next_event[1] == :on_rbrace log "lbrace is followed by an rbrace. Moving on." @do_measurement = false return 0 end second_next_event = lexed_line.at(event_index + 2) if second_next_event[1] == :on_comment log "Event + 2 is a comment." @do_measurement = false return next_event.last.size end next_event[1] != :on_sp ? 0 : next_event.last.size end |
#ignored_nl_update(lexed_line, lineno, column) ⇒ Object
25 26 27 |
# File 'lib/tailor/rulers/spaces_after_lbrace_ruler.rb', line 25 def ignored_nl_update(lexed_line, lineno, column) check_spaces_after_lbrace(lexed_line, lineno) end |
#lbrace_update(lexed_line, lineno, column) ⇒ Object
29 30 31 |
# File 'lib/tailor/rulers/spaces_after_lbrace_ruler.rb', line 29 def lbrace_update(lexed_line, lineno, column) @lbrace_columns << column end |
#measure(actual_spaces, lineno, column) ⇒ Object
Checks to see if the number of spaces after an lbrace equals the value at @config.
43 44 45 46 47 48 49 50 51 |
# File 'lib/tailor/rulers/spaces_after_lbrace_ruler.rb', line 43 def measure(actual_spaces, lineno, column) if actual_spaces != @config msg = "Line has #{actual_spaces} space(s) after a {, " msg << "but should have #{@config}." @problems << Problem.new(problem_type, lineno, column + 1, msg, @options[:level]) end end |
#nl_update(lexed_line, lineno, column) ⇒ Object
33 34 35 |
# File 'lib/tailor/rulers/spaces_after_lbrace_ruler.rb', line 33 def nl_update(lexed_line, lineno, column) ignored_nl_update(lexed_line, lineno, column) end |