Class: RubyGrepFileSourceStructure

Inherits:
FileSourceStructure show all
Includes:
RubySourceStructureUtils
Defined in:
ext/ae-editor/ae-editor.rb

Instance Attribute Summary

Attributes included from RubySourceStructureUtils

#injected_row

Attributes inherited from SourceStructure

#root

Instance Method Summary collapse

Methods included from RubySourceStructureUtils

#scheletor_from_node

Methods inherited from FileSourceStructure

#initialize_blank_rows

Methods inherited from SourceStructure

#class_node_by_line, #deep_node_by_line, #node_by_line, root

Constructor Details

#initialize(_file, _row_rif = nil) ⇒ RubyGrepFileSourceStructure

Returns a new instance of RubyGrepFileSourceStructure.



153
154
155
156
157
158
159
160
161
162
# File 'ext/ae-editor/ae-editor.rb', line 153

def initialize(_file, _row_rif = nil)
  super(_file, _row_rif)
  @last_class_node = @root
  @last_node = @root
  @last_end_node_row = 0
  @last_class_node_row = 0
  @last_end_indentation = 0
  @last_class_indentation = 0
  build_structure
end

Instance Method Details

#build_structureObject



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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/ae-editor/ae-editor.rb', line 164

def build_structure
  output = grep
  output.each {|line|
    row, other = line.split(':')
    
    indent = 0
    l0 = other.length
    if l0 > 0
      indent = l0 - other.lstrip.length
    end
    
    other.strip! if other
    key_word, other = other.split
    other.strip! if other
    next if !['class','module','def','end'].include?(key_word)
    if key_word == 'end'
      @last_end_node_row = row
      @last_end_indentation = indent
      next
    end
    # hinner class
    if ['class','module','def'].include?(key_word) && @last_class_node != @root && @last_class_node.parent != @root  && ((@last_class_indentation.to_i - @last_end_indentation.to_i).abs <= 1)
      @last_class_node = @last_class_node.parent
      @last_class_node_row = row
    end
    # kind
    if ['class','module'].include?(key_word)
      kind = key_word
      if @last_end_node_row.to_i > @last_class_node_row.to_i || @last_class_node == @root
        parent = @root
      else
        parent = @last_class_node
        @last_class_indentation = indent
      end
    elsif ['def'].include?(key_word)
      kind = 'method'
      parent = @last_class_node
    end
    #name
    name = other.split('(')[0].strip
    next if name == '<<' && kind == 'class'
    #helptext
    helptext = line.strip
    #singleton
    if kind == 'method' && name.split('.')[0] == parent.label
      kind = 'singleton method'
      name = name.sub("#{parent.label}.",'')
    end
    # create node
    node = SourceTreeNode.new(parent, kind)
    node.label = name
    node.helptext = helptext
    node.args = ''
    node.rif = row
    ln_rif = node.rif.to_i - 1 
    while @blank_rows.include?(ln_rif) && ln_rif > 0
      ln_rif = ln_rif - 1
    end
    @last_node.rif_end = ln_rif.to_s

    if ['class','module'].include?(key_word)
      @last_class_node = node
      @last_class_node_row = row
    end
    node.sortable = true
    @last_node = node
  }
end

#grepObject



233
234
235
236
237
238
239
240
241
242
243
244
# File 'ext/ae-editor/ae-editor.rb', line 233

def grep
  _cmd_ = "|grep -nE '^class\s|\sclass\s|^module\s|\smodule\s|^def\s|\sdef\s|^end\s|\send\s|^end$|\send$' #{@file}"
  to_ret = ''
  begin
    open(_cmd_, "r"){|f| 
      to_ret = f.readlines
    }
  rescue RuntimeError => e
    Arcadia.runtime_error(e)
  end
  to_ret
end