Class: RokuBuilder::IndentationInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/roku_builder/plugins/indentation_inspector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules:, path:) ⇒ IndentationInspector

Returns a new instance of IndentationInspector.



6
7
8
9
10
11
12
13
14
# File 'lib/roku_builder/plugins/indentation_inspector.rb', line 6

def initialize(rules:, path:)
  @character = get_character(rules[:character])
  @count = rules[:count].to_i
  @path = path
  @type = File.extname(path)[1..-1].to_sym
  @warnings = []
  @prev_line = nil
  @ind = 0
end

Instance Attribute Details

#warningsObject (readonly)

Returns the value of attribute warnings.



5
6
7
# File 'lib/roku_builder/plugins/indentation_inspector.rb', line 5

def warnings
  @warnings
end

Instance Method Details

#check_line(line:, number:, comment:) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/roku_builder/plugins/indentation_inspector.rb', line 16

def check_line(line:, number:, comment:)
  #byebug if number == 190 and @path.ends_with?("ScreenManager.brs")
  set_indentation(line: line)
  regexp = /^#{@character}{#{@ind}}[^#{@character}]/
  unless line =~ regexp or line == "\n" or line =~ /\'indent-ignore/ or comment
    add_warning(line: number)
  end
  @prev_line = line
end

#set_indentation(line:) ⇒ Object



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/roku_builder/plugins/indentation_inspector.rb', line 26

def set_indentation(line:)
  case @type
  when :xml
    if @prev_line and @prev_line =~ /<[^?!\/][^>]*[^\/]>/
      unless @prev_line =~ /<([^>\/]*)>.*<\/\1*>/
        @ind += @count
      end
    end
    if line =~ /<\/[^>]*>/
      unless line =~ /<([^>\/]*)>.*<\/\1*>/
        @ind -= @count
      end
    end
  when :brs
    if line =~ /'indent-reset/
      @ind = line.index(/[^#{@character}]/)
    else
      if @prev_line
        if @prev_line =~ /^\s*\'/ or @prev_line =~ /\'indent-ignore/
          # Don't change indentation
        elsif @prev_line =~ /[\{\[\(:]$/
          @ind += @count
        elsif @prev_line =~ /:\s*\bfunction\b|:\s*\bsub\b/i
          @ind += @count
        elsif @prev_line =~ /=\s*\bfunction\b|=\s*\bsub\b/i
          @ind += @count
        elsif @prev_line =~ /^\s*\bfunction\b|^\s*\bsub\b/i
          @ind += @count
        elsif @prev_line =~ /^\s*#?if\b|^\s*#?else\b/i
          unless @prev_line =~ /\bthen\b[ \t]*[^' \r\n]+.*$/i or @prev_line =~ /\breturn\b/i
            @ind += @count
          end
        elsif @prev_line =~ /^\s*\bfor\b|^\s*\bwhile\b/i
          @ind += @count
        elsif @prev_line =~ /^\s*\btry\b|^\s*\bcatch\b/i
          @ind += @count
        end
      end
      if line =~ /^\s*\'/ or line =~ /\'indent-ignore/
        # Don't change indentation
      elsif line =~ /^\s*[\}\]\)]/
        @ind -= @count
      elsif line =~ /^\s*\bfunction\b|^\s*\bsub\b/i
        # Don't change indentation
      elsif line =~ /^\s*:?\s*#?end\b|^\s*#?endif\b|^\s*endfor\b|^\s*\bnext\b/i
        @ind -= @count
      elsif line =~ /^\s*#?else\b|^\s*elseif\b/i
        @ind -= @count
      elsif line =~ /^\s*#?catch\b/i
        @ind -= @count
      end
    end
  end
end