Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
perf(ui): find a pair towards the closer side (top or bottom)
Browse files Browse the repository at this point in the history
  • Loading branch information
utilyre committed Mar 11, 2023
1 parent ebfbf70 commit 4c27b11
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lua/sentiment/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,30 @@ function M.render(win)
pair.left = find_other_pair(portion, left, under_cursor, true)
pair.right = portion:get_cursor()
else
local found_left = nil
pair.left, found_left = find_pair(portion, true)

if found_left == nil then
pair.right = find_pair(portion, false)
if portion:is_upper() then
local found_left = nil
pair.left, found_left = find_pair(portion, true)

if found_left == nil then
pair.right = find_pair(portion, false)
else
local found_right = config.get_right_by_left(found_left)
---@cast found_right -nil

pair.right = find_other_pair(portion, found_left, found_right, false)
end
else
local found_right = config.get_right_by_left(found_left)
---@cast found_right -nil
local found_right = nil
pair.right, found_right = find_pair(portion, false)

if found_right == nil then
pair.left = find_pair(portion, true)
else
local found_left = config.get_left_by_right(found_right)
---@cast found_left -nil

pair.right = find_other_pair(portion, found_left, found_right, false)
pair.left = find_other_pair(portion, found_left, found_right, true)
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions lua/sentiment/ui/Portion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ function Portion:get_top() return self.viewport[1] end
---@return integer
function Portion:get_bottom() return self.viewport[2] end

---Check whether the cursor is on the upper part of `Portion`.
---
---@return boolean
function Portion:is_upper()
local half = (self.viewport[2] - self.viewport[1]) / 2
return (self.cursor[1] - self.viewport[1]) < half
end

---Get the nth line.
---
---@private
Expand Down

0 comments on commit 4c27b11

Please sign in to comment.