-
Notifications
You must be signed in to change notification settings - Fork 23
/
ae-editor.rb
executable file
·5673 lines (5153 loc) · 162 KB
/
ae-editor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
232
233
234
235
236
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
332
333
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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
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
615
616
617
618
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
644
645
646
647
648
649
650
651
652
653
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
687
688
689
690
691
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
832
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# ae-editor.rb - Arcadia Ruby ide
# by Antonio Galeone <antonio-galeone@rubyforge.org>
#
# &require_dir_ref=../..
# &require_omissis=#{Dir.pwd}/conf/arcadia.init
require 'tk'
require 'tktext'
require "#{Dir.pwd}/lib/a-tkcommons"
#require 'lib/a-commons'
require "#{Dir.pwd}/lib/a-core"
require "#{Dir.pwd}/ext/ae-editor/lib/rbeautify"
class SourceTreeNode
attr_reader :sons
attr_reader :parent
attr_reader :kind
attr_accessor :rif, :rif_end, :label, :helptext, :sortable
def initialize(parent=nil, kind='class', sortable=true)
@sons = Array.new
@parent = parent
@kind = kind
@sortable = sortable
if @parent !=nil
@parent.sons << self
end
yield(self) if block_given?
end
def <=> (other)
if self.sortable && other.sortable
self.label.strip <=> other.label.strip
elsif !self.sortable
-1
elsif !other.sortable
1
end
end
end
class SourceStructure
attr_reader :root
def initialize
@root = SourceTreeNode.new(nil, 'root'){|_node|
_node.rif= 'root'
_node.label=''
}
end
def node_by_line(_from_node, _line)
_found_node = nil
_begin = _from_node.rif.to_i
_end = _from_node.rif_end.to_i
if _line.to_i <= _end && _line.to_i >= _begin
_found_node = _from_node
else
_sons = _from_node.sons
for inode in 0.._sons.length - 1
_son = _sons[inode]
_found_node = node_by_line(_son, _line)
break if _found_node
end
end
return _found_node
end
def deep_node_by_line(_from_node, _line, _found_node=nil)
_begin = _from_node.rif.to_i
_end = _from_node.rif_end.to_i
if _line.to_i <= _end && _line.to_i >= _begin
_found_node = _from_node
end
_sons = _from_node.sons
for inode in 0.._sons.length - 1
_son = _sons[inode]
_found_node = deep_node_by_line(_son, _line, _found_node)
end
return _found_node
end
def class_node_by_line(_line)
line_node = node_by_line(@root, _line)
class_node = line_node
while class_node != nil && class_node.kind != "class"
class_node = class_node.parent
end
return class_node
end
end
class CtagsSourceStructure < SourceStructure
SUPPORTED_LANG = ['Ant','Asm','Asp','Awk','Basic','BETA','C','C++','C#','Cobol','DosBatch','Eiffel','Erlang','Flex','Fortran','HTML','Java','JavaScript','Lisp','Lua','Make','MatLab','OCaml','Pascal','Perl','PHP','Python','REXX','Ruby','Scheme','Sh','SLang','SML','SQL','Tcl','Tex','Vera','Verilog','VHDL','Vim','YACC']
def initialize(_file, _ctags_string='ctags', _language=nil)
super()
@file = _file
@ctags_string = _ctags_string
@language = (_language.nil?)?nil:_language.capitalize
@classes = Hash.new
@last_root = @root
@last_class_node = @root
@last_node = @root
build_structure
end
def build_structure
output = ctags
output.each {|line|
b1,brest = line.split("/^")
b2,b3 = brest.split('$/;"')
name,file = b1.strip.split("\t")
definition = b2.strip
fields_raw = b3.strip.split("\t")
fields = Hash.new
fields_raw.each{|item|
k,v=item.split(":")
fields[k.strip]=v.strip if !k.nil? && !v.nil?
}
if fields['class'] != nil
parent = @classes[fields['class']]
elsif fields['interface'] != nil
parent = @classes[fields['interface']]
elsif ['method','singleton method'].include?(fields['kind'])
parent = @last_class_node
else
parent = @last_root
end
node = SourceTreeNode.new(parent, fields['kind'])
node.label = name
node.helptext = definition
node.rif = fields['line']
@last_node.rif_end = (node.rif.to_i-1).to_s
if ['class','module','interface','package'].include?(fields['kind'])
if fields['class'] != nil
@classes["#{fields['class']}.#{name}"]=node
else
@classes[name]=node
end
@last_class_node = node
end
node.sortable = !['package','field'].include?(fields['kind'])
@last_node = node
}
end
def ctags
if @language != nil && SUPPORTED_LANG.include?(@language)
@ctags_string = "#{@ctags_string} --language-force=#{@language}"
end
_cmd_ = "|#{@ctags_string} --fields=+a+f+m+i+k+K+n+s+S+t+z -uf - #{@file}"
to_ret = ''
begin
open(_cmd_, "r"){|f|
to_ret = f.readlines
}
rescue RuntimeError => e
Arcadia.runtime_error(e)
end
to_ret
end
end
class RubyCtagsSourceStructure < CtagsSourceStructure
attr_reader :injected_row
def initialize(_file, _ctags_string='ctags')
super(_file, _ctags_string, 'Ruby')
end
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}#{_son.helptext}\n"
elsif _son.kind == 'module'
_hinner_source = "#{_hinner_source}#{_son.helptext}\n"
elsif _son.kind == 'method' && _son.label != 'initialize'
_hinner_source = "#{_hinner_source} def #{_son.label}\n"
_hinner_source = "#{_hinner_source} end\n"
elsif _son.kind == 'singleton method'
_hinner_source = "#{_hinner_source} def #{_son.label}\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
end
class RubySourceStructure < SourceStructure
attr_reader :injected_row
def initialize(_source)
super()
parse_source(_source)
end
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
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
def classies
end
def modules
end
def class_methods(_class)
end
end
class SafeCompleteCode
attr_reader :modified_row, :modified_col
attr_reader :filter
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
# 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
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
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
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
def is_dot?
@is_dot
end
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
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
#-----
# if ["\s",'(','{','['].include?(focus_line[-1..-1])
# p "focus_line[-1..-1] e uguale a #{focus_line[-1..-1]}"
# focus_line = ''
# else
# focus_line_split = focus_line.split
# if focus_line_split && focus_line_split.length >0
# old_focus_line_length = focus_line.length
# focus_line = focus_line_split[-1]
# @col = @col - (old_focus_line_length = focus_line.length)
# end
# end
#---
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.class_node_by_line(@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) 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_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
# 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)
if j >= @row-1
break
else
m = re.match("\s#{line}")
if m
@dec_line = line
@class_dec_line_node = @ss.class_node_by_line(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
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
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
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
end
#class TkArcadiaText < TkText
#
# def insert(insert, chars,*tags)
# super(insert, chars,*tags)
# self.modified=true
# # event_generate("<Modified>")
# end
#end
class TkTextListBox < TkText
def initialize(parent=nil, keys={})
super(parent, keys)
wrap 'none'
tag_configure('selected','background' =>Arcadia.conf('hightlight.sel.background'),'borderwidth'=>1, 'relief'=>'raised')
tag_configure('class', 'foreground' => Arcadia.conf('hightlight.sel.foreground'))
@count = 0
@selected = -1
self.bind_append('KeyPress'){|e| key_press(e)}
self.bind_append('KeyRelease'){|e| key_release(e)}
self.bind_append("ButtonPress-1", proc{|x,y| button_press(x,y)}, "%x %y")
end
def insert(index, chars, *tags)
super(index, chars, *tags)
end
def add(chars)
meth_str, class_str = chars.split('-')
if meth_str && meth_str.strip.length>0 && class_str
insert('end', "#{meth_str}")
insert('end', "-#{class_str}\n", 'class')
elsif meth_str && meth_str.strip.length==0 && class_str
insert('end', "-#{class_str}\n")
else
insert('end', "#{chars}\n")
end
@count = @count+1
end
def clear
delete('1.0','end')
@count = 0
end
def button_press(x,y)
_index = self.index("@#{x},#{y}")
_line = _index.split('.')[0].to_i
self.select(_line)
end
def key_press(_e)
case _e.keysym
when 'Up'
if @selected > 0
select(@selected-1)
end
when 'Down'
if @selected < @count
select(@selected+1)
end
end
end
def key_release(_e)
case _e.keysym
when 'Next','Prior'
index = self.index('@0,0')
line = index.split('.')[0].to_i
select(line)
end
end
def selected_line
if @selected > 0
self.get("#{@selected}.0", "#{@selected}.0 lineend")
end
end
def select(_row)
self.tag_remove('selected', '1.0', 'end')
_start_index = "#{_row.to_s}.0"
_end_index = "#{_start_index} +1 lines linestart"
self.tag_add('selected', _start_index, _end_index)
self.set_insert(_start_index)
self.see(_start_index)
@selected = _row
end
end
class AgEditorOutlineToolbar
attr_accessor :sync
def initialize(_controller)
@controller = _controller
@panel = @controller.main_instance.frame(1).root.add_panel(@controller.main_instance.frame(1).name, "sync");
@cb_sync = TkCheckButton.new(@panel, Arcadia.style('checkbox').update('background'=>@panel.background)){
text 'Sync'
justify 'left'
indicatoron 0
offrelief 'flat'
image Arcadia.image_res(SYNC_GIF)
pack
}
Tk::BWidget::DynamicHelp::add(@cb_sync,
'text'=>'Link open editors with content in the Navigator')
do_check = proc {
if @cb_sync.cget('onvalue')==@cb_sync.cget('variable').value.to_i
sync_on
else
sync_off
end
#Arcadia.console(self, "msg"=>"@sync=#{@sync}" )
}
@sync = false
@cb_sync.command(do_check)
end
def sync_on
@sync = true
e = @controller.active_instance.raised
if e
#Arcadia.console(self, "msg"=>"@sync=#{@sync} file=#{e.file}" )
e.outline.select_without_event(e.outline.last_row) if e.outline.last_row
end
end
def sync_off
@sync = false
end
def is_sync_on?
@sync
end
end
class AgEditorOutline
attr_reader :last_row
attr_reader :tree_exp
attr_reader :ss
def initialize(_editor, _frame, _bar, _lang=nil)
@editor = _editor
@frame = _frame
@bar = _bar
@lang = _lang
initialize_tree(_frame)
end
def update_row(_row=0)
@last_row=_row
if @bar.is_sync_on?
select_without_event(_row)
end
end
# I think this is "if synced expand out the outline for the current selection"
def shure_select_node(_node)
return if @selecting_node
#return if @tree_exp.exist?(_node.rif)
@selecting_node = true
_proc = @tree_exp.selectcommand
@tree_exp.selectcommand(nil)
begin
@tree_exp.selection_clear
@tree_exp.selection_add(_node.rif)
@opened = false
to_open = @last_open_node
parent = _node.parent
while !parent.nil? && parent.rif != 'root'
@tree_exp.open_tree(parent.rif, false)
@opened = to_open==parent.rif || @opened
@last_open_node=parent.rif
parent = parent.parent
end
@tree_exp.close_tree(to_open) if to_open && !@opened
@tree_exp.see(_node.rif)
ensure
@tree_exp.selectcommand(_proc)
@selecting_node = false
end
@tree_exp.call_after_next_show_h_scroll(proc{Tk.update;@tree_exp.see(_node.rif)})
end
def select_without_event(_line)
if @ss
_node=@ss.deep_node_by_line(@ss.root, _line)
if _node && @tree_exp.exist?(_node.rif) && _node.rif!='root'
shure_select_node(_node)
end
end
end
def initialize_tree(_frame)
_tree_goto = proc{|_self|
sync_val = @bar.sync
@bar.sync=false
begin
#_line = _self.selection_get[0]
_line = _self.selected
_index =_line.to_s+'.0'
_hinner_text = @tree_exp.itemcget(_line,'text').strip
_editor_line = @editor.text.get(_index, _index+ ' lineend')
if !_editor_line.include?(_hinner_text)
Arcadia.console(self, 'msg'=>"... rebuild tree \n")
if @tree_thread && @tree_thread.alive?
@tree_thread.exit # kill the old tree
end
@tree_thread = Thread.new{
build_tree(_line)
Tk.update
}
_line = _self.selected
_index =_line.to_s+'.0'
end
@editor.text.set_focus
@editor.text.see(_index)
@editor.text.tag_remove('selected','1.0','end')
@editor.text.tag_add('selected',_line.to_s+'.0',(_line+1).to_s+'.0')
ensure
@bar.sync = sync_val
end
}
@tree_exp = BWidgetTreePatched.new(_frame, Arcadia.style('treepanel')){
showlines false
deltay 18
dragenabled true
selectcommand proc{ _tree_goto.call(self) }
crosscloseimage Arcadia.image_res(ARROWRIGHT_GIF)
crossopenimage Arcadia.image_res(ARROWDOWN_GIF)
}
@tree_exp.extend(TkScrollableWidget)
self.show
pop_up_menu_tree
end
def destroy
#@tree_scroll_wrapper.destroy
@tree_exp.hide
@tree_exp.destroy
Tk.update
end
def show
#@tree_scroll_wrapper.show(0,26)
@tree_exp.show(0,0)
Tk.update
end
def hide
@tree_exp.hide
#@tree_scroll_wrapper.hide
end
def build_tree_from_node(_node, _label_match=nil)
@image_class = Arcadia.image_res(TREE_NODE_CLASS_GIF)
@image_module = Arcadia.image_res(TREE_NODE_MODULE_GIF)
@image_method = Arcadia.image_res(TREE_NODE_METHOD_GIF)
@image_singleton_method = Arcadia.image_res(TREE_NODE_SINGLETON_METHOD_GIF)
_sorted_sons = _node.sons.sort
for inode in 0.._sorted_sons.length - 1
_son = _sorted_sons[inode]
if _son.kind == 'class'
_image = @image_class
elsif _son.kind == 'module'
_image = @image_module
elsif _son.kind == 'method' || _son.kind == 'procedure'
_image = @image_method
elsif _son.kind == 'singleton method'
_image = @image_singleton_method