Merge luaML.love

This one was hard until I realized I'm relying more on editor.width, and
have to remember to keep it up to date.
This commit is contained in:
Kartik K. Agaram 2023-04-02 17:10:21 -07:00
commit d91d98fc27
18 changed files with 88 additions and 156 deletions

View File

@ -8,10 +8,9 @@ box_height = function(node)
end
node.editor.line_cache[i].fragments = nil
node.editor.line_cache[i].screen_line_starting_pos = nil
Text.compute_fragments(node.editor, i)
Text.populate_screen_line_starting_pos(node.editor, i)
y = y + node.editor.line_height*#node.editor.line_cache[i].screen_line_starting_pos
Text.clear_screen_line_cache(node.editor, i)
end
return y
end
end

View File

@ -1,10 +1,10 @@
add_hotkey_to_menu = function(s)
local s_text = to_hud_text(s)
local width = App.width(s_text)
local width = s_text:getWidth()
if Menu_cursor > App.screen.width - 30 then
return
end
App.color(Menu_command_color)
App.screen.draw(s_text, Menu_cursor,5)
Menu_cursor = Menu_cursor + width + 30
end
end

View File

@ -1,7 +1,7 @@
manifest_coord = function(index)
local y,x = Menu_bar_height, 5
for i,definition in ipairs(Manifest_navigator.candidates) do
local width = App.width(to_hud_text(definition))
local width = to_hud_text(definition):getWidth()
if x + width > App.screen.width - 5 then
y = y + HUD_line_height
x = 5
@ -11,4 +11,4 @@ manifest_coord = function(index)
end
x = x + width + 30
end
end
end

View File

@ -2,7 +2,7 @@ manifest_index = function(fy, fx, fwidth)
local y,x = Menu_bar_height, 5
local best_guess, best_guess_x, best_guess_width
for i,definition in ipairs(Manifest_navigator.candidates) do
local width = App.width(to_hud_text(definition))
local width = to_hud_text(definition):getWidth()
if x + width > App.screen.width - 5 then
y = y + HUD_line_height
x = 5
@ -21,4 +21,4 @@ manifest_index = function(fy, fx, fwidth)
x = x + width + 30
end
return best_guess
end
end

View File

@ -1,6 +1,6 @@
add_def_to_menu = function(x,y, s, cursor_highlight)
local s_text = to_hud_text(s)
local width = App.width(s_text)
local width = s_text:getWidth()
if x + width > App.screen.width - 5 then
y = y + HUD_line_height + --[[highlight padding]] 5
x = 5
@ -18,4 +18,4 @@ add_def_to_menu = function(x,y, s, cursor_highlight)
App.screen.draw(s_text, x,y)
x = x + width + 30
return x,y
end
end

View File

@ -2,7 +2,7 @@ num_lines_for_manifest_navigator = function(candidates)
local result = 1
local x = 5
for i,def in ipairs(candidates) do
local width = App.width(to_hud_text(def))
local width = to_hud_text(def):getWidth()
if x + width > App.screen.width - 5 then
result = result+1
x = 5
@ -10,4 +10,4 @@ num_lines_for_manifest_navigator = function(candidates)
x = x + width + 30
end
return result
end
end

View File

@ -2,7 +2,7 @@ draw_manifest_navigator = function()
App.color(Menu_command_color)
local filter_text = to_hud_text(Manifest_navigator.filter)
App.screen.draw(filter_text, 5, 5)
draw_cursor(5 + App.width(filter_text), 5)
draw_cursor(5 + filter_text:getWidth(), 5)
App.color(Menu_background_color)
love.graphics.rectangle('fill', 0,Menu_bar_height, App.screen.width, Manifest_navigator.num_lines * (HUD_line_height + --[[highlight padding]]5))
local x,y = 5, Menu_bar_height

View File

@ -5,8 +5,9 @@ initialize_editor = function(obj)
local scaled_lineheight = math.floor(scaled_fontsize*1.3)
-- HACK: modify obj.editor.left after initialization
obj.editor = edit.initialize_state(Menu_bar_height + vy(obj.y), math.floor(vx(obj.x)), math.ceil(vx(obj.x+obj.w)), scaled_fontsize, scaled_lineheight)
obj.editor.left = obj.editor.left + Line_number_width*App.width(obj.editor.em)
obj.editor.left = obj.editor.left + Line_number_width*App.width('m')
obj.editor.width = obj.editor.right - obj.editor.left
obj.editor.lines = load_array(obj.data)
Text.redraw_all(obj.editor)
end
end
end

View File

@ -8,10 +8,9 @@ box_height = function(node)
end
node.editor.line_cache[i].fragments = nil
node.editor.line_cache[i].screen_line_starting_pos = nil
Text.compute_fragments(node.editor, i)
Text.populate_screen_line_starting_pos(node.editor, i)
y = y + 20*1.3*#node.editor.line_cache[i].screen_line_starting_pos
Text.clear_screen_line_cache(node.editor, i)
end
return y
end
end

View File

@ -13,6 +13,7 @@ update_editor_box = function(node, preserve_screen_top_of_cursor_node)
if node.editor.font_height ~= scale(20) then
edit.update_font_settings(node.editor, scale(20))
end
node.editor.left = math.floor(vx(node.x)) + Line_number_width*App.width(node.editor.em)
node.editor.left = math.floor(vx(node.x)) + Line_number_width*App.width('m')
node.editor.right = math.ceil(vx(node.x+node.w))
end
node.editor.width = node.editor.right - node.editor.left
end

14
app.lua
View File

@ -53,9 +53,7 @@ function love.run()
App.screen.move = love.window.setPosition
App.screen.position = love.window.getPosition
App.screen.print = love.graphics.print
App.newText = love.graphics.newText
App.screen.draw = love.graphics.draw
App.width = function(text) return text:getWidth() end
App.open_for_reading = function(filename) return io.open(filename, 'r') end
App.open_for_writing = function(filename) return io.open(filename, 'w') end
App.getTime = love.timer.getTime
@ -270,16 +268,8 @@ function App.wait_fake_time(t)
App.time = App.time + t
end
-- LÖVE's Text primitive retains no trace of the string it was created from,
-- so we'll wrap it for our tests.
--
-- This implies that we need to hook any operations we need on Text objects.
function App.newText(font, s)
return {type='text', data=s, text=love.graphics.newText(font, s)}
end
function App.width(text)
return text.text:getWidth()
return love.graphics.getFont():getWidth(text)
end
function App.screen.draw(obj, x,y)
@ -432,6 +422,8 @@ function App.run_tests(record_error_fn)
App = App_for_tests
local saved_font = love.graphics.getFont()
love.graphics.setFont(Love_snapshot.initial_font)
--? App.initialize_for_test()
--? test_initial_state()
for _,name in ipairs(sorted_names) do
print(name)
App.initialize_for_test()

View File

@ -28,7 +28,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
-- rendering wrapped text lines needs some additional short-lived data per line:
-- startpos, the index of data the line starts rendering from, can only be >1 for topmost line on screen
-- starty, the y coord in pixels the line starts rendering from
-- fragments: snippets of rendered love.graphics.Text, guaranteed to not straddle screen lines
-- fragments: snippets of the line guaranteed to not straddle screen lines
-- screen_line_starting_pos: optional array of grapheme indices if it wraps over more than one screen line
line_cache = {},
@ -59,7 +59,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
font_height = font_height,
line_height = line_height,
em = App.newText(love.graphics.getFont(), 'm'), -- widest possible character width
top = top,
left = math.floor(left),
@ -72,7 +71,6 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
-- search
search_term = nil,
search_text = nil,
search_backup = nil, -- stuff to restore when cancelling search
}
return result
@ -204,7 +202,6 @@ function edit.text_input(State, t)
--? print('text input', t)
if State.search_term then
State.search_term = State.search_term..t
State.search_text = nil
Text.search_next(State)
else
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
@ -245,20 +242,17 @@ function edit.keychord_press(State, chord, key)
for _,line_cache in ipairs(State.line_cache) do line_cache.starty = nil end -- just in case we scroll
if chord == 'escape' then
State.search_term = nil
State.search_text = nil
State.cursor1 = State.search_backup.cursor
State.screen_top1 = State.search_backup.screen_top
State.search_backup = nil
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
elseif chord == 'return' then
State.search_term = nil
State.search_text = nil
State.search_backup = nil
elseif chord == 'backspace' then
local len = utf8.len(State.search_term)
local byte_offset = Text.offset(State.search_term, len)
State.search_term = string.sub(State.search_term, 1, byte_offset-1)
State.search_text = nil
elseif chord == 'down' then
State.cursor1.pos = State.cursor1.pos+1
Text.search_next(State)
@ -272,7 +266,6 @@ function edit.keychord_press(State, chord, key)
cursor={line=State.cursor1.line, pos=State.cursor1.pos},
screen_top={line=State.screen_top1.line, pos=State.screen_top1.pos},
}
assert(State.search_text == nil)
-- zoom
elseif chord == 'C-=' then
edit.update_font_settings(State, State.font_height+2)
@ -359,7 +352,6 @@ function edit.update_font_settings(State, font_height)
State.font_height = font_height
love.graphics.setFont(love.graphics.newFont(State.font_height))
State.line_height = math.floor(font_height*1.3)
State.em = App.newText(love.graphics.getFont(), 'm')
Text_cache = {}
Text.redraw_all(State)
end

View File

@ -120,7 +120,7 @@ function load_settings()
App.screen.width, App.screen.height, App.screen.flags = App.screen.size()
-- shrink height slightly to account for window decoration
App.screen.height = App.screen.height-100
App.screen.width = 40*App.width(App.newText(love.graphics.getFont(), 'm'))
App.screen.width = 40*App.width('m')
App.screen.flags.resizable = true
App.screen.flags.minwidth = math.min(App.screen.width, 200)
App.screen.flags.minheight = math.min(App.screen.height, 200)
@ -216,15 +216,7 @@ end
-- use this sparingly
function to_hud_text(s)
if HUD_text_cache[s] == nil then
HUD_text_cache[s] = App.newText(HUD_font, s)
HUD_text_cache[s] = love.graphics.newText(HUD_font, s)
end
return HUD_text_cache[s]
end
-- use this sparingly
function to_text(s)
if Text_cache[s] == nil then
Text_cache[s] = App.newText(love.graphics.getFont(), s)
end
return Text_cache[s]
end

View File

@ -13,10 +13,7 @@ function Text.draw_search_bar(State)
love.graphics.rectangle('line', 20, y-6, App.screen.width-40, h+2, 2,2)
App.color(Text_color)
App.screen.print(State.search_term, 25,y-5)
if State.search_text == nil then
State.search_text = App.newText(love.graphics.getFont(), State.search_term)
end
Text.draw_cursor(State, 25+App.width(State.search_text),y-5, Cursor_color, Text_color)
Text.draw_cursor(State, 25+App.width(State.search_term),y-5, Cursor_color, Text_color)
end
function Text.search_next(State)

View File

@ -58,15 +58,12 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi)
lo_px = 0
else
local before = line.data:sub(pos_offset, lo_offset-1)
local before_text = App.newText(love.graphics.getFont(), before)
lo_px = App.width(before_text)
lo_px = App.width(before)
end
--? print(lo,pos,hi, '--', lo_offset,pos_offset,hi_offset, '--', lo_px)
local s = line.data:sub(lo_offset, hi_offset-1)
local text = App.newText(love.graphics.getFont(), s)
local text_width = App.width(text)
App.color(Highlight_color)
love.graphics.rectangle('fill', x+lo_px,y, text_width,State.line_height)
love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height)
App.color(Text_color)
return lo_px
end

154
text.lua
View File

@ -14,68 +14,67 @@ function Text.draw(State, line_index, y, startpos, fg, hide_cursor)
line_cache.starty = y
line_cache.startpos = startpos
-- wrap long lines
local x = State.left
local pos = 1
local screen_line_starting_pos = startpos
Text.compute_fragments(State, line_index)
local final_screen_line_starting_pos = startpos -- track value to return
Text.populate_screen_line_starting_pos(State, line_index)
App.color(Line_number_color)
love.graphics.print(line_index, State.left-Line_number_width*App.width(State.em)+10,y)
love.graphics.print(line_index, State.left-Line_number_width*App.width('m')+10,y)
if fg == nil then
initialize_color()
end
for _, f in ipairs(line_cache.fragments) do
local frag, frag_text = f.data, f.text
if fg then
App.color(fg)
else
select_color(frag)
end
local frag_len = utf8.len(frag)
--? print('text.draw:', frag, 'at', line_index,pos, 'after', x,y)
assert(#line_cache.screen_line_starting_pos >= 1)
for i=1,#line_cache.screen_line_starting_pos do
local pos = line_cache.screen_line_starting_pos[i]
if pos < startpos then
-- render nothing
--? print('skipping', frag)
--? print('skipping', f)
else
final_screen_line_starting_pos = pos
local f = Text.screen_line(line, line_cache, i)
--? print('text.draw:', f, 'at', line_index,pos, 'after', x,y)
local frag_len = utf8.len(f)
-- render fragment
local frag_width = App.width(frag_text)
if x + frag_width > State.right then
assert(x > State.left) -- no overfull lines
y = y + State.line_height
if y + State.line_height > App.screen.height then
return y, screen_line_starting_pos
end
screen_line_starting_pos = pos
x = State.left
end
if State.selection1.line then
local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
Text.draw_highlight(State, line, x,y, pos, lo,hi)
Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
end
App.screen.draw(frag_text, x,y)
if fg then
App.color(fg)
else
select_color(f)
end
App.screen.print(f, State.left,y)
-- render cursor if necessary
if line_index == State.cursor1.line then
if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
if State.search_term then
if State.lines[State.cursor1.line].data:sub(State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)-1) == State.search_term then
local lo_px = Text.draw_highlight(State, line, x,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term))
local lo_px = Text.draw_highlight(State, line, State.left,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term))
App.color(fg or Text_color)
love.graphics.print(State.search_term, x+lo_px,y)
love.graphics.print(State.search_term, State.left+lo_px,y)
end
elseif not hide_cursor then
Text.draw_cursor(State, x+Text.x(frag, State.cursor1.pos-pos+1), y, Cursor_color, fg or Text_color)
Text.draw_cursor(State, State.left+Text.x(f, State.cursor1.pos-pos+1), y, Cursor_color, fg or Text_color)
end
end
end
x = x + frag_width
end
pos = pos + frag_len
end
if not hide_cursor and State.search_term == nil then
if line_index == State.cursor1.line and State.cursor1.pos == pos then
Text.draw_cursor(State, x, y, Cursor_color, fg or Text_color)
y = y + State.line_height
if y >= App.screen.height then
break
end
end
end
return y, screen_line_starting_pos
return y - State.line_height, final_screen_line_starting_pos
end
function Text.screen_line(line, line_cache, i)
local pos = line_cache.screen_line_starting_pos[i]
local offset = Text.offset(line.data, pos)
if i >= #line_cache.screen_line_starting_pos then
return line.data:sub(offset)
end
local endpos = line_cache.screen_line_starting_pos[i+1]-1
local end_offset = Text.offset(line.data, endpos)
return line.data:sub(offset, end_offset)
end
function Text.draw_cursor(State, x, y, cursor_color, reset_color)
@ -95,67 +94,34 @@ function Text.populate_screen_line_starting_pos(State, line_index)
if line_cache.screen_line_starting_pos then
return
end
-- duplicate some logic from Text.draw
Text.compute_fragments(State, line_index)
line_cache.screen_line_starting_pos = {1}
local x = State.left
local x = 0
local pos = 1
for _, f in ipairs(line_cache.fragments) do
local frag, frag_text = f.data, f.text
-- render fragment
local frag_width = App.width(frag_text)
if x + frag_width > State.right then
x = State.left
table.insert(line_cache.screen_line_starting_pos, pos)
end
x = x + frag_width
local frag_len = utf8.len(frag)
pos = pos + frag_len
end
end
function Text.compute_fragments(State, line_index)
--? print('compute_fragments', line_index, 'between', State.left, State.right)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
if line_cache.fragments then
return
end
line_cache.fragments = {}
local x = State.left
-- try to wrap at word boundaries
for frag in line.data:gmatch('%S*%s*') do
local frag_text = App.newText(love.graphics.getFont(), frag)
local frag_width = App.width(frag_text)
--? print('x: '..tostring(x)..'; frag_width: '..tostring(frag_width)..'; '..tostring(State.right-x)..'px to go')
while x + frag_width > State.right do
--? print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x))
if (x-State.left) < 0.8 * (State.right-State.left) then
--? print('splitting')
local frag_width = App.width(frag)
--? print('-- frag:', frag, pos, x, frag_width, State.width)
while x + frag_width > State.width do
--? print('frag:', frag, pos, x, frag_width, State.width)
if x < 0.8 * State.width then
-- long word; chop it at some letter
-- We're not going to reimplement TeX here.
local bpos = Text.nearest_pos_less_than(frag, State.right - x)
--? print('bpos', bpos)
if bpos == 0 then break end -- avoid infinite loop when window is too narrow
local bpos = Text.nearest_pos_less_than(frag, State.width - x)
-- everything works if bpos == 0, but is a little inefficient
pos = pos + bpos
local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
--? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes')
local frag1 = string.sub(frag, 1, boffset-1)
local frag1_text = App.newText(love.graphics.getFont(), frag1)
local frag1_width = App.width(frag1_text)
--? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px')
assert(x + frag1_width <= State.right)
table.insert(line_cache.fragments, {data=frag1, text=frag1_text})
frag = string.sub(frag, boffset)
frag_text = App.newText(love.graphics.getFont(), frag)
frag_width = App.width(frag_text)
--? if bpos > 0 then
--? print('after chop:', frag)
--? end
frag_width = App.width(frag)
end
x = State.left -- new line
end
if #frag > 0 then
--? print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px')
table.insert(line_cache.fragments, {data=frag, text=frag_text})
--? print('screen line:', pos)
table.insert(line_cache.screen_line_starting_pos, pos)
x = 0 -- new screen line
end
x = x + frag_width
pos = pos + utf8.len(frag)
end
end
@ -694,8 +660,7 @@ function Text.screen_line_width(State, line_index, i)
else
screen_line = string.sub(line.data, start_pos)
end
local screen_line_text = App.newText(love.graphics.getFont(), screen_line)
return App.width(screen_line_text)
return App.width(screen_line)
end
function Text.screen_line_index(screen_line_starting_pos, pos)
@ -783,15 +748,13 @@ function Text.x_after(s, pos)
local offset = Text.offset(s, math.min(pos+1, #s+1))
local s_before = s:sub(1, offset-1)
--? print('^'..s_before..'$')
local text_before = App.newText(love.graphics.getFont(), s_before)
return App.width(text_before)
return App.width(s_before)
end
function Text.x(s, pos)
local offset = Text.offset(s, pos)
local s_before = s:sub(1, offset-1)
local text_before = App.newText(love.graphics.getFont(), s_before)
return App.width(text_before)
return App.width(s_before)
end
function Text.to2(State, loc1)
@ -922,7 +885,6 @@ function Text.redraw_all(State)
end
function Text.clear_screen_line_cache(State, line_index)
State.line_cache[line_index].fragments = nil
State.line_cache[line_index].screen_line_starting_pos = nil
end

View File

@ -952,7 +952,7 @@ function test_pagedown_can_start_from_middle_of_long_wrapping_line()
y = y + Editor_state.line_height
App.screen.check(y, 'jkl ', 'screen:2')
y = y + Editor_state.line_height
App.screen.check(y, 'mno ', 'screen:3')
App.screen.check(y, 'mn', 'screen:3')
end
function test_pagedown_never_moves_up()

View File

@ -58,7 +58,7 @@ function snapshot(State, s,e)
-- deep copy lines without cached stuff like text fragments
for i=s,e do
local line = State.lines[i]
table.insert(event.lines, {data=line.data})
table.insert(event.lines, {data=line.data}) -- I've forgotten: should we deepcopy(line.data)?
end
return event
end