Class: SlimLint::Filters::AutoIndenter

Inherits:
SlimLint::Filter show all
Defined in:
lib/slim_lint/filters/auto_indenter.rb

Overview

This filter annotates the sexp with indentation guidance, so that we can generate Ruby code with reasonable indentation semantics.

Constant Summary collapse

BLOCK_REGEX =
/(\A(if|unless|else|elsif|when|begin|rescue|ensure|case)\b)|\bdo\s*(\|[^|]*\|\s*)?\Z/

Instance Method Summary collapse

Methods included from SlimLint::Filter::Overrides

#on_escape, #on_html_attr, #on_html_attrs, #on_html_comment, #on_html_condcomment, #on_html_js, #on_html_tag, #on_multi, #on_slim_embedded, #on_slim_text

Instance Method Details

#on_slim_control(code, content) ⇒ Array

Handle control expression ‘[:slim, :control, code, content]`

Parameters:

  • code (String)

    Ruby code

  • content (Array)

    Temple expression

Returns:

  • (Array)

    Compiled temple expression



13
14
15
16
17
18
19
20
21
# File 'lib/slim_lint/filters/auto_indenter.rb', line 13

def on_slim_control(code, content)
  @self[3] = compile(content)
  if code.last.last.value =~ BLOCK_REGEX && content[0].value == :multi
    @self[3].insert(1, Sexp.new(:slim_lint, :indent, start: content.start, finish: content.start))
    @self[3].insert(-1, Sexp.new(:slim_lint, :outdent, start: content.finish, finish: content.finish))
  end

  @self
end

#on_slim_output(escape, code, content) ⇒ Array

Handle output expression ‘[:slim, :control, escape, code, content]`

Parameters:

  • code (String)

    Ruby code

  • content (Array)

    Temple expression

Returns:

  • (Array)

    Compiled temple expression



28
29
30
31
32
33
34
35
36
# File 'lib/slim_lint/filters/auto_indenter.rb', line 28

def on_slim_output(escape, code, content)
  @self[4] = compile(content)
  if code.last.last.value =~ BLOCK_REGEX && content[0].value == :multi
    @self[4].insert(1, Sexp.new(:slim_lint, :indent, start: content.start, finish: content.start))
    @self[4].insert(-1, Sexp.new(:slim_lint, :outdent, start: content.finish, finish: content.finish))
  end

  @self
end