show count of test failures
[lines.love.git] / select.lua
blobefb69091bb7db31a5508977a6009baf46869b325
1 -- helpers for selecting portions of text
3 -- Return any intersection of the region from State.selection1 to State.cursor1 (or
4 -- current mouse, if mouse is pressed; or recent mouse if mouse is pressed and
5 -- currently over a drawing) with the region between {line=line_index, pos=apos}
6 -- and {line=line_index, pos=bpos}.
7 -- apos must be less than bpos. However State.selection1 and State.cursor1 can be in any order.
8 -- Result: positions spos,epos between apos,bpos.
9 function Text.clip_selection(State, line_index, apos, bpos)
10 if State.selection1.line == nil then return nil,nil end
11 -- min,max = sorted(State.selection1,State.cursor1)
12 local minl,minp = State.selection1.line,State.selection1.pos
13 local maxl,maxp
14 if App.mouse_down(1) then
15 maxl,maxp = Text.mouse_pos(State)
16 else
17 maxl,maxp = State.cursor1.line,State.cursor1.pos
18 end
19 if Text.lt1({line=maxl, pos=maxp},
20 {line=minl, pos=minp}) then
21 minl,maxl = maxl,minl
22 minp,maxp = maxp,minp
23 end
24 -- check if intervals are disjoint
25 if line_index < minl then return nil,nil end
26 if line_index > maxl then return nil,nil end
27 if line_index == minl and bpos <= minp then return nil,nil end
28 if line_index == maxl and apos >= maxp then return nil,nil end
29 -- compare bounds more carefully (start inclusive, end exclusive)
30 local a_ge = Text.le1({line=minl, pos=minp}, {line=line_index, pos=apos})
31 local b_lt = Text.lt1({line=line_index, pos=bpos}, {line=maxl, pos=maxp})
32 --? print(minl,line_index,maxl, '--', minp,apos,bpos,maxp, '--', a_ge,b_lt)
33 if a_ge and b_lt then
34 -- fully contained
35 return apos,bpos
36 elseif a_ge then
37 assert(maxl == line_index)
38 return apos,maxp
39 elseif b_lt then
40 assert(minl == line_index)
41 return minp,bpos
42 else
43 assert(minl == maxl and minl == line_index)
44 return minp,maxp
45 end
46 end
48 -- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line
49 -- Creates text objects every time, so use this sparingly.
50 -- Returns some intermediate computation useful elsewhere.
51 function Text.draw_highlight(State, line, x,y, pos, lo,hi)
52 if lo then
53 local lo_offset = Text.offset(line.data, lo)
54 local hi_offset = Text.offset(line.data, hi)
55 local pos_offset = Text.offset(line.data, pos)
56 local lo_px
57 if pos == lo then
58 lo_px = 0
59 else
60 local before = line.data:sub(pos_offset, lo_offset-1)
61 lo_px = App.width(before)
62 end
63 --? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
64 local s = line.data:sub(lo_offset, hi_offset-1)
65 App.color(Highlight_color)
66 love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height)
67 App.color(Text_color)
68 return lo_px
69 end
70 end
72 -- inefficient for some reason, so don't do it on every frame
73 function Text.mouse_pos(State)
74 local time = love.timer.getTime()
75 if State.recent_mouse.time and State.recent_mouse.time > time-0.1 then
76 return State.recent_mouse.line, State.recent_mouse.pos
77 end
78 State.recent_mouse.time = time
79 local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y())
80 if line then
81 State.recent_mouse.line = line
82 State.recent_mouse.pos = pos
83 end
84 return State.recent_mouse.line, State.recent_mouse.pos
85 end
87 function Text.to_pos(State, x,y)
88 for line_index,line in ipairs(State.lines) do
89 if line.mode == 'text' then
90 if Text.in_line(State, line_index, x,y) then
91 return line_index, Text.to_pos_on_line(State, line_index, x,y)
92 end
93 end
94 end
95 end
97 function Text.cut_selection(State)
98 if State.selection1.line == nil then return end
99 local result = Text.selection(State)
100 Text.delete_selection(State)
101 return result
104 function Text.delete_selection(State)
105 if State.selection1.line == nil then return end
106 local minl,maxl = minmax(State.selection1.line, State.cursor1.line)
107 local before = snapshot(State, minl, maxl)
108 Text.delete_selection_without_undo(State)
109 record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
112 function Text.delete_selection_without_undo(State)
113 if State.selection1.line == nil then return end
114 -- min,max = sorted(State.selection1,State.cursor1)
115 local minl,minp = State.selection1.line,State.selection1.pos
116 local maxl,maxp = State.cursor1.line,State.cursor1.pos
117 if minl > maxl then
118 minl,maxl = maxl,minl
119 minp,maxp = maxp,minp
120 elseif minl == maxl then
121 if minp > maxp then
122 minp,maxp = maxp,minp
125 -- update State.cursor1 and State.selection1
126 State.cursor1.line = minl
127 State.cursor1.pos = minp
128 if Text.lt1(State.cursor1, State.screen_top1) then
129 State.screen_top1.line = State.cursor1.line
130 State.screen_top1.pos = Text.pos_at_start_of_screen_line(State, State.cursor1)
132 State.selection1 = {}
133 -- delete everything between min (inclusive) and max (exclusive)
134 Text.clear_screen_line_cache(State, minl)
135 local min_offset = Text.offset(State.lines[minl].data, minp)
136 local max_offset = Text.offset(State.lines[maxl].data, maxp)
137 if minl == maxl then
138 --? print('minl == maxl')
139 State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..State.lines[minl].data:sub(max_offset)
140 return
142 assert(minl < maxl)
143 local rhs = State.lines[maxl].data:sub(max_offset)
144 for i=maxl,minl+1,-1 do
145 table.remove(State.lines, i)
146 table.remove(State.line_cache, i)
148 State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..rhs
151 function Text.selection(State)
152 if State.selection1.line == nil then return end
153 -- min,max = sorted(State.selection1,State.cursor1)
154 local minl,minp = State.selection1.line,State.selection1.pos
155 local maxl,maxp = State.cursor1.line,State.cursor1.pos
156 if minl > maxl then
157 minl,maxl = maxl,minl
158 minp,maxp = maxp,minp
159 elseif minl == maxl then
160 if minp > maxp then
161 minp,maxp = maxp,minp
164 local min_offset = Text.offset(State.lines[minl].data, minp)
165 local max_offset = Text.offset(State.lines[maxl].data, maxp)
166 if minl == maxl then
167 return State.lines[minl].data:sub(min_offset, max_offset-1)
169 assert(minl < maxl)
170 local result = {State.lines[minl].data:sub(min_offset)}
171 for i=minl+1,maxl-1 do
172 if State.lines[i].mode == 'text' then
173 table.insert(result, State.lines[i].data)
176 table.insert(result, State.lines[maxl].data:sub(1, max_offset-1))
177 return table.concat(result, '\n')