Sha256: 5336ee66f5755c61579a8ad4bfafdaf0f3aa82c2f164cc1e034abed6d9f47492

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

module SlimLint
  module Filters
    # This filter annotates the sexp with indentation guidance, so that we can
    # generate Ruby code with reasonable indentation semantics.
    class AutoIndenter < Filter
      BLOCK_REGEX = /(\A(if|unless|else|elsif|when|begin|rescue|ensure|case)\b)|\bdo\s*(\|[^|]*\|\s*)?\Z/

      # Handle control expression `[:slim, :control, code, content]`
      #
      # @param [String] code Ruby code
      # @param [Array] content Temple expression
      # @return [Array] Compiled temple expression
      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

      # Handle output expression `[:slim, :control, escape, code, content]`
      #
      # @param [String] code Ruby code
      # @param [Array] content Temple expression
      # @return [Array] Compiled temple expression
      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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
slim_lint_standard-0.0.2.2 lib/slim_lint/filters/auto_indenter.rb
slim_lint_standard-0.0.2.1 lib/slim_lint/filters/auto_indenter.rb
slim_lint_standard-0.0.2 lib/slim_lint/filters/auto_indenter.rb
slim_lint_standard-0.0.1 lib/slim_lint/filters/auto_indenter.rb
slim_lint_standard-0.0.0 lib/slim_lint/filters/auto_indenter.rb