Not quite ideal: the scrollbar computation only considers screen_bottom1.line and not screen_bottom1.pos, and so it always assumes the final line is at the bottom of the screen.
I'm making a deeper change here that I might come to regret. I want to avoid creating new book-keeping for editor mutations, so I'm putting the work of computing scrollbar data into clear_screen_line_cache. But that implies the editor should never clear before updating data, and I caught one place that wasn't true before. A better name helps avoid this in future. Let's see how much toil this causes in resolving conflicts.
MZ3DMYPD4LSSQKEA3GNI5MK2QNTJ523DTNG6DUPWEWLH4FLUIQSAC
6K5PFF6XBFTM6CXUVVFIH4CQMCMPHTND3ICDMRMNOME5BUBF27NQC
DTBFNHJDOFMUXRAFVN3NYK2IJYH75EITUIPJIS3CNP7RS6OY5LVQC
PRE6XPRNICDCHMF7C45A6DRPVT47IPAIWW6Q7AHNGW3D2PBGOPNAC
I64IPGJXWRTGHHVAYJUBUIWFR4BY6NM5P7TLTV4JOD7K4BVYDECQC
SDEY7LFJ4LY735OZAJ6X5Y2SE3MFBT4X4TWLHVW3SS2JAK757E6QC
BULPIBEGL7TMK6CVIE7IS7WGAHGOSUJBGJSFQK542MOWGHP2ADQQC
MUJTM6REGQAK3LZTIFWGJRXE2UPCM4HSLXQYSF5ITLXLS6JCVPMQC
MU2HIRR62BULXR6QKVGJNUWSURYYJ2YFCHUHS4BOI3KHUM725BLAC
LSYLEVBDBZBGLSCXTRBW46WT4TUMMSPCH7M6HSNYI5SIH2WNPYEAC
MD3W5IRAC6UQALQE4LJC52VQNDO3I3HXF3XE2XHDABXBYJBUVAXQC
Z5HLXU4PJWWJJDBCK52NBD6PIRIA3TAN2BKZB5HBYFGIDBX4F5HAC
XNFTJHC4QSHNSIWNN7K6QZEZ37GTQYKHS4EPNSVPQCUSWREROGIQC
SPSW74Y5OJ54Y7VQ3SJFCJR5CYDKTR4A3TOEVZODDZLUSDDU2GZAC
PTDO2SOTXEI6FROZ2AVRFXSKKNKCRMPPTQSI5LWD45UVGDJPMSGQC
JCXL74WVQ23V53EOCKA2NXQIYA5NNOXGN5WYC7ZW42EI2I6D5IJAC
TGHAJBESCIEGWUE2D3FGLNOIAYT4D2IRGZKRXRMTUFW7QZETC7OAC
ILOA5BYFTQKBSHLFMMZUVPQ2JXBFJD62ERQFBTDK2WSRXUN525VQC
R53OF3ONKT5VL5BGK63YSN6GXIIAVNYDG4UMHITK72WXFWPJ25MQC
LXTTOB33N2HCUZFIUDRQGGBVHK2HODRG4NBLH6RXRQZDCHF27BSAC
2L5MEZV344TOZLVY3432RHJFIRVXFD6O3GWLL5O4CV66BGAFTURQC
BJ5X5O4ACBBJ56LRBBSTCW6IBQP4HAEOOOPNH3SKTA4F66YTOIDAC
R5QXEHUIZLELJGGCZAE7ATNS3CLRJ7JFRENMGH4XXH24C5WABZDQC
ZM7NOBRMD5HHA35Y4JDC76EOA2RD4KQOQCPURXYKXA6ABMKOJIGQC
5RUFNRJOK3PXQKJTPDEN5K5PI67MGB25QUA44WOCCH2O5KHXT45QC
function Text.clear_screen_line_cache(State, line_index)
function Text.refresh_scrollbar_data(State)
--? print('clearing fragments')
local curr_screen_line_index = 1
local npopulated = 0
for i=1,#State.lines do
if State.line_cache[i] == nil then
State.line_cache[i] = {}
end
if State.line_cache[i].screen_line_starting_pos == nil then
--? print(('refresh_scrollbar_data: populating line %d'):format(i))
Text.populate_screen_line_starting_pos(State, i)
npopulated = npopulated+1
end
State.line_cache[i].start_screen_line_index = curr_screen_line_index
curr_screen_line_index = curr_screen_line_index + #State.line_cache[i].screen_line_starting_pos
end
if npopulated > 1 then
print(('refresh_scrollbar_data: had to populate %d lines'):format(npopulated))
end
State.screen_line_count = curr_screen_line_index-1
end
function Text.refresh_screen_line_cache(State, line_index)
-- returns:
-- * a float between 0 and 1 regarding the relative position of the top line on screen
-- * a float between 0 and 1 regarding the relative position of the bottom line on screen
compute_scrollbar = function(state)
local top = state.line_cache[state.screen_top1.line].start_screen_line_index
local bot = state.line_cache[state.screen_bottom1.line].start_screen_line_index
return (top-1)/state.screen_line_count, bot/state.screen_line_count
end
draw_scrollbar = function(Editor_state)
App.color(Normal_color)
love.graphics.line(Editor_state.right+30, Editor_state.top, Editor_state.right+30, Editor_state.bottom)
love.graphics.line(Editor_state.right+25, Editor_state.top, Editor_state.right+35, Editor_state.top)
love.graphics.line(Editor_state.right+25, Editor_state.bottom, Editor_state.right+35, Editor_state.bottom)
local sbtop, sbbot = compute_scrollbar(Editor_state)
local topy = Editor_state.top + sbtop*(Editor_state.bottom - Editor_state.top)
local boty = Editor_state.top +sbbot*(Editor_state.bottom - Editor_state.top)
App.color{r=0.6, g=0.6, b=0.8, a=0.5}
love.graphics.rectangle('fill', Editor_state.right+20, topy, 20, boty-topy)
end