Class: Shirobai::Cop::Layout::IndentationWidth

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
RuboCop::Cop::AllowedPattern, RuboCop::Cop::ConfigurableEnforcedStyle
Defined in:
lib/shirobai/cop/layout/indentation_width.rb

Overview

Drop-in Rust reimplementation of ‘Layout/IndentationWidth`.

Rust walks the AST, decides the base location for every indentable body (def/class/module/if/case/while/for/block/rescue/ensure/begin), computes ‘column_offset_between(body, base)` and the resulting `column_delta`, and returns the offense range, the message, the `within?` autocorrect flag and the node range to realign. Ruby supplies the flattened config (and the `AllowedPatterns`-matched line numbers, since regex matching stays in Ruby) and applies the realignment via `AlignmentCorrector`, the same division of labour as the other indentation cops. Offenses come from the per-file bundled run (`Shirobai::Dispatch`) while `bundle_eligible?` holds; otherwise the standalone call carries the per-investigation state (allowed lines / accumulated correction ranges).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



25
# File 'lib/shirobai/cop/layout/indentation_width.rb', line 25

def self.badge = RuboCop::Cop::Badge.parse("Layout/IndentationWidth")

.bundle_args(config) ⇒ Object

Packed args for the bundled run: the 7-element config vector ‘Shirobai.check_indentation_width` receives (width / align-with / access-modifier outdent / indented internal methods / end alignment / def-end alignment / tabs).



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/shirobai/cop/layout/indentation_width.rb', line 31

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  end_config = config.for_cop("Layout/EndAlignment")
  end_align = case end_config["EnforcedStyleAlignWith"] || "keyword"
              when "variable" then 1
              when "start_of_line" then 2
              else 0
              end
  def_end_config = config.for_cop("Layout/DefEndAlignment")
  [
    cop_config["Width"] || 2,
    cop_config["EnforcedStyleAlignWith"] == "relative_to_receiver" ? 1 : 0,
    config.for_cop("Layout/AccessModifierIndentation")["EnforcedStyle"] == "outdent" ? 1 : 0,
    config.for_cop("Layout/IndentationConsistency")["EnforcedStyle"] == "indented_internal_methods" ? 1 : 0,
    end_align,
    (def_end_config["EnforcedStyleAlignWith"] || "start_of_line") == "def" ? 1 : 0,
    (config.for_cop("Layout/IndentationStyle")["EnforcedStyle"] || "spaces") == "tabs" ? 1 : 0
  ]
end

.cop_nameObject



24
# File 'lib/shirobai/cop/layout/indentation_width.rb', line 24

def self.cop_name = "Layout/IndentationWidth"

Instance Method Details

#on_new_investigationObject



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
80
# File 'lib/shirobai/cop/layout/indentation_width.rb', line 51

def on_new_investigation
  buffer = processed_source.buffer
  off = SourceOffsets.for(processed_source.raw_source)

  offenses_for_source.each do |start, fin, column_delta, message, autocorrect, cs, ce|
    # Mirror `other_offense_in_same_range?`: the cop instance accumulates
    # correction ranges across autocorrect iterations so a correction
    # nested in an already-corrected range is reported but not corrected.
    # These go BACK to Rust (`check_indentation_width` prior ranges), so
    # they stay BYTE offsets — only the Ruby-side ranges get converted.
    @offense_ranges << [cs, ce] if autocorrect

    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    # Key the split on the per-offense flag, not `autocorrect?` mode: the
    # block runs in lint mode too and the non-empty corrector is what
    # keeps the offense correctable to match stock (see argument_alignment).
    unless autocorrect
      add_offense(range, message: message)
      next
    end

    add_offense(range, message: message) do |corrector|
      node = node_at(off[cs], off[ce])
      target = node || Parser::Source::Range.new(buffer, off[cs], off[ce])
      RuboCop::Cop::AlignmentCorrector.correct(
        corrector, processed_source, target, column_delta
      )
    end
  end
end