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

Fix async reading config #300

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 18 additions & 18 deletions lua/chatgpt/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,24 @@ end

local function loadConfigFromCommand(command, optionName, callback, defaultValue)
local cmd = splitCommandIntoTable(command)
job
:new({
command = cmd[1],
args = vim.list_slice(cmd, 2, #cmd),
on_exit = function(j, exit_code)
if exit_code ~= 0 then
logger.warn("Config '" .. optionName .. "' did not return a value when executed")
return
end
local value = j:result()[1]:gsub("%s+$", "")
if value ~= nil and value ~= "" then
callback(value)
elseif defaultValue ~= nil and defaultValue ~= "" then
callback(defaultValue)
end
end,
})
:start()
local j = job:new({
command = cmd[1],
args = vim.list_slice(cmd, 2, #cmd),
on_exit = function(j, exit_code)
if exit_code ~= 0 then
logger.warn("Config '" .. optionName .. "' did not return a value when executed")
return
end
local value = j:result()[1]:gsub("%s+$", "")
if value ~= nil and value ~= "" then
callback(value)
elseif defaultValue ~= nil and defaultValue ~= "" then
callback(defaultValue)
end
end,
})
j:start()
j:wait() -- we should wait the job to finish. Otherwise we can't ensure correctly reading config before using it.
end

local function loadConfigFromEnv(envName, configName)
Expand Down
5 changes: 5 additions & 0 deletions lua/chatgpt/code_edits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,12 @@ M.edit_with_instructions = function(output_lines, bufnr, selection, ...)
for _, winid in ipairs({ input_window.winid, output_window.winid }) do
vim.api.nvim_set_current_win(winid)
if diff_mode then
-- set local wrap to be previous option to make it mroe readable(wrap=true is often more readable in diff mode).
local previous_wrap = vim.o.wrap
vim.api.nvim_command("diffthis")
if vim.o.wrap ~= previous_wrap then
vim.o.wrap = previous_wrap
end
else
vim.api.nvim_command("diffoff")
end
Expand Down
Loading