more precise search highlighting

This commit is contained in:
Kartik K. Agaram 2022-06-03 14:37:32 -07:00
parent 1bdb931575
commit e2696fcc72
2 changed files with 5 additions and 6 deletions

View File

@ -51,6 +51,8 @@ function Text.clip_selection(line_index, apos, bpos)
end
-- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line
-- Creates text objects every time, so use this sparingly.
-- Returns some intermediate computation useful elsewhere.
function Text.draw_highlight(line, x,y, pos, lo,hi)
if lo then
local lo_offset = utf8.offset(line.data, lo)
@ -71,6 +73,7 @@ function Text.draw_highlight(line, x,y, pos, lo,hi)
love.graphics.setColor(0.7,0.7,0.9)
love.graphics.rectangle('fill', x+lo_px,y, text_width,Line_height)
love.graphics.setColor(0,0,0)
return lo_px
end
end

View File

@ -62,13 +62,9 @@ function Text.draw(line, line_width, line_index)
if pos <= Cursor1.pos and pos + frag_len > Cursor1.pos then
if Search_term then
if Lines[Cursor1.line].data:sub(Cursor1.pos, Cursor1.pos+utf8.len(Search_term)-1) == Search_term then
if Search_text == nil then
Search_text = App.newText(love.graphics.getFont(), Search_term)
end
love.graphics.setColor(0.7,1,1)
love.graphics.rectangle('fill', x,y, App.width(Search_text),Line_height)
local lo_px = Text.draw_highlight(line, x,y, pos, Cursor1.pos, Cursor1.pos+utf8.len(Search_term))
love.graphics.setColor(0,0,0)
love.graphics.print(Search_term, x,y)
love.graphics.print(Search_term, x+lo_px,y)
end
else
Text.draw_cursor(x+Text.x(frag, Cursor1.pos-pos+1), y)