Class: IndentR
- Inherits:
-
Object
- Object
- IndentR
- Defined in:
- lib/indentr.rb,
lib/intentr/version.rb,
lib/intentr/lang/ruby.rb
Defined Under Namespace
Classes: Ruby
Constant Summary collapse
- VERSION =
Version = "0.0.1"
Instance Method Summary collapse
- #buffer_tokens ⇒ Object
- #code ⇒ Object
-
#initialize(buffer, opt = {:indent => 2, :lang => :ruby}) ⇒ IndentR
constructor
A new instance of IndentR.
Constructor Details
#initialize(buffer, opt = {:indent => 2, :lang => :ruby}) ⇒ IndentR
Returns a new instance of IndentR.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/indentr.rb', line 6 def initialize(buffer, opt = {:indent => 2, :lang => :ruby}) @lang = opt[:lang] || :ruby @indent = opt[:indent] || 2 @opening_and_modifier_tokens = %w[if unless until while].to_set @opening_tokens = %w[begin case class def for module do {].to_set @closing_tokens = %w[end }].to_set @separator = [';', :operator] @coderay_proxy = CodeRay.scan(buffer, @lang) end |
Instance Method Details
#buffer_tokens ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/indentr.rb', line 17 def buffer_tokens buf_t = [] index = 0 slice = nil @coderay_proxy.each do |item| if 1 == index % 2 buf_t << [slice, item] else slice = item end index += 1 end return buf_t end |
#code ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/indentr.rb', line 33 def code indent_tokens = [] indent = 0 newline = false buffer_tokens.each {|token, kind| if kind == :keyword || kind == :operator if @closing_tokens.include?(token) indent -= 1 end end if kind == :space && token == "\n" indent_tokens << token newline = true elsif kind == :space if newline indent_tokens << (" " * indent * @indent) else indent_tokens << token end newline = false else if newline indent_tokens << (" " * indent * @indent) + token else indent_tokens << token end newline = false end if kind == :keyword || kind == :operator if @opening_tokens.include?(token) || \ @opening_and_modifier_tokens.include?(token) indent += 1 end end } indent_tokens.join("") end |