chore: remove unused functions and simplify with utils (#103)
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
This commit is contained in:
parent
410824e357
commit
5f74c54e55
@ -253,7 +253,7 @@ function Sidebar:update_content(content, opts)
|
|||||||
local scroll_to_bottom = function()
|
local scroll_to_bottom = function()
|
||||||
local last_line = api.nvim_buf_line_count(self.view.buf)
|
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
|
if #current_lines > 0 then
|
||||||
local last_line_content = current_lines[1]
|
local last_line_content = current_lines[1]
|
||||||
@ -324,6 +324,7 @@ end
|
|||||||
---@field end_line integer
|
---@field end_line integer
|
||||||
---@field lang string
|
---@field lang string
|
||||||
|
|
||||||
|
---@param buf integer
|
||||||
---@return AvanteCodeblock[]
|
---@return AvanteCodeblock[]
|
||||||
local function parse_codeblocks(buf)
|
local function parse_codeblocks(buf)
|
||||||
local codeblocks = {}
|
local codeblocks = {}
|
||||||
@ -331,7 +332,7 @@ local function parse_codeblocks(buf)
|
|||||||
local start_line = nil
|
local start_line = nil
|
||||||
local lang = 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
|
for i, line in ipairs(lines) do
|
||||||
if line:match("^```") then
|
if line:match("^```") then
|
||||||
-- parse language
|
-- parse language
|
||||||
@ -352,8 +353,8 @@ end
|
|||||||
|
|
||||||
---@param codeblocks table<integer, any>
|
---@param codeblocks table<integer, any>
|
||||||
local function is_cursor_in_codeblock(codeblocks)
|
local function is_cursor_in_codeblock(codeblocks)
|
||||||
local cursor_pos = api.nvim_win_get_cursor(0)
|
local cursor_line, _ = Utils.get_cursor_pos()
|
||||||
local cursor_line = cursor_pos[1] - 1 -- 转换为 0-indexed 行号
|
cursor_line = cursor_line - 1 -- 转换为 0-indexed 行号
|
||||||
|
|
||||||
for _, block in ipairs(codeblocks) do
|
for _, block in ipairs(codeblocks) do
|
||||||
if cursor_line >= block.start_line and cursor_line <= block.end_line then
|
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
|
return result
|
||||||
end
|
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
|
---@return string
|
||||||
function Sidebar:get_content_between_separators()
|
function Sidebar:get_content_between_separators()
|
||||||
local separator = "---"
|
local separator = "---"
|
||||||
local cursor_line = api.nvim_win_get_cursor(0)[1]
|
local cursor_line, _ = Utils.get_cursor_pos()
|
||||||
local lines = api.nvim_buf_get_lines(self.view.buf, 0, -1, false)
|
local lines = Utils.get_buf_lines(0, -1, self.view.buf)
|
||||||
local start_line, end_line
|
local start_line, end_line
|
||||||
|
|
||||||
for i = cursor_line, 1, -1 do
|
for i = cursor_line, 1, -1 do
|
||||||
@ -601,7 +596,7 @@ function Sidebar:render()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function apply()
|
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 response = self:get_content_between_separators()
|
||||||
local snippets = extract_code_snippets(response)
|
local snippets = extract_code_snippets(response)
|
||||||
local conflict_content = get_conflict_content(content, snippets)
|
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("", { focus = true, scroll = false })
|
||||||
self:update_content(content_prefix .. "🔄 **Generating response ...**\n")
|
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 content_with_line_numbers = prepend_line_number(content)
|
||||||
|
|
||||||
local selected_code_content_with_line_numbers = nil
|
local selected_code_content_with_line_numbers = nil
|
||||||
|
@ -19,6 +19,7 @@ local AvanteRenderer = require("nui-components.renderer")
|
|||||||
|
|
||||||
---@class NuiComponent
|
---@class NuiComponent
|
||||||
---@field winid integer | nil
|
---@field winid integer | nil
|
||||||
|
---@field bufnr integer | nil
|
||||||
local AvanteComponent = require("nui-components.component")
|
local AvanteComponent = require("nui-components.component")
|
||||||
|
|
||||||
---@param opts table<string, any>
|
---@param opts table<string, any>
|
||||||
@ -28,6 +29,14 @@ function AvanteRenderer.create(opts) end
|
|||||||
---@return NuiComponent[]
|
---@return NuiComponent[]
|
||||||
function AvanteRenderer:get_focusable_components() end
|
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
|
---@param body fun():NuiComponent
|
||||||
function AvanteRenderer:render(body) end
|
function AvanteRenderer:render(body) end
|
||||||
|
|
||||||
|
@ -83,36 +83,6 @@ function M.get_visual_selection_and_range()
|
|||||||
return SelectionResult.new(content, range)
|
return SelectionResult.new(content, range)
|
||||||
end
|
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
|
---Wrapper around `api.nvim_buf_get_lines` which defaults to the current buffer
|
||||||
---@param start integer
|
---@param start integer
|
||||||
---@param _end integer
|
---@param _end integer
|
||||||
|
Loading…
x
Reference in New Issue
Block a user