chore(secrets): support table of string (#500)
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
parent
b46eec2a84
commit
d7d476ddf5
@ -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
|
-- set silent_warning to true if debug is false
|
||||||
M.options.silent_warning = not M.options.debug
|
M.options.silent_warning = not M.options.debug
|
||||||
end
|
end
|
||||||
@ -307,11 +307,4 @@ M.BASE_PROVIDER_KEYS = {
|
|||||||
"use_xml_format",
|
"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
|
return M
|
||||||
|
@ -1,8 +1,14 @@
|
|||||||
local api = vim.api
|
local api, fn = vim.api, vim.fn
|
||||||
|
|
||||||
local Config = require("avante.config")
|
local Config = require("avante.config")
|
||||||
local Utils = require("avante.utils")
|
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>
|
---@class AvanteHandlerOptions: table<[string], string>
|
||||||
---@field on_chunk AvanteChunkParser
|
---@field on_chunk AvanteChunkParser
|
||||||
@ -87,9 +93,6 @@ local M = {}
|
|||||||
---@class EnvironmentHandler
|
---@class EnvironmentHandler
|
||||||
local E = {}
|
local E = {}
|
||||||
|
|
||||||
---@private
|
|
||||||
E._once = false
|
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
---@type table<string, string>
|
---@type table<string, string>
|
||||||
E.cache = {}
|
E.cache = {}
|
||||||
@ -100,9 +103,11 @@ E.parse_envvar = function(Opts)
|
|||||||
local api_key_name = Opts.api_key_name
|
local api_key_name = Opts.api_key_name
|
||||||
if api_key_name == nil then error("Requires api_key_name") end
|
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
|
||||||
|
|
||||||
local cmd = api_key_name:match("^cmd:(.*)")
|
if E.cache[cache_key] ~= nil then return E.cache[cache_key] end
|
||||||
|
|
||||||
|
local cmd = type(api_key_name) == "table" and api_key_name or api_key_name:match("^cmd:(.*)")
|
||||||
|
|
||||||
local key = nil
|
local key = nil
|
||||||
|
|
||||||
@ -114,33 +119,27 @@ E.parse_envvar = function(Opts)
|
|||||||
if key ~= nil then
|
if key ~= nil then
|
||||||
---@diagnostic disable: no-unknown
|
---@diagnostic disable: no-unknown
|
||||||
E.cache[Opts._shellenv] = key
|
E.cache[Opts._shellenv] = key
|
||||||
E.cache[api_key_name] = key
|
E.cache[cache_key] = key
|
||||||
vim.g.avante_login = true
|
vim.g.avante_login = true
|
||||||
return key
|
return key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if type(cmd) == "string" then cmd = vim.split(cmd, " ", { trimempty = true }) end
|
||||||
|
|
||||||
local exit_codes = { 0 }
|
local exit_codes = { 0 }
|
||||||
local ok, job_or_err = pcall(
|
local ok, job_or_err = pcall(vim.system, cmd, { text = true }, function(result)
|
||||||
vim.system,
|
|
||||||
vim.split(cmd, " ", { trimempty = true }),
|
|
||||||
{ text = true },
|
|
||||||
function(result)
|
|
||||||
local code = result.code
|
local code = result.code
|
||||||
local stderr = result.stderr or ""
|
local stderr = result.stderr or ""
|
||||||
local stdout = result.stdout and vim.split(result.stdout, "\n") or {}
|
local stdout = result.stdout and vim.split(result.stdout, "\n") or {}
|
||||||
if vim.tbl_contains(exit_codes, code) then
|
if vim.tbl_contains(exit_codes, code) then
|
||||||
key = stdout[1]
|
key = stdout[1]
|
||||||
E.cache[api_key_name] = key
|
E.cache[cache_key] = key
|
||||||
vim.g.avante_login = true
|
vim.g.avante_login = true
|
||||||
else
|
else
|
||||||
Utils.error(
|
Utils.error("Failed to get API key: (error code" .. code .. ")\n" .. stderr, { once = true, title = "Avante" })
|
||||||
"Failed to get API key: (error code" .. code .. ")\n" .. stderr,
|
|
||||||
{ once = true, title = "Avante" }
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
)
|
|
||||||
|
|
||||||
if not ok then
|
if not ok then
|
||||||
error("failed to run command: " .. cmd .. "\n" .. job_or_err)
|
error("failed to run command: " .. cmd .. "\n" .. job_or_err)
|
||||||
@ -151,7 +150,7 @@ E.parse_envvar = function(Opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if key ~= nil then
|
if key ~= nil then
|
||||||
E.cache[api_key_name] = key
|
E.cache[cache_key] = key
|
||||||
vim.g.avante_login = true
|
vim.g.avante_login = true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -173,7 +172,7 @@ E.setup = function(opts)
|
|||||||
opts.provider.setup()
|
opts.provider.setup()
|
||||||
|
|
||||||
-- check if var is a all caps string
|
-- 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
|
local refresh = opts.refresh or false
|
||||||
|
|
||||||
@ -185,7 +184,7 @@ E.setup = function(opts)
|
|||||||
vim.g.avante_login = true
|
vim.g.avante_login = true
|
||||||
else
|
else
|
||||||
if not opts.provider.has() then
|
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
|
end
|
||||||
end
|
end
|
||||||
@ -207,30 +206,49 @@ E.setup = function(opts)
|
|||||||
"DressingInput",
|
"DressingInput",
|
||||||
"noice",
|
"noice",
|
||||||
}
|
}
|
||||||
|
|
||||||
if not vim.tbl_contains(exclude_filetypes, vim.bo.filetype) and not opts.provider.has() then
|
if not vim.tbl_contains(exclude_filetypes, vim.bo.filetype) and not opts.provider.has() then
|
||||||
Dressing.initialize_input_buffer({
|
DressingState.winid = api.nvim_get_current_win()
|
||||||
opts = { prompt = "Enter " .. var .. ": " },
|
vim.ui.input({ default = "", prompt = "Enter " .. var .. ": " }, on_confirm)
|
||||||
on_confirm = 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
|
||||||
end, 200)
|
end, 200)
|
||||||
end
|
end
|
||||||
|
|
||||||
if refresh then
|
if refresh then return mount_dressing_buffer() end
|
||||||
mount_dressing_buffer()
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if not E._once then
|
api.nvim_create_autocmd("User", {
|
||||||
E._once = true
|
pattern = E.REQUEST_LOGIN_PATTERN,
|
||||||
api.nvim_create_autocmd({ "BufEnter", "BufWinEnter", "WinEnter" }, {
|
|
||||||
pattern = "*",
|
|
||||||
once = true,
|
|
||||||
callback = mount_dressing_buffer,
|
callback = mount_dressing_buffer,
|
||||||
})
|
})
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
E.REQUEST_LOGIN_PATTERN = "AvanteRequestLogin"
|
||||||
|
|
||||||
---@param provider Provider
|
---@param provider Provider
|
||||||
E.is_local = function(provider)
|
E.is_local = function(provider)
|
||||||
local cur = M.get_config(provider)
|
local cur = M.get_config(provider)
|
||||||
|
@ -2,6 +2,7 @@ local Utils = require("avante.utils")
|
|||||||
local Config = require("avante.config")
|
local Config = require("avante.config")
|
||||||
local Llm = require("avante.llm")
|
local Llm = require("avante.llm")
|
||||||
local Highlights = require("avante.highlights")
|
local Highlights = require("avante.highlights")
|
||||||
|
local Provider = require("avante.providers")
|
||||||
|
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
local fn = vim.fn
|
local fn = vim.fn
|
||||||
@ -260,6 +261,11 @@ end
|
|||||||
function Selection:create_editing_input()
|
function Selection:create_editing_input()
|
||||||
self:close_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_bufnr = api.nvim_get_current_buf()
|
||||||
local code_wind = api.nvim_get_current_win()
|
local code_wind = api.nvim_get_current_win()
|
||||||
self.cursor_pos = api.nvim_win_get_cursor(code_wind)
|
self.cursor_pos = api.nvim_win_get_cursor(code_wind)
|
||||||
|
@ -4,6 +4,7 @@ local fn = vim.fn
|
|||||||
local Split = require("nui.split")
|
local Split = require("nui.split")
|
||||||
local event = require("nui.utils.autocmd").event
|
local event = require("nui.utils.autocmd").event
|
||||||
|
|
||||||
|
local Provider = require("avante.providers")
|
||||||
local Path = require("avante.path")
|
local Path = require("avante.path")
|
||||||
local Config = require("avante.config")
|
local Config = require("avante.config")
|
||||||
local Diff = require("avante.diff")
|
local Diff = require("avante.diff")
|
||||||
@ -82,6 +83,11 @@ function Sidebar:open()
|
|||||||
self:focus()
|
self:focus()
|
||||||
end
|
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 =")
|
vim.cmd("wincmd =")
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
@ -2,6 +2,7 @@ local Utils = require("avante.utils")
|
|||||||
local Llm = require("avante.llm")
|
local Llm = require("avante.llm")
|
||||||
local Highlights = require("avante.highlights")
|
local Highlights = require("avante.highlights")
|
||||||
local Config = require("avante.config")
|
local Config = require("avante.config")
|
||||||
|
local Provider = require("avante.providers")
|
||||||
local api = vim.api
|
local api = vim.api
|
||||||
local fn = vim.fn
|
local fn = vim.fn
|
||||||
|
|
||||||
@ -36,6 +37,10 @@ function Suggestion:new(id)
|
|||||||
self._timer = nil
|
self._timer = nil
|
||||||
self._contexts = {}
|
self._contexts = {}
|
||||||
if Config.behaviour.auto_suggestions then
|
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_mappings()
|
||||||
self:setup_autocmds()
|
self:setup_autocmds()
|
||||||
end
|
end
|
||||||
|
@ -1,61 +0,0 @@
|
|||||||
local api, fn = vim.api, vim.fn
|
|
||||||
|
|
||||||
---@class avante.Dressing
|
|
||||||
local H = {}
|
|
||||||
|
|
||||||
local C = {
|
|
||||||
filetype = "DressingInput",
|
|
||||||
conceal_char = "*",
|
|
||||||
close_window = function() require("dressing.input").close() end,
|
|
||||||
}
|
|
||||||
|
|
||||||
---@class avante.DressingState
|
|
||||||
local state = {
|
|
||||||
winid = nil, ---@type integer
|
|
||||||
input_winid = nil, ---@type integer
|
|
||||||
input_bufnr = nil, ---@type integer
|
|
||||||
}
|
|
||||||
|
|
||||||
---@param options {opts: table<string, any>, on_confirm: fun(value: string): nil} See vim.ui.input for more info
|
|
||||||
H.initialize_input_buffer = function(options)
|
|
||||||
state.winid = api.nvim_get_current_win()
|
|
||||||
vim.ui.input(vim.tbl_deep_extend("force", { default = "" }, options.opts), options.on_confirm)
|
|
||||||
for _, winid in ipairs(api.nvim_list_wins()) do
|
|
||||||
local bufnr = api.nvim_win_get_buf(winid)
|
|
||||||
if vim.bo[bufnr].filetype == C.filetype then
|
|
||||||
state.input_winid = winid
|
|
||||||
state.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(state.input_bufnr))
|
|
||||||
api.nvim_buf_call(
|
|
||||||
state.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
|
|
||||||
|
|
||||||
---@param switch_buffer? boolean To switch back original buffer, default to tru
|
|
||||||
H.teardown = function(switch_buffer)
|
|
||||||
switch_buffer = switch_buffer or true
|
|
||||||
|
|
||||||
if state.input_winid and api.nvim_win_is_valid(state.input_winid) then
|
|
||||||
C.close_window()
|
|
||||||
state.input_winid = nil
|
|
||||||
if switch_buffer then pcall(api.nvim_set_current_win, state.winid) end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return H
|
|
@ -317,7 +317,7 @@ end
|
|||||||
---@param msg string|string[]
|
---@param msg string|string[]
|
||||||
---@param opts? LazyNotifyOpts
|
---@param opts? LazyNotifyOpts
|
||||||
function M.warn(msg, opts)
|
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 = opts or {}
|
||||||
opts.level = vim.log.levels.WARN
|
opts.level = vim.log.levels.WARN
|
||||||
|
Loading…
x
Reference in New Issue
Block a user