Skip to content

Commit

Permalink
working on simple window
Browse files Browse the repository at this point in the history
  • Loading branch information
huynle committed Jan 30, 2024
1 parent cda0bcd commit b3f70d9
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 220 deletions.
9 changes: 3 additions & 6 deletions lua/ogpt/common/layouts.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Config = require("ogpt.config")
local SimpleParameters = require("ogpt.simple_parameters")
local Layout = require("nui.layout")

local M = {}
Expand Down Expand Up @@ -66,21 +67,17 @@ M.edit_with_no_layout = function(layout, input, instruction, output, parameters,
})

input:mount()
vim.api.nvim_buf_set_var(input.bufnr, "ogpt_input", true)
output:mount()
vim.api.nvim_buf_set_var(output.bufnr, "ogpt_output", true)
instruction:mount()
vim.api.nvim_buf_set_var(instruction.bufnr, "ogpt_instruction", true)

vim.api.nvim_buf_set_var(parameters.bufnr, "ogpt_parameters", true)

if opts.show_parameters then
parameters:show()
parameters:mount()

vim.api.nvim_set_current_win(parameters.winid)
vim.api.nvim_buf_set_option(parameters.bufnr, "modifiable", false)
vim.api.nvim_win_set_option(parameters.winid, "cursorline", true)

SimpleParameters.refresh_panel()
else
parameters:hide()
-- vim.api.nvim_set_current_win(instruction.winid)
Expand Down
202 changes: 61 additions & 141 deletions lua/ogpt/common/simple_window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,46 @@ local Config = require("ogpt.config")

local SimpleView = classes.class()

function SimpleView:init(visitor, opts)
self.visitor = visitor
function SimpleView:init(name, opts)
-- Set default values for the options table if it's not provided.
opts = vim.tbl_extend("force", {
buf = {
swapfile = false,
bufhidden = "wipe",
bufhidden = "delete",
filetype = "markdown",
vars = {},
buftype = "nofile",
},
buf_vars = {},
win = {
wrap = true,
cursorline = true,
},
enter = false,
keymaps = {},
new_win = false,
events = {
{
events = { "BufWipeout", "BufDelete" },
callback = function()
-- vim.print("GOTHERE")
end,
},
},
}, opts or {})
self.opts = opts

self.name = visitor.name or opts.buf.filetype
self.name = name
self.bufnr = nil
self.winid = nil
self.visible = false
end

-- Define a function that creates a new window with the given options.
-- The function returns the buffer and window handles.
-- function SimpleView:show(opts)
-- self.visible = true
-- opts = vim.tbl_extend("force", self.opts, opts or {})
--
-- -- Save the handle of the window from which we open the navigation.
-- local start_win = vim.api.nvim_get_current_win()
--
-- -- Get the buffer handle.
-- -- local buf = vim.fn.bufnr(self.name)
-- local buf = self.bufnr
--
-- -- Get the window handle.
-- local win
--
-- -- If the buffer already exists, find the window that displays it and return its handle.
-- if buf ~= -1 then
-- for _, win_id in ipairs(vim.api.nvim_list_wins()) do
-- local bufnr = vim.api.nvim_win_get_buf(win_id)
-- if bufnr == buf then
-- vim.api.nvim_set_current_win(win_id)
-- vim.api.nvim_set_current_win(start_win)
-- return buf, win_id
-- end
-- end
-- end
--
-- -- Reset the current window to the one from which we opened the navigation.
-- vim.api.nvim_set_current_win(start_win)
--
-- -- Return the buffer and window handles.
-- return buf, win
-- end

function SimpleView:unmount()
-- local buf, win = self:mount()
-- Close the window
local force = true
vim.api.nvim_win_close(self.winid, force)
local winid = vim.fn.bufwinid(self.bufnr)
if winid ~= -1 then
vim.api.nvim_win_close(self.winid, force)
end
self.visible = false
end

-- mount can take name or filename
Expand All @@ -77,130 +53,74 @@ end

-- mount can take name or filename
function SimpleView:hide()
local force = true
local winid = vim.fn.bufwinid(self.bufnr)
if winid ~= -1 then
vim.api.nvim_win_close(self.winid, force)
if self.visible then
self:unmount()
end
end

-- mount can take name or filename
function SimpleView:mount(name, opts)
if self.visible then
return
end
self.visible = true
opts = opts or {}
opts = vim.tbl_extend("force", self.opts, opts)
name = name or self.name
-- Save the handle of the window from which we open the navigation.
local start_win = vim.api.nvim_get_current_win()

-- Try to get the buffer handle.
local buf = vim.fn.bufnr(name)
if buf ~= -1 and vim.fn.bufexists(buf) then
-- buffer is still sitting out there and is valid
else
-- pull it from vim global
buf = vim.g[name]
end

local previous_winid
if not self.opts.new_win then
previous_winid = self.winid
end

-- If the buffer already exists, find the window that displays it and return its handle.
if buf and vim.fn.bufexists(buf) then
for _, win_id in ipairs(vim.api.nvim_list_wins()) do
local bufnr = vim.api.nvim_win_get_buf(win_id)
if bufnr == buf then
-- if not self.opts.enter then
-- vim.api.nvim_set_current_win(start_win)
-- else
-- vim.api.nvim_set_current_win(win_id)
-- end
previous_winid = win_id
break
-- self.bufnr = buf
-- self.winid = win_id
-- return buf, win_id
end
end
end

-- -- If the buffer already exists, find the window that displays it and return its handle.
-- if buf and vim.fn.bufexists(buf) then
-- for _, win_id in ipairs(vim.api.nvim_list_wins()) do
-- local bufnr = vim.api.nvim_win_get_buf(win_id)
-- if bufnr == buf then
-- if not self.opts.enter then
-- vim.api.nvim_set_current_win(start_win)
-- else
-- vim.api.nvim_set_current_win(win_id)
-- end
-- self.bufnr = buf
-- self.winid = win_id
-- return buf, win_id
-- end
-- end
-- end

local previous_winid = vim.fn.bufwinid(buf)
-- Open a new vertical window at the far right.
-- vim.api.nvim_command("botright " .. "vnew")
if vim.fn.filereadable(name) == 1 then
if previous_winid then
vim.api.nvim_set_current_win(previous_winid)
vim.api.nvim_command("edit " .. name)
else
vim.api.nvim_command("vnew " .. name)
end
self.bufnr = vim.api.nvim_get_current_buf()
-- if vim.fn.filereadable(name) == 1 then
if previous_winid ~= -1 and not self.opts.new_win then
vim.api.nvim_set_current_win(previous_winid)
vim.api.nvim_command("edit " .. name)
else
if previous_winid and not self.opts.new_win and (self.bufnr ~= nil and vim.fn.bufwinid(self.bufnr) ~= -1) then
vim.api.nvim_set_current_win(previous_winid)
self.bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(self.bufnr, -1, -1, false, {})
else
vim.api.nvim_command("vnew")
self.bufnr = vim.api.nvim_get_current_buf()
-- Set the buffer's filetype to the filetype specified in the options table.
vim.api.nvim_buf_set_option(self.bufnr, "filetype", self.opts.buf.filetype)
-- Set the name of the buffer to the buffer name specified in the options table.
vim.api.nvim_buf_set_name(self.bufnr, name)
end
vim.api.nvim_command("vnew " .. name)
end

-- Get the buffer and window handles of the new window.
self.bufnr = vim.api.nvim_get_current_buf()
self.winid = vim.api.nvim_get_current_win()

-- -- Set the buffer type to "nofile" to prevent it from being saved.
vim.api.nvim_buf_set_option(self.bufnr, "buftype", "nofile")

-- Disable swapfile for the buffer.
vim.api.nvim_buf_set_option(self.bufnr, "swapfile", false)

-- Set the buffer's hidden option to "wipe" to destroy it when it's hidden.
vim.api.nvim_buf_set_option(self.bufnr, "bufhidden", "delete")

-- -- Set so that the cusor does not jump
-- vim.api.nvim_buf_set_option(self.bufnr, "switchbuf", "useopen")

-- -- Set the name of the buffer to the buffer name specified in the options table.
-- vim.api.nvim_buf_set_name(self.bufnr, name or self.name)
-- Set the buffer type to "nofile" to prevent it from being saved.
-- vim.api.nvim_buf_set_option(self.bufnr, "buftype", "nofile")
for opt, val in pairs(self.opts.buf) do
vim.api.nvim_buf_set_option(self.bufnr, opt, val)
end

-- -- Disable swapfile for the buffer.
-- vim.api.nvim_buf_set_option(self.bufnr, "swapfile", false)
--
-- -- Set the buffer's hidden option to "wipe" to destroy it when it's hidden.
-- vim.api.nvim_buf_set_option(self.bufnr, "bufhidden", "delete")
-- -- Set the window options as specified in the options table.
-- -- vim.api.nvim_win_set_option(win, "wrap", opts.win.wrap)
-- -- vim.api.nvim_win_set_option(win, "cursorline", opts.win.cursorline)
--
-- Set buffer variables as specified in the options table.
for key, value in pairs(self.opts.buf.vars or {}) do
for key, value in pairs(self.opts.buf_vars or {}) do
vim.api.nvim_buf_set_var(self.bufnr, key, value)
end

-- Set the window options as specified in the options table.
-- vim.api.nvim_win_set_option(win, "wrap", opts.win.wrap)
-- vim.api.nvim_win_set_option(win, "cursorline", opts.win.cursorline)

-- Set the keymaps for the window as specified in the options table.
for keymap, command in pairs(self.opts.keymaps) do
vim.keymap.set("n", keymap, command, { noremap = true, buffer = self.bufnr })
end

local group = vim.api.nvim_create_augroup(name .. "_augroup", {})
for _, event in ipairs(self.opts.events) do
local event_names = event.events
event.events = nil
opts = vim.tbl_extend("force", opts or {}, {
group = group,
buffer = self.bufnr,
})
vim.api.nvim_create_autocmd(event_names, event)
end

if not self.opts.enter then
vim.api.nvim_set_current_win(start_win)
end
vim.g[name] = self.bufnr
end

function SimpleView:map(mode, key, command)
Expand Down
Loading

0 comments on commit b3f70d9

Please sign in to comment.