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(_editor, _row, _col) ⇒ SafeCompleteCode

Returns a new instance of SafeCompleteCode.



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'ext/ae-editor/ae-editor.rb', line 355

def initialize(_editor, _row, _col)
  @editor = _editor
  @source = _editor.text_value
  #@file = _file
  @row = _row.to_i
  @col = _col.to_i
  if _editor && _editor.has_ctags?
    tmp_file = _editor.create_temp_file
    begin
      @ss = RubyCtagsSourceStructure.new(tmp_file, _editor.ctags_string)
    ensure
      File.delete(tmp_file)
    end
  else
    @ss = RubySourceStructure.new(_source)
  end
  @filter=''
  @words = Array.new
  process_source
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



354
355
356
# File 'ext/ae-editor/ae-editor.rb', line 354

def filter
  @filter
end

#modified_colObject (readonly)

Returns the value of attribute modified_col.



353
354
355
# File 'ext/ae-editor/ae-editor.rb', line 353

def modified_col
  @modified_col
end

#modified_rowObject (readonly)

Returns the value of attribute modified_row.



353
354
355
# File 'ext/ae-editor/ae-editor.rb', line 353

def modified_row
  @modified_row
end

Instance Method Details

#candidates(_show_error = false) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'ext/ae-editor/ae-editor.rb', line 632

def candidates(_show_error = false)
  temp_file = create_modified_temp_file(@editor.file)
  begin
    Arcadia.is_windows??ruby='rubyw':ruby=Arcadia.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.runtime_error(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



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'ext/ae-editor/ae-editor.rb', line 660

def create_modified_temp_file(_base_file=nil)
  if _base_file
  File.basename(_base_file)
    _file = File.join(File.dirname(_base_file),'~~'+File.basename(_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



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
# File 'ext/ae-editor/ae-editor.rb', line 409

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



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'ext/ae-editor/ae-editor.rb', line 376

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



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'ext/ae-editor/ae-editor.rb', line 616

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)


443
444
445
# File 'ext/ae-editor/ae-editor.rb', line 443

def is_dot?
  @is_dot
end

#process_sourceObject



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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'ext/ae-editor/ae-editor.rb', line 461

def process_source
  @modified_source = ""
  @modified_row = @row
  @modified_col = @col
  source_array = @source.split("\n")
  focus_line = source_array[@row-1]
  focus_line = focus_line[0..@col] if focus_line
  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)
  focus_line_to_evaluate = focus_line
  @modified_source = "#{@modified_source}Dir.chdir('#{File.dirname(@editor.file)}')\n" if @editor.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) include 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
    # 2) include evalueting row with a $SAFE 3
    elsif j.to_i == @row-1
      focus_line_to_evaluate = line
      break
    # 3) eliminiamo la riga
    else
      @modified_row = @modified_row-1
    end
    break if j.to_i >= @row - 1
  }
  if focus_line_to_evaluate
    # search an optional declaration
      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)
            if j >= @row-1
              break
            else
              m = re.match("\s#{line}")
              if m
                @dec_line = line
                @class_dec_line_node = @ss.(j+1)
                break
              end
            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_to_evaluate.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
  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



447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'ext/ae-editor/ae-editor.rb', line 447

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



400
401
402
403
404
405
406
407
# File 'ext/ae-editor/ae-editor.rb', line 400

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