chore: run stylua [generated] (#460)

* chore: add stylua

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

* chore: running stylua

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>

---------

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
Aaron Pham
2024-09-03 04:19:54 -04:00
committed by GitHub
parent 4ad913435c
commit e8c71d931e
28 changed files with 608 additions and 1181 deletions

View File

@@ -10,9 +10,7 @@ local M = {}
setmetatable(M, {
__index = function(t, k)
local ok, lazyutil = pcall(require, "lazy.core.util")
if ok and lazyutil[k] then
return lazyutil[k]
end
if ok and lazyutil[k] then return lazyutil[k] end
---@diagnostic disable-next-line: no-unknown
t[k] = require("avante.utils." .. k)
@@ -25,15 +23,11 @@ setmetatable(M, {
---@return boolean
M.has = function(plugin)
local ok, LazyConfig = pcall(require, "lazy.core.config")
if ok then
return LazyConfig.plugins[plugin] ~= nil
end
if ok then return LazyConfig.plugins[plugin] ~= nil end
return package.loaded[plugin] ~= nil
end
M.is_win = function()
return jit.os:find("Windows") ~= nil
end
M.is_win = function() return jit.os:find "Windows" ~= nil end
---@return "linux" | "darwin" | "windows"
M.get_os_name = function()
@@ -58,12 +52,12 @@ M.shell_run = function(input_cmd)
local cmd
-- powershell then we can just run the cmd
if shell:match("powershell") or shell:match("pwsh") then
if shell:match "powershell" or shell:match "pwsh" then
cmd = input_cmd
elseif vim.fn.has("wsl") > 0 then
elseif vim.fn.has "wsl" > 0 then
-- wsl: powershell.exe -Command 'command "/path"'
cmd = "powershell.exe -NoProfile -Command '" .. input_cmd:gsub("'", '"') .. "'"
elseif vim.fn.has("win32") > 0 then
elseif vim.fn.has "win32" > 0 then
cmd = 'powershell.exe -NoProfile -Command "' .. input_cmd:gsub('"', "'") .. '"'
else
-- linux and macos we wil just do sh -c
@@ -138,9 +132,7 @@ M.safe_keymap_set = function(mode, lhs, rhs, opts)
---@cast modes -string
---@param m string
modes = vim.tbl_filter(function(m)
return not (Keys.have and Keys:have(lhs, m))
end, modes)
modes = vim.tbl_filter(function(m) return not (Keys.have and Keys:have(lhs, m)) end, modes)
-- don't create keymap if a lazy keys handler exists
if #modes > 0 then
@@ -157,16 +149,12 @@ end
---@param str string
---@param opts? {suffix?: string, prefix?: string}
function M.trim(str, opts)
if not opts then
return str
end
if not opts then return str end
local res = str
if opts.suffix then
res = str:sub(#str - #opts.suffix + 1) == opts.suffix and str:sub(1, #str - #opts.suffix) or str
end
if opts.prefix then
res = str:sub(1, #opts.prefix) == opts.prefix and str:sub(#opts.prefix + 1) or str
end
if opts.prefix then res = str:sub(1, #opts.prefix) == opts.prefix and str:sub(#opts.prefix + 1) or str end
return res
end
@@ -178,15 +166,13 @@ end
---Get the selected content and range in Visual mode
---@return avante.SelectionResult | nil Selected content and range
function M.get_visual_selection_and_range()
local Range = require("avante.range")
local SelectionResult = require("avante.selection_result")
local Range = require "avante.range"
local SelectionResult = require "avante.selection_result"
if not M.in_visual_mode() then
return nil
end
if not M.in_visual_mode() then return nil end
-- Get the start and end positions of Visual mode
local start_pos = vim.fn.getpos("v")
local end_pos = vim.fn.getpos(".")
local start_pos = vim.fn.getpos "v"
local end_pos = vim.fn.getpos "."
-- Get the start and end line and column numbers
local start_line = start_pos[2]
local start_col = start_pos[3]
@@ -219,9 +205,7 @@ function M.get_visual_selection_and_range()
content = lines
end
end
if not content then
return nil
end
if not content then return nil end
-- Return the selected content and range
return SelectionResult.new(content, range)
end
@@ -231,16 +215,12 @@ end
---@param end_ integer
---@param buf integer?
---@return string[]
function M.get_buf_lines(start, end_, buf)
return api.nvim_buf_get_lines(buf or 0, start, end_, false)
end
function M.get_buf_lines(start, end_, buf) return api.nvim_buf_get_lines(buf or 0, start, end_, false) end
---Get cursor row and column as (1, 0) based
---@param win_id integer?
---@return integer, integer
function M.get_cursor_pos(win_id)
return unpack(api.nvim_win_get_cursor(win_id or 0))
end
function M.get_cursor_pos(win_id) return unpack(api.nvim_win_get_cursor(win_id or 0)) end
---Check if the buffer is likely to have actionable conflict markers
---@param bufnr integer?
@@ -253,9 +233,7 @@ end
---@param name string?
---@return table<string, string>
function M.get_hl(name)
if not name then
return {}
end
if not name then return {} end
return api.nvim_get_hl(0, { name = name })
end
@@ -278,9 +256,7 @@ end
function M.norm(path)
if path:sub(1, 1) == "~" then
local home = vim.uv.os_homedir()
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
home = home:sub(1, -2)
end
if home:sub(-1) == "\\" or home:sub(-1) == "/" then home = home:sub(1, -2) end
path = home .. path:sub(2)
end
path = path:gsub("\\", "/"):gsub("/+", "/")
@@ -290,38 +266,25 @@ end
---@param msg string|string[]
---@param opts? LazyNotifyOpts
function M.notify(msg, opts)
if vim.in_fast_event() then
return vim.schedule(function()
M.notify(msg, opts)
end)
end
if vim.in_fast_event() then return vim.schedule(function() M.notify(msg, opts) end) end
opts = opts or {}
if type(msg) == "table" then
---@diagnostic disable-next-line: no-unknown
msg = table.concat(
vim.tbl_filter(function(line)
return line or false
end, msg),
"\n"
)
msg = table.concat(vim.tbl_filter(function(line) return line or false end, msg), "\n")
end
---@diagnostic disable-next-line: undefined-field
if opts.stacktrace then
---@diagnostic disable-next-line: undefined-field
msg = msg .. M.pretty_trace({ level = opts.stacklevel or 2 })
msg = msg .. M.pretty_trace { level = opts.stacklevel or 2 }
end
local lang = opts.lang or "markdown"
---@diagnostic disable-next-line: undefined-field
local n = opts.once and vim.notify_once or vim.notify
n(msg, opts.level or vim.log.levels.INFO, {
on_open = function(win)
local ok = pcall(function()
vim.treesitter.language.add("markdown")
end)
if not ok then
pcall(require, "nvim-treesitter")
end
local ok = pcall(function() vim.treesitter.language.add "markdown" end)
if not ok then pcall(require, "nvim-treesitter") end
vim.wo[win].conceallevel = 3
vim.wo[win].concealcursor = ""
vim.wo[win].spell = false
@@ -354,9 +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").options.silent_warning then return end
opts = opts or {}
opts.level = vim.log.levels.WARN
@@ -366,13 +327,9 @@ end
---@param msg string|table
---@param opts? LazyNotifyOpts
function M.debug(msg, opts)
if not require("avante.config").options.debug then
return
end
if not require("avante.config").options.debug then return end
opts = opts or {}
if opts.title then
opts.title = "avante.nvim: " .. opts.title
end
if opts.title then opts.title = "avante.nvim: " .. opts.title end
if type(msg) == "string" then
M.notify(msg, opts)
else
@@ -383,9 +340,7 @@ end
function M.tbl_indexof(tbl, value)
for i, v in ipairs(tbl) do
if v == value then
return i
end
if v == value then return i end
end
return nil
end
@@ -396,9 +351,7 @@ function M.update_win_options(winid, opt_name, key, value)
if cur_opt_value:find(key .. ":") then
cur_opt_value = cur_opt_value:gsub(key .. ":[^,]*", key .. ":" .. value)
else
if #cur_opt_value > 0 then
cur_opt_value = cur_opt_value .. ","
end
if #cur_opt_value > 0 then cur_opt_value = cur_opt_value .. "," end
cur_opt_value = cur_opt_value .. key .. ":" .. value
end
@@ -407,15 +360,11 @@ end
function M.get_win_options(winid, opt_name, key)
local cur_opt_value = api.nvim_get_option_value(opt_name, { win = winid })
if not cur_opt_value then
return
end
if not cur_opt_value then return end
local pieces = vim.split(cur_opt_value, ",")
for _, piece in ipairs(pieces) do
local kv_pair = vim.split(piece, ":")
if kv_pair[1] == key then
return kv_pair[2]
end
if kv_pair[1] == key then return kv_pair[2] end
end
end
@@ -425,7 +374,7 @@ function M.unlock_buf(bufnr)
end
function M.lock_buf(bufnr)
vim.cmd("stopinsert")
vim.cmd "stopinsert"
vim.bo[bufnr].modified = false
vim.bo[bufnr].modifiable = false
end
@@ -453,14 +402,10 @@ end
M.buf_list_wins = function(bufnr)
local wins = {}
if not bufnr or bufnr == 0 then
bufnr = api.nvim_get_current_buf()
end
if not bufnr or bufnr == 0 then bufnr = api.nvim_get_current_buf() end
for _, winnr in ipairs(api.nvim_list_wins()) do
if api.nvim_win_is_valid(winnr) and api.nvim_win_get_buf(winnr) == bufnr then
table.insert(wins, winnr)
end
if api.nvim_win_is_valid(winnr) and api.nvim_win_get_buf(winnr) == bufnr then table.insert(wins, winnr) end
end
return wins
@@ -468,25 +413,17 @@ end
local sidebar_buffer_var_name = "is_avante_sidebar_buffer"
function M.mark_as_sidebar_buffer(bufnr)
api.nvim_buf_set_var(bufnr, sidebar_buffer_var_name, true)
end
function M.mark_as_sidebar_buffer(bufnr) api.nvim_buf_set_var(bufnr, sidebar_buffer_var_name, true) end
function M.is_sidebar_buffer(bufnr)
local ok, v = pcall(api.nvim_buf_get_var, bufnr, sidebar_buffer_var_name)
if not ok then
return false
end
if not ok then return false end
return v == true
end
function M.trim_spaces(s)
return s:match("^%s*(.-)%s*$")
end
function M.trim_spaces(s) return s:match "^%s*(.-)%s*$" end
function M.fallback(v, default_value)
return type(v) == "nil" and default_value or v
end
function M.fallback(v, default_value) return type(v) == "nil" and default_value or v end
-- luacheck: push no max comment line length
---@param type_name "'nil'" | "'number'" | "'string'" | "'boolean'" | "'table'" | "'function'" | "'thread'" | "'userdata'" | "'list'" | '"map"'
@@ -494,13 +431,9 @@ end
function M.is_type(type_name, v)
---@diagnostic disable-next-line: deprecated
local islist = vim.islist or vim.tbl_islist
if type_name == "list" then
return islist(v)
end
if type_name == "list" then return islist(v) end
if type_name == "map" then
return type(v) == "table" and not islist(v)
end
if type_name == "map" then return type(v) == "table" and not islist(v) end
return type(v) == type_name
end
@@ -508,20 +441,14 @@ end
---@param code string
---@return string
function M.get_indentation(code)
return code:match("^%s*") or ""
end
function M.get_indentation(code) return code:match "^%s*" or "" end
--- remove indentation from code: spaces or tabs
function M.remove_indentation(code)
return code:gsub("^%s*", "")
end
function M.remove_indentation(code) return code:gsub("^%s*", "") end
local function relative_path(absolute)
local relative = fn.fnamemodify(absolute, ":.")
if string.sub(relative, 0, 1) == "/" then
return fn.fnamemodify(absolute, ":t")
end
if string.sub(relative, 0, 1) == "/" then return fn.fnamemodify(absolute, ":t") end
return relative
end
@@ -558,9 +485,7 @@ function M.prepend_line_number(content, start_line)
return table.concat(result, "\n")
end
function M.trim_line_number(line)
return line:gsub("^L%d+: ", "")
end
function M.trim_line_number(line) return line:gsub("^L%d+: ", "") end
function M.trim_all_line_numbers(content)
return vim
@@ -569,7 +494,7 @@ function M.trim_all_line_numbers(content)
local new_line = M.trim_line_number(line)
return new_line
end)
:join("\n")
:join "\n"
end
function M.debounce(func, delay)
@@ -578,9 +503,7 @@ function M.debounce(func, delay)
return function(...)
local args = { ... }
if timer_id then
fn.timer_stop(timer_id)
end
if timer_id then fn.timer_stop(timer_id) end
timer_id = fn.timer_start(delay, function()
func(unpack(args))

View File

@@ -1,12 +1,10 @@
-- COPIED and MODIFIED from https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/util/root.lua
local Utils = require("avante.utils")
local Utils = require "avante.utils"
---@class avante.utils.root
---@overload fun(): string
local M = setmetatable({}, {
__call = function(m)
return m.get()
end,
__call = function(m) return m.get() end,
})
---@class AvanteRoot
@@ -22,25 +20,19 @@ M.spec = { "lsp", { ".git", "lua" }, "cwd" }
M.detectors = {}
function M.detectors.cwd()
return { vim.uv.cwd() }
end
function M.detectors.cwd() return { vim.uv.cwd() } end
---@param buf number
function M.detectors.lsp(buf)
local bufpath = M.bufpath(buf)
if not bufpath then
return {}
end
if not bufpath then return {} end
local roots = {} ---@type string[]
for _, client in pairs(Utils.lsp.get_clients({ bufnr = buf })) do
for _, client in pairs(Utils.lsp.get_clients { bufnr = buf }) do
local workspace = client.config.workspace_folders
for _, ws in pairs(workspace or {}) do
roots[#roots + 1] = vim.uri_to_fname(ws.uri)
end
if client.root_dir then
roots[#roots + 1] = client.root_dir
end
if client.root_dir then roots[#roots + 1] = client.root_dir end
end
return vim.tbl_filter(function(path)
path = Utils.norm(path)
@@ -54,30 +46,20 @@ function M.detectors.pattern(buf, patterns)
local path = M.bufpath(buf) or vim.uv.cwd()
local pattern = vim.fs.find(function(name)
for _, p in ipairs(patterns) do
if name == p then
return true
end
if p:sub(1, 1) == "*" and name:find(vim.pesc(p:sub(2)) .. "$") then
return true
end
if name == p then return true end
if p:sub(1, 1) == "*" and name:find(vim.pesc(p:sub(2)) .. "$") then return true end
end
return false
end, { path = path, upward = true })[1]
return pattern and { vim.fs.dirname(pattern) } or {}
end
function M.bufpath(buf)
return M.realpath(vim.api.nvim_buf_get_name(assert(buf)))
end
function M.bufpath(buf) return M.realpath(vim.api.nvim_buf_get_name(assert(buf))) end
function M.cwd()
return M.realpath(vim.uv.cwd()) or ""
end
function M.cwd() return M.realpath(vim.uv.cwd()) or "" end
function M.realpath(path)
if path == "" or path == nil then
return nil
end
if path == "" or path == nil then return nil end
path = vim.uv.fs_realpath(path) or path
return Utils.norm(path)
end
@@ -90,9 +72,7 @@ function M.resolve(spec)
elseif type(spec) == "function" then
return spec
end
return function(buf)
return M.detectors.pattern(buf, spec)
end
return function(buf) return M.detectors.pattern(buf, spec) end
end
---@param opts? { buf?: number, spec?: AvanteRootSpec[], all?: boolean }
@@ -109,18 +89,12 @@ function M.detect(opts)
local roots = {} ---@type string[]
for _, p in ipairs(paths) do
local pp = M.realpath(p)
if pp and not vim.tbl_contains(roots, pp) then
roots[#roots + 1] = pp
end
if pp and not vim.tbl_contains(roots, pp) then roots[#roots + 1] = pp end
end
table.sort(roots, function(a, b)
return #a > #b
end)
table.sort(roots, function(a, b) return #a > #b end)
if #roots > 0 then
ret[#ret + 1] = { spec = spec, paths = roots }
if opts.all == false then
break
end
if opts.all == false then break end
end
end
return ret
@@ -141,13 +115,11 @@ function M.get(opts)
local buf = opts.buf or vim.api.nvim_get_current_buf()
local ret = M.cache[buf]
if not ret then
local roots = M.detect({ all = false, buf = buf })
local roots = M.detect { all = false, buf = buf }
ret = roots[1] and roots[1].paths[1] or vim.uv.cwd()
M.cache[buf] = ret
end
if opts and opts.normalize then
return ret
end
if opts and opts.normalize then return ret end
return Utils.is_win() and ret:gsub("/", "\\") or ret
end

View File

@@ -1,5 +1,5 @@
--Taken from https://github.com/jackMort/ChatGPT.nvim/blob/main/lua/chatgpt/flows/chat/tokens.lua
local Tokenizer = require("avante.tokenizers")
local Tokenizer = require "avante.tokenizers"
---@class avante.utils.tokens
local Tokens = {}
@@ -13,13 +13,11 @@ local cost_per_token = {
---@param text string The text to calculate the number of tokens for.
---@return integer The number of tokens in the given text.
function Tokens.calculate_tokens(text)
if Tokenizer.available() then
return Tokenizer.count(text)
end
if Tokenizer.available() then return Tokenizer.count(text) end
local tokens = 0
local current_token = ""
for char in text:gmatch(".") do
for char in text:gmatch "." do
if char == " " or char == "\n" then
if current_token ~= "" then
tokens = tokens + 1
@@ -29,9 +27,7 @@ function Tokens.calculate_tokens(text)
current_token = current_token .. char
end
end
if current_token ~= "" then
tokens = tokens + 1
end
if current_token ~= "" then tokens = tokens + 1 end
return tokens
end
@@ -48,8 +44,6 @@ end
-- @param tokens The number of tokens to calculate the cost of.
-- @param model The model to use to calculate the cost.
-- @return The cost of the given number of tokens in dollars.
function Tokens.usage_in_dollars(tokens, model)
return tokens * cost_per_token[model or "davinci"]
end
function Tokens.usage_in_dollars(tokens, model) return tokens * cost_per_token[model or "davinci"] end
return Tokens