Class: RubyCompleteCode

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

Constant Summary collapse

RESERVED_WORDS =
["__FILE__","and","def","end","in","or","self","unless",
"__LINE__","begin","defined?","ensure","module","redo","super","until",
"BEGIN","break","do","false","next","rescue","then","when",
"END","case","else","for","nil","retry","true","while",
"alias","class","elsif","if","not","return","undef","yield"]

Constants inherited from CompleteCode

CompleteCode::LEFT_CHARS

Instance Attribute Summary collapse

Attributes inherited from CompleteCode

#filter

Instance Method Summary collapse

Methods inherited from CompleteCode

#focus_word, #is_dot?, #refresh_words

Constructor Details

#initialize(_editor, _row, _col) ⇒ RubyCompleteCode

Returns a new instance of RubyCompleteCode.



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'ext/ae-editor/ae-editor.rb', line 594

def initialize(_editor, _row, _col)
#    @editor = _editor
#    @source = _editor.text_value
#    @row = _row.to_i
#    @col = _col.to_i
  super
  if _editor && (_editor.has_ctags? || OS.unix?)
    tmp_file = _editor.create_temp_file
    begin
      if _editor.has_ctags?
        @ss = RubyCtagsFileSourceStructure.new(tmp_file, _editor.ctags_string, _row.to_i)
      elsif OS.unix?
        @ss = RubyGrepFileSourceStructure.new(tmp_file, _row.to_i)
      end
    ensure
      File.delete(tmp_file)
    end
  else
    @ss = RubySourceStructureParser.new(@source)
  end
#    @filter=''
#    @words = Array.new
#    process_source
end

Instance Attribute Details

#modified_colObject (readonly)

Returns the value of attribute modified_col.



588
589
590
# File 'ext/ae-editor/ae-editor.rb', line 588

def modified_col
  @modified_col
end

#modified_rowObject (readonly)

Returns the value of attribute modified_row.



588
589
590
# File 'ext/ae-editor/ae-editor.rb', line 588

def modified_row
  @modified_row
end

Instance Method Details

#candidates(_show_error = false) ⇒ Object



833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# File 'ext/ae-editor/ae-editor.rb', line 833

def candidates(_show_error = false)
  super
  temp_file = create_modified_temp_file(@editor.file)
  begin
    Arcadia.is_windows??ruby='rubyw':ruby=Arcadia.ruby
    _cmp_s = "|#{ruby} '#{temp_file}' 2>&1"
    _ret = nil
    onlyw = []
    begin
      open(_cmp_s,"r") do  |f|
        _ret = f.readlines.collect!{| line | 
          #line.chomp
          anlyl = line.split('#')[1]
          onlyw << anlyl.strip if anlyl
          line.strip
        }
      end
    rescue
      _ret = [] 
    end
    if _ret.include?(':ARCADIA_CC_ERROR')
      _ret = [] 
    end
    if @filter.strip.length > 0 && !is_dot?
      @words.each{|w| 
        if !(onlyw.include?(w) || onlyw.include?("##{w}"))
          _ret << w
        end
      }
    end
    if !is_dot?
      RESERVED_WORDS.each{|w| 
        if !onlyw.include?(w) && !@words.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



879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
# File 'ext/ae-editor/ae-editor.rb', line 879

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



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'ext/ae-editor/ae-editor.rb', line 654

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



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'ext/ae-editor/ae-editor.rb', line 619

def dot_trip(_var_name)
#    ret = "_class=#{_var_name}.class.name\n"
  ret = "_class=#{_var_name}.class.to_s\n"
#    ret = ret +"_methods=#{_var_name}.methods\n"
  ret = ret +"_methods=#{_var_name}.public_methods(includeSuper=true)\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

#process_sourceObject

def is_dot?

  @is_dot
end


692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
# File 'ext/ae-editor/ae-editor.rb', line 692

def process_source
  super
  @modified_source = "begin\n"
  @modified_row = @row+1
  @modified_col = @col
  @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
  require_tk = false
  @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 !require_tk && line.strip[8..-1].include?("tk")
        require_tk = true
        #  @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 require_tk
    @modified_source = "#{@modified_source}Tk.root.destroy if Tk && Tk.root\n"
    @modified_row = @modified_row+1
  end
  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
  @modified_source = "#{@modified_source}rescue Exception => e\n"
  @modified_source = "#{@modified_source}  if e.class != SystemExit\n"
  @modified_source = "#{@modified_source}    p :ARCADIA_CC_ERROR\n"
  @modified_source = "#{@modified_source}    p e.message\n"
  @modified_source = "#{@modified_source}  end\n"
  @modified_source = "#{@modified_source}end\n"
  @modified_row = @modified_row+6
#    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

#scope_tripObject



645
646
647
648
649
650
651
652
# File 'ext/ae-editor/ae-editor.rb', line 645

def scope_trip
  ret = "ObjectSpace.each_object(Module){|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