Class: RubySourceStructure

Inherits:
SourceStructure show all
Defined in:
ext/ae-editor/ae-editor.rb

Instance Attribute Summary collapse

Attributes inherited from SourceStructure

#root

Instance Method Summary collapse

Methods inherited from SourceStructure

#class_node_by_line, #deep_node_by_line, #node_by_line, root

Constructor Details

#initialize(_source) ⇒ RubySourceStructure

Returns a new instance of RubySourceStructure.



232
233
234
235
# File 'ext/ae-editor/ae-editor.rb', line 232

def initialize(_source)
  super()
  parse_source(_source)
end

Instance Attribute Details

#injected_rowObject (readonly)

Returns the value of attribute injected_row.



230
231
232
# File 'ext/ae-editor/ae-editor.rb', line 230

def injected_row
  @injected_row
end

Instance Method Details

#class_methods(_class) ⇒ Object



368
369
# File 'ext/ae-editor/ae-editor.rb', line 368

def class_methods(_class)
end

#classiesObject



364
365
# File 'ext/ae-editor/ae-editor.rb', line 364

def classies
end

#modulesObject



366
367
# File 'ext/ae-editor/ae-editor.rb', line 366

def modules
end

#parse_source(_source) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'ext/ae-editor/ae-editor.rb', line 237

def parse_source(_source)
  _row = 1
  _liv = 0
  _livs = Array.new
  _livs[_liv]=@root
  _source.each_line{|line|
    line = "\s"+line.split("#")[0]+"\s"
    m = /[\s\n\t\;]+(module|class|def|if|unless|begin|case|for|while|do)[\s\n\t\;]+/.match("\s#{line}")
    if m
      index = m.post_match.strip.length - 1
      if m.post_match.strip[index,index]=='{'
        _row = _row +1
        next
      end
      _liv>=0? _liv = _liv + 1:_liv=1
      _pliv = _liv
      _parent = nil
      while (_parent == nil && _pliv>=0)
        _pliv = _pliv -1
        _parent = _livs[_pliv]
      end
      if _parent
        _helptext = m.post_match.strip
        _label = _helptext.split('<')[0]
        if _label == nil || _label.strip.length==0
          _label = _helptext
        end
        if (m[0].strip[0..4] == "class" && m.pre_match.strip.length==0)
          _kind = 'class'
          if m.post_match.strip[0..1]=='<<'
            hinner_class = true
          else
            hinner_class = false
          end
        elsif (m[0].strip[0..4] == "class" && m.pre_match.strip.length>0)
          _row = _row +1
          _liv = _liv - 1
          next
        elsif (m[0].strip[0..5] == "module" && m.pre_match.strip.length==0)
          _kind = 'module'
        elsif (m[0].strip[0..5] == "module" && m.pre_match.strip.length>0)
          _row = _row +1
          _liv = _liv - 1
          next
        elsif ((m[0].strip[0..4] == "begin")||(m[0].strip[0..3] == "case") ||(m[0].strip[0..4] == "while") || (m[0].strip[0..2] == "for") || (m[0].strip[0..1] == "do") || ((m[0].strip[0..1] == "if" || m[0].strip[0..5] == "unless") && m.pre_match.strip.length==0))
          _row = _row +1
          next
        elsif ((m[0].strip[0..1] == "if" || m[0].strip[0..5] == "unless") && m.pre_match.strip.length>0)
          _row = _row +1
          _liv = _liv - 1
          next
        elsif (m[0].strip[0..2] == "def" && m.pre_match.strip.length==0)
          _kind = 'method'
          if _label.include?(_parent.label + '.')
            _kind = 'singleton method'
          end
#          elsif (m[0].strip[0..10] == "attr_reader" && m.pre_match.strip.length==0)
#            _kind = 'KAttr_reader'
#            _liv = _liv - 1
#            _row = _row +1
        end
        
        if  _livs[_liv-1] && (_livs[_liv-1].kind != 'method' || (_livs[_liv-1].kind == 'method' && _kind == 'class' && hinner_class))
          SourceTreeNode.new(_parent, _kind){|_node|
            _node.label = _label
            _node.helptext = _helptext
            _node.rif = _row.to_s
            _livs[_pliv + 1]=_node
          }
        else
          SourceTreeNode.new(_livs[_liv-3], _kind){|_node|
            _node.label = _label
            _node.helptext = _helptext
            _node.rif = _row.to_s
            _livs[_pliv-1]=_node
          }
          _liv = _liv - 2
        end
      else
        _row = _row +1
        _liv = _liv - 1
        next
      end
    end
    m_end = /[\s\n\t\;]+end[\s\n\t\;]+/.match(line)
    if m_end
      if _livs[_liv]
        _livs[_liv].rif_end = _row
        #p "#{_livs[_liv].helptext} rif_end = #{_livs[_liv].rif_end}"
      end
      _liv = _liv - 1
    end
    _row = _row +1
  }
end

#scheletor_from_node(_node, _source = '', _injected_source = '', _injected_class = '') ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'ext/ae-editor/ae-editor.rb', line 334

def scheletor_from_node(_node, _source='', _injected_source='', _injected_class='')
  _hinner_source = ''
  #_sons = _node.sons.sort
  _sons = _node.sons
  for inode in 0.._sons.length - 1
    _son = _sons[inode]
    if _son.kind == 'class'
       _hinner_source = "#{_hinner_source}class #{_son.helptext}\n"
    elsif _son.kind == 'module'
       _hinner_source = "#{_hinner_source}module #{_son.helptext}\n"
    elsif _son.kind == 'method' && _son.helptext != 'initialize'
       
       _hinner_source = "#{_hinner_source}  def #{_son.helptext}\n"
       _hinner_source = "#{_hinner_source}  end\n"
    elsif _son.kind == 'singleton method'
       _hinner_source = "#{_hinner_source}  def #{_son.helptext}\n"
       _hinner_source = "#{_hinner_source}  end\n"
    end
    _hinner_source= scheletor_from_node(_son, _hinner_source, _injected_source, _injected_class)
  end
  _source = "#{_source}#{_hinner_source}" if _hinner_source.strip.length>0
  if _node.kind == 'class' && _node.label == _injected_class
    _source = "#{_source}  def initialize\n  #{_injected_source}  end\n"
    @injected_row = _source.split("\n").length-2
  end
  _source = "#{_source}end\n" if _node.kind == 'class' || _node.kind == 'module'
  _source
end