Class: Shirobai::Cop::Layout::IndentationWidth
- Inherits:
-
RuboCop::Cop::Base
- Object
- RuboCop::Cop::Base
- Shirobai::Cop::Layout::IndentationWidth
- 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
- .badge ⇒ Object
-
.bundle_args(config) ⇒ Object
Packed args for the bundled run: the 7-element config vector
Shirobai.check_indentation_widthreceives (width / align-with / access-modifier outdent / indented internal methods / end alignment / def-end alignment / tabs). - .cop_name ⇒ Object
Instance Method Summary collapse
Class Method Details
.badge ⇒ Object
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_name ⇒ Object
24 |
# File 'lib/shirobai/cop/layout/indentation_width.rb', line 24 def self.cop_name = "Layout/IndentationWidth" |
Instance Method Details
#on_new_investigation ⇒ Object
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 81 82 83 84 85 |
# 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, , 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]) # Always run the correction so the corrector is non-empty and the # offense is correctable, matching stock. `IndentationWidth#offense` # only nils the corrected node when `autocorrect? && other_offense_in_ # same_range?`; the `autocorrect?` guard means the suppression never # fires in lint mode, so every offense stays correctable there. We # mirror that: skip the correction only in a real autocorrect run when # the per-offense flag says this offense is nested in an already- # corrected range (an empty corrector then, exactly like stock's nil # node). In lint mode the flag is ignored and the correction always # runs. This keeps the `-a` convergence identical while fixing the # lint-mode `[Correctable]` divergence. add_offense(range, message: ) do |corrector| next if autocorrect? && !autocorrect 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 |