Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cursor layout #878

Merged
merged 18 commits into from
Jul 16, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor cursor layout code
  • Loading branch information
Luxed committed Feb 25, 2021
commit 510ec13c68ccfb1d12e4e53c34490e0bf1299147
41 changes: 28 additions & 13 deletions lua/telescope/pickers/layout_strategies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,38 +221,53 @@ layout_strategies.cursor = function(self, columns, lines)
local height = resolve.resolve_height(self.window.results_height)(self, columns, lines)
local width = resolve.resolve_width(self.window.width)(self, columns, lines)

--local max_results = (height > lines and lines or height)
local max_width = (width > columns and columns or width)

-- border size
local bs = 1
if is_borderless(self) then
bs = 0
end
local edge_gap = 1

prompt.height = 1
results.height = height
preview.height = height + 2
preview.height = results.height + prompt.height + bs

-- If previewer is activated, it should take 2/3 of the space (width)
local width_left = self.previewer and math.floor(max_width/3) or max_width
prompt.width = width_left
results.width = width_left
results.width = prompt.width
preview.width = self.previewer and (2*width_left) or 0

local total_height = preview.height + (bs*2)
local total_width = prompt.width + (bs*2) + preview.width + bs

local position = vim.fn.nvim_win_get_position(0)
local cursor_line = vim.fn.winline() + position[1]
local cursor_col = vim.fn.wincol() + position[2]
local top_left = {
line = vim.fn.winline() + position[1] + bs,
col = vim.fn.wincol() + position[2]
}
local bot_right = {
line = top_left.line + total_height - 1,
col = top_left.col + total_width - 1
}

if bot_right.line > lines then
-- position above current line
top_left.line = top_left.line - total_height - 1
end
if bot_right.col >= columns then
-- cap to the right of the screen
top_left.col = columns - total_width
end

local max_line = (cursor_line + (bs*3) + height + prompt.height) > lines and (cursor_line - (bs*3) - height - prompt.height - 1) or cursor_line
prompt.line = max_line + bs
results.line = prompt.line + (bs*2)
prompt.line = top_left.line
results.line = prompt.line + bs + 1
preview.line = prompt.line

local max_col = (cursor_col + max_width + bs + edge_gap) > columns and (columns - max_width - bs - edge_gap) or cursor_col
prompt.col = max_col
results.col = max_col
preview.col = results.col + results.width + bs + bs
prompt.col = top_left.col
results.col = prompt.col
preview.col = results.col + (bs*2) + results.width

return {
preview = self.previewer and preview.width > 0 and preview,
Expand Down