@@ -4,11 +4,7 @@ def initialize
44 @elements = [ ]
55 @line_height = $app. line_height
66 @line_space = $app. line_space
7- @positiontable = Hash . new { |this_hash , missing_key |
8- found_key = this_hash . keys . find { |this_key |
9- this_key . class == Range && this_key . include? ( missing_key ) }
10- found_key ? this_hash [ missing_key ] = this_hash [ found_key ] : :undefined
11- } #source: http://www.developwithpurpose.com/ruby-hash-awesomeness-part-2/
7+ @positiontable = generate_hashtable
128 initialize_positiontable
139 end
1410
@@ -28,13 +24,17 @@ def position(x,y)
2824 [ $app. start_of_editor_text , y ]
2925 end
3026
31- def new_or_next_line ( cursor )
32- if last_line_of_editor ( cursor . y_position )
33- update_positiontable_with_new_line
34- cursor . go_to_new_line
35- else
27+ def make_new_line ( cursor )
28+ update_positiontable_with_new_line ( cursor )
3629 cursor . go_to_new_line
37- end
30+ end
31+
32+ def previous_line ( cursor )
33+ cursor . go_to_previous_line unless first_line_of_editor ( cursor . y_position )
34+ end
35+
36+ def next_line ( cursor )
37+ cursor . go_to_new_line unless last_line_of_editor ( cursor . y_position )
3838 end
3939
4040 def elements
@@ -45,39 +45,62 @@ def current_content(y)
4545 @positiontable [ y ] . content
4646 end
4747
48- def delete_last_key ( y )
49- @positiontable [ y ] . delete
48+ def delete_last_key ( cursor )
49+ y = cursor . y_position
50+ if @positiontable [ y ] . content . empty?
51+ cursor . go_to_previous_line unless first_line_of_editor ( y )
52+ else
53+ @positiontable [ y ] . delete
54+ end
5055 end
5156
5257
5358 private
54-
5559 def initialize_positiontable
60+ str_obj = StringObject . new ( "" )
61+ @elements << str_obj
5662 begin_of_line = 0
5763 line_range = [ begin_of_line ..@line_height + @line_space ]
58- string_object = StringObject . new ( "" )
59- string_object . set_position = line_range
60- @elements << { line_range [ 0 ] => string_object }
61- @elements . each do |e | #should wok with first
62- @positiontable . update ( e )
63- end
64+ str_obj . set_position = line_range
65+ @positiontable . update ( { line_range [ 0 ] => str_obj } )
6466 end
6567
6668 def last_line_of_editor ( y )
67- @positiontable [ y ] == @elements . last . to_a . last . last #ugly
69+ @positiontable [ y ] == @elements . last
6870 end
6971
70- def update_positiontable_with_new_line
71- #create_line_element with position range
72- begin_of_line = @line_height + @line_space
73- begin_of_line *= @elements . count if @elements
74- line_range = [ begin_of_line ..begin_of_line + @line_height + @line_space ]
72+ def first_line_of_editor ( y )
73+ @positiontable [ y ] == @elements . first
74+ end
75+
76+ def update_positiontable_with_new_line ( cursor )
77+ @new_positiontable = generate_hashtable #make new one becouse of content generated on mouseover
7578 string_object = StringObject . new ( "" )
76- string_object . set_position = line_range
77- @elements << { line_range [ 0 ] => string_object }
78- @elements . each do |e |
79- @positiontable . update ( e )
79+ position = line_number ( on_position_of ( cursor ) )
80+ @elements . insert ( position , string_object )
81+ @elements . each_with_index do |e , idx |
82+ line_range = find_position_range ( idx )
83+ e . set_position = line_range
84+ @new_positiontable . update ( { line_range [ 0 ] => e } )
8085 end
86+ @positiontable = @new_positiontable
87+ end
88+
89+ def generate_hashtable
90+ Hash . new { |this_hash , missing_key |
91+ found_key = this_hash . keys . find { |this_key |
92+ this_key . class == Range && this_key . include? ( missing_key ) }
93+ found_key ? this_hash [ missing_key ] = this_hash [ found_key ] : :undefined
94+ } #source: http://www.developwithpurpose.com/ruby-hash-awesomeness-part-2/
95+ end
96+
97+ def find_position_range ( idx )
98+ begin_of_line = ( @line_height + @line_space ) * idx
99+ line_range = [ begin_of_line ..begin_of_line + @line_height + @line_space ]
100+ return line_range
81101 end
82102
103+ def line_number ( str_obj ) #begins with 0!!
104+ str_obj . position . first . last /( @line_height + @line_space )
105+ end
83106end
0 commit comments