Class: Kode::Indenter

Inherits:
Object
  • Object
show all
Defined in:
lib/kode.rb

Constant Summary collapse

INDENT_EXP =

indent regexp

[
  /^def\b/,
  /^if\b/,
  /^else\b/,
  /^elsif\b/,
  /\bdo\b/,
  /^class\b/,
  /^module\b/,
  /(=\s*|^)until\b/,
  /(=\s*|^)for\b/,
  /^unless\b/,
  /(=\s*|^)while\b/,
  /(=\s*|^)begin\b/,
  /(^| )case\b/,
  /\bthen\b/,
  /^rescue\b/,
  /^ensure\b/,
  /\bwhen\b/,
  /\{[^\}]*$/,
  /\[[^\]]*$/
]
OUTDENT_EXP =

outdent regexp

[
  /^end\b/,
  /^else\b/,
  /^elsif\b/,
  /^rescue\b/,
  /^ensure\b/,
  /\bwhen\b/,
  /^[^\{]*\}/,
  /^[^\[]*\]/
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Indenter

Creates cleaner. Takes hash of parametrs:

:indent_char - indent symbol (default: space)

:indent_size - quantity of indent symbols for single step (default: 2)



69
70
71
72
# File 'lib/kode.rb', line 69

def initialize(params = {})
  @indent_char = params[:indent_char] || " "
  @indent_size = params[:indent_size] || 2
end

Class Method Details

.process(params = {}) ⇒ Object

Cleaning source. Takes hash of parametrs:

:file - Source code as path to file

:text - Source code as ruby string

:backup - makes backup copy if true

:indent_char - indent symbol (default: “ ”)

:indent_size - quantity of indent symbols for single step



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/kode.rb', line 87

def self.process(params = {})
  unless params[:file].nil?
    params[:text] = File.read(params[:file])
    @path = params[:file]
  end

  output = self.new(params).process(params[:text])

  if params[:backup] && (source != dest) && params[:file]
    File.open(params[:file] + ".ugly~","w") { |f| f.write(params[:text]) }
    File.open(params[:file],"w") { |f| f.write(output) }
  else
    return output
  end

end

Instance Method Details

#process(source) ⇒ Object

Cleaning source. Takes source code as ruby string.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/kode.rb', line 107

def process(source)

  cursor_inside_comment_block = false
  cursor_at_program_end       = false

  multiline_array  = []
  multiline_string = ""

  indent_level  = 0
  dest          = ""

  source.each_line do |line|

    unless cursor_at_program_end

      # detect program end mark
      if line =~ /^__END__$/
        cursor_at_program_end = true
      else

        # combine continuing lines
        if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
          multiline_array.push line
          multiline_string += line.sub(/^(.*)\\\s*$/,"\\1")
          next
        end

        # add final line
        if (multiline_string.length > 0)
          multiline_array.push line
          multiline_string += line.sub(/^(.*)\\\s*$/,"\\1")
        end

        tline = ((multiline_string.length > 0) ? multiline_string : line).strip

        cursor_inside_comment_block = true if tline.match(/^=begin/)
      end
    end

    if (cursor_inside_comment_block or cursor_at_program_end)
      dest += line # add the line unchanged
    else

      cursor_at_comment_line = (tline =~ /^#/)

      unless cursor_at_comment_line
        # throw out sequences that will
        # only sow confusion
        # XXX WTF?
        while tline.gsub!(/\{[^\{]*?\}/,"")
        end
        while tline.gsub!(/\[[^\[]*?\]/,"")
        end
        while tline.gsub!(/'.*?'/,"")
        end
        while tline.gsub!(/".*?"/,"")
        end
        while tline.gsub!(/\`.*?\`/,"")
        end
        while tline.gsub!(/\([^\(]*?\)/,"")
        end
        while tline.gsub!(/\/.*?\//,"")
        end
        while tline.gsub!(/%r(.).*?\1/,"")
        end

        # delete end-of-line comments
        tline.sub!(/#[^\"]+$/,"")
        # convert quotes
        # WTF?
        tline.gsub!(/\\\"/,"'")
        OUTDENT_EXP.each do |re|
          if (tline =~ re)
            indent_level -= 1
            break
          end
        end
      end

      unless multiline_array.empty?
        multiline_array.each do |ml|
          dest += add_line(ml,indent_level)
        end
        multiline_array.clear
        multiline_string = ""
      else
        dest += add_line(line,indent_level)
      end

      unless cursor_at_comment_line
        INDENT_EXP.each do |re|
          if(tline =~ re && !(tline =~ /\s+end\s*$/))
            indent_level += 1
            break
          end
        end
      end

    end

    cursor_inside_comment_block = false if tline =~ /^=end/
  end


  if (indent_level != 0)
    STDERR.puts "#{@path}: Indentation error: #{indent_level}" if @path
  end

  return dest
end