Skip to content

Commit

Permalink
cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
huynle committed Jan 31, 2024
1 parent 82f2dd4 commit b7e3bd4
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local pickers = require("telescope.pickers")
local SimpleWindow = require("ogpt.common.simple_window")
local SimpleWindow = require("ogpt.common.ui.window")
local utils = require("ogpt.utils")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
Expand Down
Empty file added lua/ogpt/common/ui/init.lua
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local classes = require("ogpt.common.classes")
local utils = require("ogpt.utils")
local Config = require("ogpt.config")

local SimpleView = classes.class()

Expand Down Expand Up @@ -136,75 +135,4 @@ function SimpleView:map(mode, key, command)
vim.keymap.set(mode, key, command, { buffer = self.bufnr })
end

function SimpleView:apply_map(opts)
-- accept output and replace
self:map("n", Config.options.popup.keymaps.accept, function()
-- local _lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
vim.api.nvim_buf_set_text(
opts.main_bufnr,
opts.selection_idx.start_row - 1,
opts.selection_idx.start_col - 1,
opts.selection_idx.end_row - 1,
opts.selection_idx.end_col,
opts.lines
)
vim.cmd("q")
end)

-- accept output and prepend
self:map("n", Config.options.popup.keymaps.prepend, function()
local _lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
table.insert(_lines, "")
table.insert(_lines, "")
vim.api.nvim_buf_set_text(
opts.main_bufnr,
opts.selection_idx.end_row - 1,
opts.selection_idx.start_col - 1,
opts.selection_idx.end_row - 1,
opts.selection_idx.start_col - 1,
_lines
)
vim.cmd("q")
end)

-- accept output and append
self:map("n", Config.options.popup.keymaps.append, function()
local _lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
table.insert(_lines, 1, "")
table.insert(_lines, "")
vim.api.nvim_buf_set_text(
opts.main_bufnr,
opts.selection_idx.end_row,
opts.selection_idx.start_col - 1,
opts.selection_idx.end_row,
opts.selection_idx.start_col - 1,
_lines
)
vim.cmd("q")
end)

-- yank code in output and close
self:map("n", Config.options.popup.keymaps.yank_code, function()
local _lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
local _code = utils.getSelectedCode(_lines)
vim.fn.setreg(Config.options.yank_register, _code)

if vim.fn.mode() == "i" then
vim.api.nvim_command("stopinsert")
end
vim.cmd("q")
end)

-- yank output and close
self:map("n", Config.options.popup.keymaps.yank_to_register, function()
local _lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
vim.fn.setreg(Config.options.yank_register, _lines)

if vim.fn.mode() == "i" then
vim.api.nvim_command("stopinsert")
end
vim.cmd("q")
end)
end

return SimpleView
2 changes: 1 addition & 1 deletion lua/ogpt/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function M.defaults()
-- model = "mistral:7b",
type = "edit",
strategy = "edit",
template = "Given the follow snippet, {{instruction}}.\n\nSnippet:\n```{{filetype}}\n{{input}}\n```",
template = "Given the follow input, {{instruction}}.\n\nInput:\n```{{filetype}}\n{{input}}\n```",
delay = true,
params = {
temperature = 0.5,
Expand Down
2 changes: 1 addition & 1 deletion lua/ogpt/flows/actions/edits/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ local setup_and_mount = vim.schedule_wrap(function(lines, output_lines, ...)

-- set input and output settings
for _, window in ipairs({ input_window, output_window }) do
vim.api.nvim_buf_set_option(window.bufnr, "filetype", "markdown")
vim.api.nvim_buf_set_option(window.bufnr, "syntax", "markdown")
vim.api.nvim_win_set_option(window.winid, "number", true)
end
end)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
local Config = require("ogpt.config")
local utils = require("ogpt.utils")
local SimpleParameters = require("ogpt.simple_parameters")
local SimpleParameters = require("ogpt.common.simple_parameters")
local Layout = require("nui.layout")

local M = {}

M.edit_with_nui_layout = function(layout, input, instruction, output, parameters, opts)
M.edit_with_nui_layout = function(layout, parent, input, instruction, output, parameters, opts)
opts = opts or {}
local _boxes
if opts.show_parameters then
Expand Down
63 changes: 21 additions & 42 deletions lua/ogpt/flows/actions/popup/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ local classes = require("ogpt.common.classes")
local BaseAction = require("ogpt.flows.actions.base")
local utils = require("ogpt.utils")
local Config = require("ogpt.config")
local SimpleWindow = require("ogpt.common.simple_window")
local SimpleWindow = require("ogpt.common.ui.window")
local popup_keymap = require("ogpt.flows.actions.popup.keymaps")

local PopupAction = classes.class(BaseAction)

Expand All @@ -11,6 +12,7 @@ local STRATEGY_APPEND = "append"
local STRATEGY_PREPEND = "prepend"
local STRATEGY_DISPLAY = "display"
local STRATEGY_DISPLAY_WINDOW = "display_window"
local STRATEGY_NEW_DISPLAY_WINDOW = "new_display_window"
local STRATEGY_QUICK_FIX = "quick_fix"

function PopupAction:init(name, opts)
Expand Down Expand Up @@ -77,66 +79,43 @@ function PopupAction:run()
end
end
)
elseif self.strategy == STRATEGY_DISPLAY_WINDOW then
self.popup = SimpleWindow.new(self, {
elseif self.strategy == STRATEGY_DISPLAY_WINDOW or self.strategy == STRATEGY_NEW_DISPLAY_WINDOW then
self.popup = SimpleWindow.new("ogpt_popup", {
new_win = false,
buf = {
filetype = "markdown",
vars = {
ogpt = true,
},
syntax = "markdown",
},
events = {
-- {
-- events = { "BufUnload" },
-- callback = function()
-- opts.stop()
-- end,
-- },
},
})
self.popup:apply_map(opts)
popup_keymap.apply_map(self.popup, opts)

local keys = Config.options.popup.keymaps.close
if type(keys) ~= "table" then
keys = { keys }
end
for _, key in ipairs(keys) do
self.popup:map("n", key, function()
if opts.stop and type(opts.stop) == "function" then
opts.stop()
end
self.popup:unmount()
end)
self:set_loading(true)
if self.strategy == STRATEGY_NEW_DISPLAY_WINDOW then
self.popup:mount(self.name)
else
self.popup:mount()
end

self:set_loading(true)
self.popup:mount(self.name, {
name = self.name,
cur_win = self.cur_win,
main_bufnr = self:get_bufnr(),
selection_idx = {
start_row = start_row,
start_col = start_col,
end_row = end_row,
end_col = end_col,
},
default_ui = self.ui,
title = self.opts.title,
args = self.opts.args,
stop = function()
self.stop = true
end,
})
params.stream = true

self.provider.api:chat_completions(
params,
utils.partial(utils.add_partial_completion, {
panel = self.popup,
progress = function(flag)
self:run_spinner(flag)
end,
on_complete = function(total_text)
-- print("completed: " .. total_text)
end,
}),
function()
-- should stop function
if self.stop then
-- self.stop = false
-- self:run_spinner(false)
self:set_loading(false)
return true
else
Expand Down
90 changes: 90 additions & 0 deletions lua/ogpt/flows/actions/popup/keymaps.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
local utils = require("ogpt.utils")
local Config = require("ogpt.config")

local M = {}

function M.apply_map(popup, opts)
-- accept output and replace
popup:map("n", Config.options.popup.keymaps.accept, function()
-- local _lines = vim.api.nvim_buf_get_lines(popup.bufnr, 0, -1, false)
vim.api.nvim_buf_set_text(
opts.main_bufnr,
opts.selection_idx.start_row - 1,
opts.selection_idx.start_col - 1,
opts.selection_idx.end_row - 1,
opts.selection_idx.end_col,
opts.lines
)
vim.cmd("q")
end)

-- accept output and prepend
popup:map("n", Config.options.popup.keymaps.prepend, function()
local _lines = vim.api.nvim_buf_get_lines(popup.bufnr, 0, -1, false)
table.insert(_lines, "")
table.insert(_lines, "")
vim.api.nvim_buf_set_text(
opts.main_bufnr,
opts.selection_idx.end_row - 1,
opts.selection_idx.start_col - 1,
opts.selection_idx.end_row - 1,
opts.selection_idx.start_col - 1,
_lines
)
vim.cmd("q")
end)

-- accept output and append
popup:map("n", Config.options.popup.keymaps.append, function()
local _lines = vim.api.nvim_buf_get_lines(popup.bufnr, 0, -1, false)
table.insert(_lines, 1, "")
table.insert(_lines, "")
vim.api.nvim_buf_set_text(
opts.main_bufnr,
opts.selection_idx.end_row,
opts.selection_idx.start_col - 1,
opts.selection_idx.end_row,
opts.selection_idx.start_col - 1,
_lines
)
vim.cmd("q")
end)

-- yank code in output and close
popup:map("n", Config.options.popup.keymaps.yank_code, function()
local _lines = vim.api.nvim_buf_get_lines(popup.bufnr, 0, -1, false)
local _code = utils.getSelectedCode(_lines)
vim.fn.setreg(Config.options.yank_register, _code)

if vim.fn.mode() == "i" then
vim.api.nvim_command("stopinsert")
end
vim.cmd("q")
end)

-- yank output and close
popup:map("n", Config.options.popup.keymaps.yank_to_register, function()
local _lines = vim.api.nvim_buf_get_lines(popup.bufnr, 0, -1, false)
vim.fn.setreg(Config.options.yank_register, _lines)

if vim.fn.mode() == "i" then
vim.api.nvim_command("stopinsert")
end
vim.cmd("q")
end)

local keys = Config.options.popup.keymaps.close
if type(keys) ~= "table" then
keys = { keys }
end
for _, key in ipairs(keys) do
popup:map("n", key, function()
if opts.stop and type(opts.stop) == "function" then
opts.stop()
end
popup:unmount()
end)
end
end

return M
10 changes: 3 additions & 7 deletions lua/ogpt/flows/actions/simple_edit/init.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local classes = require("ogpt.common.classes")
local SimpleWindow = require("ogpt.common.simple_window")
local SimpleWindow = require("ogpt.common.ui.window")
local BaseAction = require("ogpt.flows.actions.base")
local layouts = require("ogpt.common.layouts")
local layouts = require("ogpt.flows.actions.edits.layouts")
local utils = require("ogpt.utils")
local Config = require("ogpt.config")
local SimpleParameters = require("ogpt.simple_parameters")
local SimpleParameters = require("ogpt.common.simple_parameters")

local EditAction = classes.class(BaseAction)

Expand Down Expand Up @@ -173,10 +173,6 @@ function EditAction:edit_with_instructions(output_lines, selection, opts, ...)
},
},
})
-- instructions_input:map("n", "<C-CR>", function()
-- local instructions = vim.api.nvim_buf_get_lines(instructions_input.bufnr, 0, -1, false)
-- instructions_input.opts.on_submit(table.concat(instructions, "\n"))
-- end)

layout =
layouts.edit_with_no_layout(layout, self, input_window, instructions_input, output_window, parameters_panel, {
Expand Down
1 change: 0 additions & 1 deletion lua/ogpt/parameters.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local pickers = require("telescope.pickers")
local SimpleWindow = require("ogpt.common.simple_window")
local Utils = require("ogpt.utils")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
Expand Down

0 comments on commit b7e3bd4

Please sign in to comment.