From f1886391c56e530bb1bb362a450aa9ab6cb8485c Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 1 Jun 2023 12:20:34 -0700 Subject: [PATCH] some temporary logging to catch a bug The bug has been spotted twice: 1. In snap.love, I selected text in one node, then another, and hit: Error: text.lua:789: attempt to compare nil with number stack traceback: text.lua:789: in function 'lt1' select.lua:19: in function 'clip_selection' text.lua:32: in function 'draw' edit.lua:117: in function 'draw' [string "REPL"]:21: in function 'draw' main.lua:152: in function 'draw' app.lua:102: in function [C]: in function 'xpcall' app.lua:112: in function [C]: in function 'xpcall' Couldn't reproduce. 2. In text.love, inscript selected all text in a small buffer and then clicked outside the text. And got: Error: text.lua:784: attempt to compare nil with number Traceback [love "callbacks.lua"]:228: in function 'handler' text.lua:784: in function 'lt1' select.lua:19: in function 'clip_selection' text.lua:27: in function 'draw' edit.lua:117: in function 'draw' run.lua:136: in function 'draw' main.lua:148: in function 'draw' app.lua:42: in function [C]: in function 'xpcall' This is reproducible, and also across forks. --- edit.lua | 14 ++++++++------ run.lua | 5 +++++ select.lua | 4 ++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/edit.lua b/edit.lua index 0f5bef5..7140997 100644 --- a/edit.lua +++ b/edit.lua @@ -223,7 +223,7 @@ end function edit.mouse_press(State, x,y, mouse_button) if State.search_term then return end ---? print('press', State.cursor1.line) + print_and_log(('edit.mouse_press: cursor at %d,%d').format(State.cursor1.line, State.cursor1.pos)) if mouse_press_consumed_by_any_button_handler(State, x,y, mouse_button) then -- press on a button and it returned 'true' to short-circuit return @@ -241,6 +241,7 @@ function edit.mouse_press(State, x,y, mouse_button) -- press and hold to start a selection: sets selection on press, cursor on release -- press and hold, then press shift: ignore shift -- i.e. mouse_release should never look at shift state + print_and_log(('edit.mouse_press: in line %d').format(line_index)) State.old_cursor1 = State.cursor1 State.old_selection1 = State.selection1 State.mousepress_shift = App.shift_down() @@ -248,7 +249,7 @@ function edit.mouse_press(State, x,y, mouse_button) line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } ---? print('selection', State.selection1.line, State.selection1.pos) + print_and_log(('edit.mouse_press: selection now %d,%d').format(State.selection1.line, State.selection1.pos)) break end elseif line.mode == 'drawing' then @@ -266,7 +267,7 @@ end function edit.mouse_release(State, x,y, mouse_button) if State.search_term then return end ---? print('release', State.cursor1.line) + print_and_log(('edit.mouse_release: cursor at %d,%d').format(State.cursor1.line, State.cursor1.pos)) if State.lines.current_drawing then Drawing.mouse_release(State, x,y, mouse_button) schedule_save(State) @@ -275,15 +276,16 @@ function edit.mouse_release(State, x,y, mouse_button) Drawing.before = nil end else + print_and_log('edit.mouse_release: no current drawing') for line_index,line in ipairs(State.lines) do if line.mode == 'text' then if Text.in_line(State, line_index, x,y) then ---? print('reset selection') + print_and_log(('edit.mouse_release: in line %d').format(line_index)) State.cursor1 = { line=line_index, pos=Text.to_pos_on_line(State, line_index, x, y), } ---? print('cursor', State.cursor1.line, State.cursor1.pos) + print_and_log(('edit.mouse_release: cursor now %d,%d').format(State.cursor1.line, State.cursor1.pos)) if State.mousepress_shift then if State.old_selection1.line == nil then State.selection1 = State.old_cursor1 @@ -299,7 +301,7 @@ function edit.mouse_release(State, x,y, mouse_button) end end end ---? print('selection:', State.selection1.line, State.selection1.pos) + print_and_log(('edit.mouse_release: finally selection %s,%s cursor %d,%d').format(tostring(State.selection1.line), tostring(State.selection1.pos), State.cursor1.line, State.cursor1.pos)) end end diff --git a/run.lua b/run.lua index d029bed..5f35a0b 100644 --- a/run.lua +++ b/run.lua @@ -48,6 +48,11 @@ function run.initialize(arg) end end +function print_and_log(s) + print(s) + log(3, s) +end + function run.load_settings() love.graphics.setFont(love.graphics.newFont(Settings.font_height)) -- determine default dimensions and flags diff --git a/select.lua b/select.lua index efb6909..094cca9 100644 --- a/select.lua +++ b/select.lua @@ -8,13 +8,17 @@ -- Result: positions spos,epos between apos,bpos. function Text.clip_selection(State, line_index, apos, bpos) if State.selection1.line == nil then return nil,nil end + print_and_log('text.clip_selection') -- min,max = sorted(State.selection1,State.cursor1) local minl,minp = State.selection1.line,State.selection1.pos + print_and_log(('text.clip_selection: one end from selection: %d,%d'):format(minl,minp)) local maxl,maxp if App.mouse_down(1) then maxl,maxp = Text.mouse_pos(State) + print_and_log(('text.clip_selection: other end from mouse: %d,%d'):format(maxl,maxp)) else maxl,maxp = State.cursor1.line,State.cursor1.pos + print_and_log(('text.clip_selection: other end from cursor: %d,%d'):format(maxl,maxp)) end if Text.lt1({line=maxl, pos=maxp}, {line=minl, pos=minp}) then