Class: SCSSLint::AutoCorrect::Correctors::Indentation

Inherits:
Base
  • Object
show all
Defined in:
lib/scss_lint/auto_correct/correctors/indentation.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#enabled?, linter_name, priority, short_name

Constructor Details

#initialize(config = {}) ⇒ Indentation

Returns a new instance of Indentation.



3
4
5
6
7
8
# File 'lib/scss_lint/auto_correct/correctors/indentation.rb', line 3

def initialize(config = {})
  super
  @result = []
  @level = 0
  @indentation_symbol = "  "
end

Class Method Details

.descriptionObject



31
32
33
# File 'lib/scss_lint/auto_correct/correctors/indentation.rb', line 31

def self.description
  "Fixes indentation"
end

Instance Method Details

#apply_indentation(line, level) ⇒ Object



27
28
29
# File 'lib/scss_lint/auto_correct/correctors/indentation.rb', line 27

def apply_indentation(line, level)
  (@indentation_symbol * level) + line.lstrip
end

#call(source) ⇒ Object



10
11
12
13
# File 'lib/scss_lint/auto_correct/correctors/indentation.rb', line 10

def call(source)
  source.lines.each { |line| visit_line(line) }
  @result.join
end

#visit_line(line) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/scss_lint/auto_correct/correctors/indentation.rb', line 15

def visit_line(line)
  if line =~ /^\s*\/\// || line =~ /^\s*$/
    @result << line
    return
  end
  opening = line.include? "{"
  closing = line.include? "}"
  @level -= 1 if closing && !opening
  @result << apply_indentation(line, @level)
  @level += 1 if opening && !closing
end