Skip to content

Commit

Permalink
chore(secrets): support table of string (#500)
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
  • Loading branch information
aarnphm authored Sep 4, 2024
1 parent b46eec2 commit d7d476d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 116 deletions.
9 changes: 1 addition & 8 deletions lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function M.setup(opts)
},
}
)
if not M.options.silent_warning then
if not M.options.silent_warning == nil then
-- set silent_warning to true if debug is false
M.options.silent_warning = not M.options.debug
end
Expand Down Expand Up @@ -307,11 +307,4 @@ M.BASE_PROVIDER_KEYS = {
"use_xml_format",
}

---@return {width: integer, height: integer}
M.get_sidebar_layout_options = function()
local width = M.get_window_width()
local height = vim.o.lines
return { width = width, height = height }
end

return M
110 changes: 64 additions & 46 deletions lua/avante/providers/init.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
local api = vim.api
local api, fn = vim.api, vim.fn

local Config = require("avante.config")
local Utils = require("avante.utils")
local Dressing = require("avante.ui.dressing")

local DressingConfig = {
conceal_char = "*",
filetype = "DressingInput",
close_window = function() require("dressing.input").close() end,
}
local DressingState = { winid = nil, input_winid = nil, input_bufnr = nil }

---@class AvanteHandlerOptions: table<[string], string>
---@field on_chunk AvanteChunkParser
Expand Down Expand Up @@ -87,9 +93,6 @@ local M = {}
---@class EnvironmentHandler
local E = {}

---@private
E._once = false

---@private
---@type table<string, string>
E.cache = {}
Expand All @@ -100,9 +103,11 @@ E.parse_envvar = function(Opts)
local api_key_name = Opts.api_key_name
if api_key_name == nil then error("Requires api_key_name") end

if E.cache[api_key_name] ~= nil then return E.cache[api_key_name] end
local cache_key = type(api_key_name) == "table" and table.concat(api_key_name, "__") or api_key_name

if E.cache[cache_key] ~= nil then return E.cache[cache_key] end

local cmd = api_key_name:match("^cmd:(.*)")
local cmd = type(api_key_name) == "table" and api_key_name or api_key_name:match("^cmd:(.*)")

local key = nil

Expand All @@ -114,33 +119,27 @@ E.parse_envvar = function(Opts)
if key ~= nil then
---@diagnostic disable: no-unknown
E.cache[Opts._shellenv] = key
E.cache[api_key_name] = key
E.cache[cache_key] = key
vim.g.avante_login = true
return key
end
end

if type(cmd) == "string" then cmd = vim.split(cmd, " ", { trimempty = true }) end

local exit_codes = { 0 }
local ok, job_or_err = pcall(
vim.system,
vim.split(cmd, " ", { trimempty = true }),
{ text = true },
function(result)
local code = result.code
local stderr = result.stderr or ""
local stdout = result.stdout and vim.split(result.stdout, "\n") or {}
if vim.tbl_contains(exit_codes, code) then
key = stdout[1]
E.cache[api_key_name] = key
vim.g.avante_login = true
else
Utils.error(
"Failed to get API key: (error code" .. code .. ")\n" .. stderr,
{ once = true, title = "Avante" }
)
end
local ok, job_or_err = pcall(vim.system, cmd, { text = true }, function(result)
local code = result.code
local stderr = result.stderr or ""
local stdout = result.stdout and vim.split(result.stdout, "\n") or {}
if vim.tbl_contains(exit_codes, code) then
key = stdout[1]
E.cache[cache_key] = key
vim.g.avante_login = true
else
Utils.error("Failed to get API key: (error code" .. code .. ")\n" .. stderr, { once = true, title = "Avante" })
end
)
end)

if not ok then
error("failed to run command: " .. cmd .. "\n" .. job_or_err)
Expand All @@ -151,7 +150,7 @@ E.parse_envvar = function(Opts)
end

if key ~= nil then
E.cache[api_key_name] = key
E.cache[cache_key] = key
vim.g.avante_login = true
end

Expand All @@ -173,7 +172,7 @@ E.setup = function(opts)
opts.provider.setup()

-- check if var is a all caps string
if var == M.AVANTE_INTERNAL_KEY or var:match("^cmd:(.*)") then return end
if var == M.AVANTE_INTERNAL_KEY or type(var) == "table" or var:match("^cmd:(.*)") then return end

local refresh = opts.refresh or false

Expand All @@ -185,7 +184,7 @@ E.setup = function(opts)
vim.g.avante_login = true
else
if not opts.provider.has() then
Utils.warn("Failed to set " .. var .. ". Avante won't work as expected", { once = true, title = "Avante" })
Utils.warn("Failed to set " .. var .. ". Avante won't work as expected", { once = true })
end
end
end
Expand All @@ -207,30 +206,49 @@ E.setup = function(opts)
"DressingInput",
"noice",
}

if not vim.tbl_contains(exclude_filetypes, vim.bo.filetype) and not opts.provider.has() then
Dressing.initialize_input_buffer({
opts = { prompt = "Enter " .. var .. ": " },
on_confirm = on_confirm,
})
DressingState.winid = api.nvim_get_current_win()
vim.ui.input({ default = "", prompt = "Enter " .. var .. ": " }, on_confirm)
for _, winid in ipairs(api.nvim_list_wins()) do
local bufnr = api.nvim_win_get_buf(winid)
if vim.bo[bufnr].filetype == DressingConfig.filetype then
DressingState.input_winid = winid
DressingState.input_bufnr = bufnr
vim.wo[winid].conceallevel = 2
vim.wo[winid].concealcursor = "nvi"
break
end
end

local prompt_length = api.nvim_strwidth(fn.prompt_getprompt(DressingState.input_bufnr))
api.nvim_buf_call(
DressingState.input_bufnr,
function()
vim.cmd(string.format(
[[
syn region SecretValue start=/^/ms=s+%s end=/$/ contains=SecretChar
syn match SecretChar /./ contained conceal %s
]],
prompt_length,
"cchar=*"
))
end
)
end
end, 200)
end

if refresh then
mount_dressing_buffer()
return
end
if refresh then return mount_dressing_buffer() end

if not E._once then
E._once = true
api.nvim_create_autocmd({ "BufEnter", "BufWinEnter", "WinEnter" }, {
pattern = "*",
once = true,
callback = mount_dressing_buffer,
})
end
api.nvim_create_autocmd("User", {
pattern = E.REQUEST_LOGIN_PATTERN,
callback = mount_dressing_buffer,
})
end

E.REQUEST_LOGIN_PATTERN = "AvanteRequestLogin"

---@param provider Provider
E.is_local = function(provider)
local cur = M.get_config(provider)
Expand Down
6 changes: 6 additions & 0 deletions lua/avante/selection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local Utils = require("avante.utils")
local Config = require("avante.config")
local Llm = require("avante.llm")
local Highlights = require("avante.highlights")
local Provider = require("avante.providers")

local api = vim.api
local fn = vim.fn
Expand Down Expand Up @@ -260,6 +261,11 @@ end
function Selection:create_editing_input()
self:close_editing_input()

if not vim.g.avante_login or vim.g.avante_login == false then
api.nvim_exec_autocmds("User", { pattern = Provider.env.REQUEST_LOGIN_PATTERN })
vim.g.avante_login = true
end

local code_bufnr = api.nvim_get_current_buf()
local code_wind = api.nvim_get_current_win()
self.cursor_pos = api.nvim_win_get_cursor(code_wind)
Expand Down
6 changes: 6 additions & 0 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local fn = vim.fn
local Split = require("nui.split")
local event = require("nui.utils.autocmd").event

local Provider = require("avante.providers")
local Path = require("avante.path")
local Config = require("avante.config")
local Diff = require("avante.diff")
Expand Down Expand Up @@ -82,6 +83,11 @@ function Sidebar:open()
self:focus()
end

if not vim.g.avante_login or vim.g.avante_login == false then
api.nvim_exec_autocmds("User", { pattern = Provider.env.REQUEST_LOGIN_PATTERN })
vim.g.avante_login = true
end

vim.cmd("wincmd =")
return self
end
Expand Down
5 changes: 5 additions & 0 deletions lua/avante/suggestion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local Utils = require("avante.utils")
local Llm = require("avante.llm")
local Highlights = require("avante.highlights")
local Config = require("avante.config")
local Provider = require("avante.providers")
local api = vim.api
local fn = vim.fn

Expand Down Expand Up @@ -36,6 +37,10 @@ function Suggestion:new(id)
self._timer = nil
self._contexts = {}
if Config.behaviour.auto_suggestions then
if not vim.g.avante_login or vim.g.avante_login == false then
api.nvim_exec_autocmds("User", { pattern = Provider.env.REQUEST_LOGIN_PATTERN })
vim.g.avante_login = true
end
self:setup_mappings()
self:setup_autocmds()
end
Expand Down
61 changes: 0 additions & 61 deletions lua/avante/ui/dressing.lua

This file was deleted.

2 changes: 1 addition & 1 deletion lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ end
---@param msg string|string[]
---@param opts? LazyNotifyOpts
function M.warn(msg, opts)
if require("avante.config").options.silent_warning then return end
if require("avante.config").silent_warning then return end

opts = opts or {}
opts.level = vim.log.levels.WARN
Expand Down

0 comments on commit d7d476d

Please sign in to comment.