Skip to content

Commit 6ce2c72

Browse files
authored
Merge pull request #71 from jmsdrh/fix-buffer-position-cursor
fix(buffer): position=cursor uses the right location
2 parents a9ee358 + 5658d49 commit 6ce2c72

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

lua/tiny-code-action/pickers/buffer_utils/preview.lua

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,33 @@ function M.show_preview(
101101
local orig_preview_width = math.floor(nvim_width * 0.4)
102102
local orig_preview_height = math.floor(nvim_height * 0.4)
103103
local preview_row, preview_col, preview_width, preview_height
104+
local margin = main_win_config.margin
105+
local win_row, win_col = unpack(vim.api.nvim_win_get_position(0))
106+
local south_space = nvim_height - win_row - main_height - margin
107+
local north_space = win_row - margin
104108

105-
if main_col + main_width + 2 + orig_preview_width <= nvim_width then
109+
if win_col + main_width + margin + orig_preview_width <= nvim_width then
106110
preview_width = orig_preview_width
107111
preview_height = orig_preview_height
108112
preview_row = main_row
109-
preview_col = main_col + main_width + 2
113+
preview_col = main_col + main_width + margin
114+
elseif win_col > margin + orig_preview_width then
115+
preview_width = orig_preview_width
116+
preview_height = orig_preview_height
117+
preview_row = main_row
118+
preview_col = win_col - margin - orig_preview_width
119+
elseif south_space >= north_space then
120+
preview_width = main_width
121+
preview_height =
122+
math.max(0, math.min(math.ceil(orig_preview_height / 2), south_space - margin))
123+
preview_row = win_row + main_height + margin
124+
preview_col = main_col
110125
else
111-
local south_space = nvim_height - (main_row + main_height) - 2
112-
local north_space = main_row - 2
113126
preview_width = main_width
114-
preview_height = math.ceil(orig_preview_height / 2)
115-
if south_space >= north_space then
116-
preview_row = main_row + main_height + 2
117-
preview_col = main_col
118-
else
119-
preview_row = math.max(0, main_row - preview_height - 2)
120-
preview_col = main_col
121-
end
127+
preview_height =
128+
math.max(0, math.min(math.ceil(orig_preview_height / 2), north_space - margin))
129+
preview_row = win_row - preview_height - margin
130+
preview_col = main_col
122131
end
123132

124133
local preview_buf = vim.api.nvim_create_buf(false, true)

lua/tiny-code-action/pickers/buffer_utils/window.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local display = require("tiny-code-action.pickers.buffer_utils.display")
22
local preview = require("tiny-code-action.pickers.buffer_utils.preview")
33
local utils = require("tiny-code-action.utils")
4+
local margin = 2
45

56
local M = {}
67

@@ -15,7 +16,7 @@ end
1516

1617
local function calculate_window_position(lines, config)
1718
local width, height = display.calculate_window_size(lines)
18-
local row, col
19+
local row, col, win_row, win_col, shift
1920
local position = config.picker and config.picker.opts and config.picker.opts.position or "cursor"
2021

2122
if position == "center" then
@@ -24,9 +25,12 @@ local function calculate_window_position(lines, config)
2425
row = math.floor((nvim_height - height) / 2)
2526
col = math.floor((nvim_width - width) / 2)
2627
else
27-
local cursor_row = vim.api.nvim_win_get_cursor(0)[1] - 1
28-
row = cursor_row + 2
29-
col = 2
28+
win_row, win_col = unpack(vim.api.nvim_win_get_position(0))
29+
row = vim.fn.winline()
30+
col = vim.fn.wincol()
31+
shift = math.max(0, col - math.floor(width / 2))
32+
row = win_row + row + margin
33+
col = win_col + shift
3034
end
3135

3236
return width, height, row, col
@@ -172,6 +176,7 @@ function M.create_main_window(
172176

173177
local win = vim.api.nvim_open_win(buf, true, win_config)
174178
win_config.win = win
179+
win_config.margin = margin
175180
vim.api.nvim_win_set_cursor(win, { 2, 0 })
176181

177182
utils.set_buf_option(buf, "modifiable", false)

0 commit comments

Comments
 (0)