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

Commit 2b20fbe

Browse files
authored
feat(panel): support layout config
resolves zbirenbaum#103
1 parent 9ae1389 commit 2b20fbe

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ require('copilot').setup({
5656
refresh = "gr",
5757
open = "<M-CR>"
5858
},
59+
layout = {
60+
position = "bottom", -- bottom | top | left | right
61+
ratio = 0.4
62+
},
5963
},
6064
suggestion = {
6165
enabled = true,
@@ -99,7 +103,7 @@ The `copilot.panel` module exposes the following functions:
99103
require("copilot.panel").accept()
100104
require("copilot.panel").jump_next()
101105
require("copilot.panel").jump_prev()
102-
require("copilot.panel").open()
106+
require("copilot.panel").open({postion, ratio})
103107
require("copilot.panel").refresh()
104108
```
105109

lua/copilot/config.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ local default_config = {
1212
refresh = "gr",
1313
open = "<M-CR>",
1414
},
15+
layout = {
16+
position = "bottom",
17+
ratio = 0.4
18+
}
1519
},
1620
---@class copilot_config_suggestion
1721
suggestion = {

lua/copilot/panel.lua

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ local panel = {
3535
was_insert = nil,
3636
auto_refreshing = nil,
3737
},
38+
layout = {
39+
position = "bottom",
40+
ratio = 0.4
41+
},
3842

3943
auto_refresh = false,
4044
keymap = {},
@@ -303,10 +307,33 @@ function panel:ensure_winid()
303307
return
304308
end
305309

306-
local height = math.floor(vim.api.nvim_win_get_height(0) * 0.4)
310+
local position = self.layout.position
311+
local ratio = self.layout.ratio
312+
313+
local get_width = vim.api.nvim_win_get_width
314+
local get_height = vim.api.nvim_win_get_height
315+
316+
local split_map = {
317+
top = { cmd_prefix = "topleft ", winsize_fn = get_height },
318+
right = { cmd_prefix = "vertical botright ", winsize_fn = get_width },
319+
bottom = { cmd_prefix = "botright ", winsize_fn = get_height },
320+
left = { cmd_prefix = "vertical topleft ", winsize_fn = get_width },
321+
}
322+
323+
local split_info = split_map[position]
324+
if not split_info then
325+
print('Error: ' .. position .. ' is not a valid position')
326+
return
327+
end
328+
329+
local function resolve_splitcmd()
330+
local size = math.floor(split_info.winsize_fn(0) * ratio)
331+
local cmd_prefix = split_info.cmd_prefix
332+
return "silent noswapfile " .. cmd_prefix .. tostring(size) .. ' split'
333+
end
307334

308335
self.winid = vim.api.nvim_win_call(0, function()
309-
vim.cmd("silent noswapfile " .. tostring(height) .. "split")
336+
vim.cmd(resolve_splitcmd())
310337
return vim.api.nvim_get_current_win()
311338
end)
312339

@@ -483,14 +510,18 @@ function mod.refresh()
483510
end)
484511
end
485512

486-
function mod.open()
513+
---@param layout {position: string, ratio: number}
514+
---position: (optional) 'bottom' | 'top' | 'left' | 'right'
515+
---ratio: (optional) between 0 and 1
516+
function mod.open(layout)
487517
local client = c.get()
488518
if not client then
489519
print("Error, copilot not running")
490520
return
491521
end
492522

493523
panel.client = client
524+
panel.layout = vim.tbl_deep_extend("force", panel.layout, layout or {})
494525

495526
panel:init()
496527
end
@@ -505,6 +536,7 @@ function mod.setup(config)
505536
panel.auto_refresh = config.auto_refresh or false
506537

507538
panel.keymap = config.keymap or {}
539+
panel.layout = vim.tbl_deep_extend('force', panel.layout, config.layout or {})
508540

509541
if panel.keymap.open then
510542
vim.keymap.set("i", panel.keymap.open, mod.open, {

0 commit comments

Comments
 (0)