Class: SlimLint::Corrector
- Inherits:
-
Object
- Object
- SlimLint::Corrector
- Defined in:
- lib/slim_lint/corrector.rb
Overview
Applies the corrections attached to a set of lints to a document's source, producing the corrected source text.
Instance Method Summary collapse
-
#correct(lints) ⇒ String
Applies every correctable lint's correction to the document's source and marks each applied lint as corrected.
-
#initialize(document) ⇒ Corrector
constructor
A new instance of Corrector.
Constructor Details
#initialize(document) ⇒ Corrector
Returns a new instance of Corrector.
8 9 10 |
# File 'lib/slim_lint/corrector.rb', line 8 def initialize(document) @document = document end |
Instance Method Details
#correct(lints) ⇒ String
Applies every correctable lint's correction to the document's source and marks each applied lint as corrected.
A correction maps the current text of its line to the corrected text:
returning nil removes the line entirely, and returning a string
containing embedded newlines replaces the line with multiple lines.
Corrections are applied from the last line to the first so that a
correction which adds or removes lines never invalidates the line
numbers of corrections still waiting to be applied.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/slim_lint/corrector.rb', line 24 def correct(lints) # `-1` keeps a trailing empty element when the source ends with a # newline, so the array can be joined back with "\n" to exactly # reproduce the source (including its trailing newline, or lack of one). lines = @document.source.split("\n", -1) correction_groups(lints).each do |line_number, line_lints| index = line_number - 1 next unless index.between?(0, lines.length - 1) lines[index, 1] = apply_corrections(lines[index], line_lints) end @document.source_prefix + lines.join("\n") end |