chore: remove unused functions and simplify with utils (#103)

Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
Aaron Pham 2024-08-19 19:15:58 -04:00 committed by GitHub
parent 410824e357
commit 5f74c54e55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 44 deletions

View File

@ -253,7 +253,7 @@ function Sidebar:update_content(content, opts)
local scroll_to_bottom = function()
local last_line = api.nvim_buf_line_count(self.view.buf)
local current_lines = api.nvim_buf_get_lines(self.view.buf, last_line - 1, last_line, false)
local current_lines = Utils.get_buf_lines(last_line - 1, last_line, self.view.buf)
if #current_lines > 0 then
local last_line_content = current_lines[1]
@ -324,6 +324,7 @@ end
---@field end_line integer
---@field lang string
---@param buf integer
---@return AvanteCodeblock[]
local function parse_codeblocks(buf)
local codeblocks = {}
@ -331,7 +332,7 @@ local function parse_codeblocks(buf)
local start_line = nil
local lang = nil
local lines = api.nvim_buf_get_lines(buf, 0, -1, false)
local lines = Utils.get_buf_lines(0, -1, buf)
for i, line in ipairs(lines) do
if line:match("^```") then
-- parse language
@ -352,8 +353,8 @@ end
---@param codeblocks table<integer, any>
local function is_cursor_in_codeblock(codeblocks)
local cursor_pos = api.nvim_win_get_cursor(0)
local cursor_line = cursor_pos[1] - 1 -- 转换为 0-indexed 行号
local cursor_line, _ = Utils.get_cursor_pos()
cursor_line = cursor_line - 1 -- 转换为 0-indexed 行号
for _, block in ipairs(codeblocks) do
if cursor_line >= block.start_line and cursor_line <= block.end_line then
@ -542,17 +543,11 @@ local function get_conflict_content(content, snippets)
return result
end
---@return string
function Sidebar:get_code_content()
local lines = api.nvim_buf_get_lines(self.code.buf, 0, -1, false)
return table.concat(lines, "\n")
end
---@return string
function Sidebar:get_content_between_separators()
local separator = "---"
local cursor_line = api.nvim_win_get_cursor(0)[1]
local lines = api.nvim_buf_get_lines(self.view.buf, 0, -1, false)
local cursor_line, _ = Utils.get_cursor_pos()
local lines = Utils.get_buf_lines(0, -1, self.view.buf)
local start_line, end_line
for i = cursor_line, 1, -1 do
@ -601,7 +596,7 @@ function Sidebar:render()
end
local function apply()
local content = self:get_code_content()
local content = table.concat(Utils.get_buf_lines(0, -1, self.code.buf), "\n")
local response = self:get_content_between_separators()
local snippets = extract_code_snippets(response)
local conflict_content = get_conflict_content(content, snippets)
@ -692,7 +687,7 @@ function Sidebar:render()
self:update_content("", { focus = true, scroll = false })
self:update_content(content_prefix .. "🔄 **Generating response ...**\n")
local content = self:get_code_content()
local content = table.concat(Utils.get_buf_lines(0, -1, self.code.buf), "\n")
local content_with_line_numbers = prepend_line_number(content)
local selected_code_content_with_line_numbers = nil

View File

@ -19,6 +19,7 @@ local AvanteRenderer = require("nui-components.renderer")
---@class NuiComponent
---@field winid integer | nil
---@field bufnr integer | nil
local AvanteComponent = require("nui-components.component")
---@param opts table<string, any>
@ -28,6 +29,14 @@ function AvanteRenderer.create(opts) end
---@return NuiComponent[]
function AvanteRenderer:get_focusable_components() end
---@param mappings {mode: string[], key: string, handler: fun(): any}[]
---@return nil
function AvanteRenderer:add_mappings(mappings) end
---@param id string
---@return NuiComponent
function AvanteRenderer:get_component_by_id(id) end
---@param body fun():NuiComponent
function AvanteRenderer:render(body) end

View File

@ -83,36 +83,6 @@ function M.get_visual_selection_and_range()
return SelectionResult.new(content, range)
end
--- Start an async job
---@param cmd string
---@param callback fun(data: string[]): nil
function M.job(cmd, callback)
fn.jobstart(cmd, {
stdout_buffered = true,
on_stdout = function(_, data, _)
callback(data)
end,
})
end
---Only call the passed function once every timeout in ms
---@param timeout integer
---@param func function
---@return function
function M.throttle(timeout, func)
local timer = vim.loop.new_timer()
local running = false
return function(...)
if not running then
func(...)
running = true
timer:start(timeout, 0, function()
running = false
end)
end
end
end
---Wrapper around `api.nvim_buf_get_lines` which defaults to the current buffer
---@param start integer
---@param _end integer