Class: SafeCompleteCode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_source, _row, _col, _file = nil) ⇒ SafeCompleteCode

Returns a new instance of SafeCompleteCode.



266
267
268
269
270
271
272
273
274
275
# File 'ext/ae-editor/ae-editor.rb', line 266

def initialize(_source, _row, _col, _file=nil)
  @source = _source
  @file = _file
  @row = _row.to_i
  @col = _col.to_i
  @ss = RubySourceStructure.new(_source)
  @filter=''
  @words = Array.new
  process_source
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



265
266
267
# File 'ext/ae-editor/ae-editor.rb', line 265

def filter
  @filter
end

#modified_colObject (readonly)

Returns the value of attribute modified_col.



264
265
266
# File 'ext/ae-editor/ae-editor.rb', line 264

def modified_col
  @modified_col
end

#modified_rowObject (readonly)

Returns the value of attribute modified_row.



264
265
266
# File 'ext/ae-editor/ae-editor.rb', line 264

def modified_row
  @modified_row
end

Instance Method Details

#candidates(_show_error = false) ⇒ Object



559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'ext/ae-editor/ae-editor.rb', line 559

def candidates(_show_error = false)
  temp_file = create_modified_temp_file(@file)
  begin
    Arcadia.is_windows??ruby='rubyw':ruby='ruby'
    _cmp_s = "|#{ruby} '#{temp_file}'"
    _ret = nil
    open(_cmp_s,"r") do  |f|
      _ret = f.readlines.collect!{| line | 
        #line.chomp
        line.strip
      } 
    end
    if @filter.strip.length > 0 && !is_dot?
      @words.each{|w| 
        if !(_ret.include?(w) || _ret.include?("##{w}")) 
          _ret << w
        end
      }
    end
    _ret.sort
  rescue Exception => e
    #Arcadia.console(self, 'msg'=>e.to_s, 'level'=>'error')
  ensure
    File.delete(temp_file) if File.exist?(temp_file)
  end
end

#create_modified_temp_file(_base_file = nil) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'ext/ae-editor/ae-editor.rb', line 586

def create_modified_temp_file(_base_file=nil)
  if _base_file
    _file = _base_file+'~~'
  else
    _file = File.join(Arcadia.instance.local_dir,'buffer~~')
  end
  f = File.new(_file, "w")
  begin
    if f
      f.syswrite(@modified_source)
    end
  ensure
    f.close unless f.nil?
  end
  _file
end

#declaration(_dec_line = '') ⇒ 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
362
363
364
365
366
# File 'ext/ae-editor/ae-editor.rb', line 334

def declaration(_dec_line='')
  if _dec_line.include?('.new')
    pre, post = _dec_line.split('.new')
    dec_line_processed = "#{pre}.new"
    post.strip! if post
    if post && post[0..0]=='('
      k=0
      ch = '('
      while k < post.length && ch != ')'
        k = k+1
        ch=post[k..k]
      end
      if ch == ')'
        args = post[1..k-1]
        args_array = args.split(',')
        n_args = args_array.length
        if n_args > 0
          new_args = ''
          1.upto( n_args ){
             if new_args.length > 0
               new_args = "#{new_args},"
             end
             new_args = "#{new_args}nil"
          }
          dec_line_processed = "#{dec_line_processed}(#{new_args})"
        end
      end
    end
  else
    dec_line_processed = _dec_line
  end
  dec_line_processed
end

#dot_trip(_var_name) ⇒ Object

def dot_trip(_var_name)

  ret = "_class=#{_var_name}.class.name\n"
  ret = ret +" _methods=#{_var_name}.methods\n"
  ret = ret +"owner_on = Method.instance_methods.include?('owner')\n"
  ret = ret + "_methods.each{|m|\n"
  ret = ret + "meth = #{_var_name}.method(m)\n"
  ret = ret +"if owner_on\n"
  ret = ret +"_owner=meth.owner.name\n"
  ret = ret +"else\n"
  ret = ret +"meth_insp = meth.inspect\n"
  ret = ret +"to_sub ='#<Method:\s'+_class\n"
  ret = ret +"_owner=meth_insp.sub(to_sub,'').split('#')[0].strip.sub('(','').sub(')','')\n"
  ret = ret +"_owner=_class if _owner.strip.length==0\n"
  ret = ret +"end\n"
  ret = ret + "if _owner != _class\n"
  ret = ret + "print _owner+'#'+m+'#'+meth.arity.to_s+'\n'\n"
  ret = ret +"else\n"
  ret = ret + "print ''+'#'+m+'#'+meth.arity.to_s+'\n'\n"
  ret = ret +"end\n"
  ret = ret + "}\n"
  ret = ret + "exit\n"
  ret
end


301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/ae-editor/ae-editor.rb', line 301

def dot_trip(_var_name)
  ret = "_class=#{_var_name}.class.name\n"
  ret = ret +"_methods=#{_var_name}.methods\n"
  ret = ret +"owner_on = Method.instance_methods.include?('owner')\n"
  ret = ret +"_methods.each{|m|\n"
  ret = ret +"meth = #{_var_name}.method(m)\n"
  ret = ret +"if owner_on\n"
  ret = ret +"  _owner=meth.owner.name\n"
  ret = ret +"else\n"
  ret = ret +"  meth_insp = meth.inspect\n"
  ret = ret +"  to_sub ='#<Method:\s'+_class\n"
  ret = ret +"  _owner=meth_insp.sub(to_sub,'').split('#')[0].strip.sub('(','').sub(')','')\n"
  ret = ret +"  _owner=_class if _owner.strip.length==0\n"
  ret = ret +"end\n"
  ret = ret +"if _owner != _class\n"
  ret = ret +'  print %Q{#{_owner}##{m}##{meth.arity.to_s}\n}'+"\n"
  ret = ret +"else\n"
  ret = ret +'  print %Q{##{m}##{meth.arity.to_s}\n}'+"\n"
  ret = ret +"end\n"
  ret = ret + "}\n"
  ret = ret + "exit\n"
  ret
end

#focus_word(focus_segment) ⇒ Object



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'ext/ae-editor/ae-editor.rb', line 543

def focus_word(focus_segment)
    focus_world = ''
    char = focus_segment[-1..-1]
    while [")","]","}"].include?(char) 
      char=focus_segment[-2..-2]
      focus_segment = focus_segment[0..-2]
    end
    j = focus_segment.length - 1
    while !["\s","\t",";",",","(","[","{",">"].include?(char) && j >= 0
      focus_world = "#{char}#{focus_world}"
      j=j-1
      char = focus_segment[j..j]
    end
    focus_world
end

#is_dot?Boolean

Returns:

  • (Boolean)


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

def is_dot?
  @is_dot
end

#process_sourceObject



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'ext/ae-editor/ae-editor.rb', line 386

def process_source
  @modified_source = ""
  @modified_row = @row
  @modified_col = @col
  source_array = @source.split("\n")
  #---------------------------------
  focus_line = source_array[@row-1]
  focus_line = '' if focus_line.nil?
  focus_world = ''
  if focus_line && focus_line.strip.length > 0
    if focus_line[@col-1..@col-1] == '.'
      @is_dot=true
      focus_segment = focus_line[0..@col-2]
    elsif focus_line.include?('.')
      @is_dot=true
      focus_segment_array = focus_line.split('.')
      focus_segment = ''
      focus_segment_array[0..-2].each{|seg|
        if focus_segment.strip.length > 0
          focus_segment = focus_segment+'.'
        end
        focus_segment = focus_segment+seg
      }
      @filter = focus_word(focus_segment_array[-1].strip)
    else
      focus_segment = ''
      @filter = focus_word(focus_line[0..@col-1].strip)
    end
    focus_world= focus_word(focus_segment)
  end
  @class_node = @ss.(@row)
  #---------------------------------
  @modified_source = "#{@modified_source}Dir.chdir('#{File.dirname(@file)}')\n" if @file
  @modified_row = @modified_row+1
  source_array.each_with_index{|line,j|
    # 0) if a comment I do not consider it
    if line.strip.length > 0 && line.strip[0..0]=='#'
      @modified_row = @modified_row-1
      m = /&require_dir_ref=[\s]*(.)*/.match(line)
      if m 
        require_dir_ref=line.split('&require_dir_ref=')[1].strip
        @modified_source = "#{@modified_source}Dir.chdir('#{require_dir_ref}')\n"
        @modified_row = @modified_row+1
      end   
      m = /&require_omissis=[\s]*(.)*/.match(line)
      if m 
        require_omissis=line.split('&require_omissis=')[1].strip
        @modified_source = "#{@modified_source}require '#{require_omissis}'\n"
        @modified_row = @modified_row+1
      end   
      
           
    # 1) includiano i require
    elsif line.strip.length>7 && (line.strip[0..7]=="require " || line.strip[0..7]=="require(")
      @modified_source = "#{@modified_source}#{line}\n"
      if line.strip[8..-1].include?("tk")
        @modified_source = "#{@modified_source}Tk.root.destroy if Tk && Tk.root\n"
      end
      #@modified_row = @modified_row+1
      #Arcadia.console(self, 'msg'=>"per require @modified_row=#{@modified_row}")
    # 2) includiano la riga da evaluare con un $SAFE 3
    elsif j.to_i == @row-1
      focus_line = line
      break
    # 3) eliminiamo la riga
    else
      @modified_row = @modified_row-1
    end
    break if j.to_i >= @row - 1
  }
  if focus_line
    # ricerchiamo una eventuale dichiarazione
      if focus_world && focus_world.strip.length > 0
        begin
          re = Regexp::new('[\s\n\t\;]('+focus_world.strip+')[\s\t]*=(.)*')
          source_array.each_with_index  do |line,j|
            #m = /[\s\n\t\;](#{focus_world})[\s\t]*=(.)*/.match(line)
            
            m = re.match("\s#{line}")
            if m
              @dec_line = line
              @class_dec_line_node = @ss.(j+1)
              break
            end
          end
      		rescue Exception => e
#            Arcadia.console(self, 'msg'=>e.inspect, 'level'=>'error')
        end
        
      end

      if @class_node
        to_iniect = "$SAFE = 3\n"
        if @class_dec_line_node && @class_dec_line_node.label == @class_node.label
          to_iniect = "#{to_iniect}#{declaration(@dec_line)}\n"
        end
        if focus_world.length > 0
          to_iniect = "#{to_iniect}#{dot_trip(focus_world)}\n"
        else
          to_iniect = "#{to_iniect}#{scope_trip}\n"
        end
        #to_iniect = "#{to_iniect}#{focus_line}\n"
        to_iniect_class = @class_node.label
      else
        to_iniect = ''
        to_iniect_class = ''
      end

      ss_source = @ss.scheletor_from_node(@ss.root,'',to_iniect, to_iniect_class)
      ss_source_array = ss_source.split("\n")
      ss_len = ss_source_array.length
      if ss_len>0 && ss_source_array[0].strip != focus_line.strip
        @modified_source = "#{@modified_source}#{ss_source}"
        if @class_node
          @modified_source = "#{@modified_source}#{@class_node.label.downcase} = #{@class_node.label}.new\n"
        end
      else
        ss_len = 0
        @class_node = nil
      end
      if @class_node
        @modified_row = @modified_row + @ss.injected_row
      else
        @modified_source = "#{@modified_source}$SAFE = 3\n"
        if @dec_line
          
          
          @modified_source = "#{@modified_source}#{declaration(@dec_line)}\n"
          @modified_row = @modified_row+1
        end
        #@modified_source = "#{@modified_source}_candidates=@candidates\n"
        if focus_world.length > 0
          @modified_source = "#{@modified_source}#{dot_trip(focus_world)}\n"
        else
          @modified_source = "#{@modified_source}#{scope_trip}\n"
        end
        #@modified_source = "#{@modified_source}@candidates=_candidates\n"

        
        #@modified_source = "#{@modified_source}#{focus_line}\n"
        @modified_row = @modified_row+1+ss_len
      end
  end
  if @filter.strip.length > 0 && !is_dot?
#        refresh_words(source_array)
      refresh_words
  end

#    Arcadia.console(self, 'msg'=>@modified_source)
#    Arcadia.console(self, 'msg'=>"@modified_row=#{@modified_row}")
#    Arcadia.console(self, 'msg'=>"focus_line=#{focus_line}") if focus_line
#    Arcadia.console(self, 'msg'=>"focus_world=#{focus_world}") if focus_world
#    Arcadia.console(self, 'msg'=>"@filter=#{@filter}") if @filter
#    Arcadia.console(self, 'msg'=>"@dec_line=#{@dec_line}") if @dec_line
#    Arcadia.console(self, 'msg'=>"declaration(@dec_line)=#{declaration(@dec_line)}") if @dec_line
end

#refresh_wordsObject



372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'ext/ae-editor/ae-editor.rb', line 372

def refresh_words
  @words.clear
  _re = /[\s\t\n"'(\[\{=><]#{@filter}[a-zA-Z0-9\-_]*/
  m = _re.match(@source)
  while m && (_txt=m.post_match)
    can = m[0].strip
    if  ['"','(','[','{',"'",'=','>','<'].include?(can[0..0])
      can = can[1..-1]
    end
    @words << can if can != @filter
    m = _re.match(_txt)
  end
end

#scope_tripObject



325
326
327
328
329
330
331
332
# File 'ext/ae-editor/ae-editor.rb', line 325

def scope_trip
  ret = "ObjectSpace.each_object(Class){|o|\n"
  ret = ret + " o_name = o.name\n"
  ret = ret + " print '#'+o_name+'\n' if o_name && o_name.strip.length>0 \n"
  ret = ret + "}\n"
  ret = ret + dot_trip('self')
  ret
end