2015-03-16 02:37:10 +09:00
|
|
|
module Hamlit
|
|
|
|
module Concerns
|
|
|
|
module Indentable
|
|
|
|
EOF = -1
|
|
|
|
|
|
|
|
def reset_indent
|
|
|
|
@current_indent = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return nearest line's indent level since next line. This method ignores
|
|
|
|
# empty line. It returns -1 if next_line does not exist.
|
|
|
|
def next_indent
|
2015-03-16 18:18:44 +09:00
|
|
|
count_indent(next_line)
|
2015-03-16 02:37:10 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def with_indented(&block)
|
|
|
|
@current_indent += 1
|
|
|
|
result = block.call
|
|
|
|
@current_indent -= 1
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2015-03-16 18:18:44 +09:00
|
|
|
def count_indent(line, strict: false)
|
|
|
|
return EOF unless line
|
2015-03-16 02:37:10 +09:00
|
|
|
width = line[/\A +/].to_s.length
|
2015-03-16 18:18:44 +09:00
|
|
|
|
|
|
|
return (width + 1) / 2 unless strict
|
2015-03-16 02:37:10 +09:00
|
|
|
raise SyntaxError if width.odd?
|
|
|
|
|
|
|
|
width / 2
|
|
|
|
end
|
|
|
|
|
|
|
|
def same_indent?(line)
|
|
|
|
return false unless line
|
|
|
|
count_indent(line) == @current_indent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|