about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-07-31 08:24:15 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-07-31 08:40:07 -0700
commitf7f42b0befc3fbe6c425cfa8bad18192c2beb926 (patch)
treef130115e51beb5b0feb4b4f7d0eb9c9d5f78a9cf
parent484b76f5c6984f4d3a745d9fb36dd55cefb97c06 (diff)
downloadview.love-f7f42b0befc3fbe6c425cfa8bad18192c2beb926.tar.gz
hoist and duplicate a conditional
I'm duplicating the bounds check when drawing cursor and search
highlight because they're separate concerns and require subtly different
logic.
-rw-r--r--source_text.lua8
-rw-r--r--text.lua8
2 files changed, 10 insertions, 6 deletions
diff --git a/source_text.lua b/source_text.lua
index b3d4963..9e3a9ac 100644
--- a/source_text.lua
+++ b/source_text.lua
@@ -53,8 +53,8 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
       end
       if not hide_cursor and line_index == State.cursor1.line then
         -- render search highlight or cursor
-        if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
-          if State.search_term then
+        if State.search_term then
+          if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
             local data = State.lines[State.cursor1.line].data
             local cursor_offset = Text.offset(data, State.cursor1.pos)
             if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
@@ -62,7 +62,9 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
               App.color(Text_color)
               love.graphics.print(State.search_term, State.left+lo_px,y)
             end
-          elseif Focus == 'edit' then
+          end
+        elseif Focus == 'edit' then
+          if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
             Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
           end
         end
diff --git a/text.lua b/text.lua
index 9762365..8da0586 100644
--- a/text.lua
+++ b/text.lua
@@ -31,8 +31,8 @@ function Text.draw(State, line_index, y, startpos)
       App.screen.print(screen_line, State.left,y)
       if line_index == State.cursor1.line then
         -- render search highlight or cursor
-        if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
-          if State.search_term then
+        if State.search_term then
+          if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
             local data = State.lines[State.cursor1.line].data
             local cursor_offset = Text.offset(data, State.cursor1.pos)
             if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
@@ -40,7 +40,9 @@ function Text.draw(State, line_index, y, startpos)
               App.color(Text_color)
               love.graphics.print(State.search_term, State.left+lo_px,y)
             end
-          else
+          end
+        else
+          if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
             Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
           end
         end