fix: align lua oop (#889)

This commit is contained in:
yetone 2024-11-23 20:23:05 +08:00 committed by GitHub
parent da41105fc8
commit 9d0e1cd4af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 38 additions and 31 deletions

View File

@ -18,6 +18,7 @@ local Utils = require("avante.utils")
---@field spinner_timer uv_timer_t | nil ---@field spinner_timer uv_timer_t | nil
---@field spinner_active boolean ---@field spinner_active boolean
local PromptInput = {} local PromptInput = {}
PromptInput.__index = PromptInput
---@class PromptInputOptions ---@class PromptInputOptions
---@field start_insert? boolean ---@field start_insert? boolean
@ -29,7 +30,7 @@ local PromptInput = {}
---@param opts? PromptInputOptions ---@param opts? PromptInputOptions
function PromptInput:new(opts) function PromptInput:new(opts)
opts = opts or {} opts = opts or {}
local obj = setmetatable({}, { __index = self }) local obj = setmetatable({}, PromptInput)
obj.bufnr = nil obj.bufnr = nil
obj.winid = nil obj.winid = nil
obj.shortcuts_hints_winid = nil obj.shortcuts_hints_winid = nil
@ -235,6 +236,7 @@ function PromptInput:setup_keymaps()
local bufnr = self.bufnr local bufnr = self.bufnr
local function get_input() local function get_input()
if not bufnr or not api.nvim_buf_is_valid(bufnr) then return "" end
local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false) local lines = api.nvim_buf_get_lines(bufnr, 0, -1, false)
return lines[1] or "" return lines[1] or ""
end end

View File

@ -11,11 +11,11 @@ Range.__index = Range
---Create a selection range ---Create a selection range
---@param start avante.RangeSelection Selection start point ---@param start avante.RangeSelection Selection start point
---@param finish avante.RangeSelection Selection end point ---@param finish avante.RangeSelection Selection end point
function Range.new(start, finish) function Range:new(start, finish)
local self = setmetatable({}, Range) local instance = setmetatable({}, Range)
self.start = start instance.start = start
self.finish = finish instance.finish = finish
return self return instance
end end
return Range return Range

View File

@ -21,6 +21,7 @@ local PRIORITY = vim.highlight.priorities.user
---@field code_winid integer | nil ---@field code_winid integer | nil
---@field prompt_input PromptInput | nil ---@field prompt_input PromptInput | nil
local Selection = {} local Selection = {}
Selection.__index = Selection
Selection.did_setup = false Selection.did_setup = false
@ -34,7 +35,7 @@ function Selection:new(id)
cursor_pos = nil, cursor_pos = nil,
code_winid = nil, code_winid = nil,
prompt_input = nil, prompt_input = nil,
}, { __index = self }) }, Selection)
end end
function Selection:get_virt_text_line() function Selection:get_virt_text_line()
@ -238,7 +239,7 @@ function Selection:create_editing_input()
if has_cmp then if has_cmp then
cmp.register_source( cmp.register_source(
"avante_mentions", "avante_mentions",
require("cmp_avante.mentions").new(Utils.get_mentions(), prompt_input.bufnr) require("cmp_avante.mentions"):new(Utils.get_mentions(), prompt_input.bufnr)
) )
cmp.setup.buffer({ cmp.setup.buffer({
enabled = true, enabled = true,

View File

@ -7,11 +7,11 @@ SelectionResult.__index = SelectionResult
-- Create a selection content and range -- Create a selection content and range
---@param content string Selected content ---@param content string Selected content
---@param range avante.Range Selection range ---@param range avante.Range Selection range
function SelectionResult.new(content, range) function SelectionResult:new(content, range)
local self = setmetatable({}, SelectionResult) local instance = setmetatable({}, SelectionResult)
self.content = content instance.content = content
self.range = range instance.range = range
return self return instance
end end
return SelectionResult return SelectionResult

View File

@ -1653,11 +1653,11 @@ function Sidebar:create_input(opts)
if has_cmp then if has_cmp then
cmp.register_source( cmp.register_source(
"avante_commands", "avante_commands",
require("cmp_avante.commands").new(self:get_commands(), self.input.bufnr) require("cmp_avante.commands"):new(self:get_commands(), self.input.bufnr)
) )
cmp.register_source( cmp.register_source(
"avante_mentions", "avante_mentions",
require("cmp_avante.mentions").new(Utils.get_mentions(), self.input.bufnr) require("cmp_avante.mentions"):new(Utils.get_mentions(), self.input.bufnr)
) )
cmp.setup.buffer({ cmp.setup.buffer({
enabled = true, enabled = true,

View File

@ -190,7 +190,7 @@ function M.get_visual_selection_and_range()
start_col, end_col = end_col, start_col start_col, end_col = end_col, start_col
end end
local content = "" -- luacheck: ignore local content = "" -- luacheck: ignore
local range = Range.new({ line = start_line, col = start_col }, { line = end_line, col = end_col }) local range = Range:new({ line = start_line, col = start_col }, { line = end_line, col = end_col })
-- Check if it's a single-line selection -- Check if it's a single-line selection
if start_line == end_line then if start_line == end_line then
-- Get partial content of a single line -- Get partial content of a single line
@ -213,7 +213,7 @@ function M.get_visual_selection_and_range()
end end
if not content then return nil end if not content then return nil end
-- Return the selected content and range -- Return the selected content and range
return SelectionResult.new(content, range) return SelectionResult:new(content, range)
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

View File

@ -1,18 +1,20 @@
local api = vim.api local api = vim.api
---@class commands_source ---@class commands_source : cmp.Source
---@field commands AvanteSlashCommand[] ---@field commands AvanteSlashCommand[]
---@field bufnr integer ---@field bufnr integer
local commands_source = {} local commands_source = {}
commands_source.__index = commands_source
---@param commands AvanteSlashCommand[] ---@param commands AvanteSlashCommand[]
---@param bufnr integer ---@param bufnr integer
function commands_source.new(commands, bufnr) function commands_source:new(commands, bufnr)
---@type cmp.Source local instance = setmetatable({}, commands_source)
return setmetatable({
commands = commands, instance.commands = commands
bufnr = bufnr, instance.bufnr = bufnr
}, { __index = commands_source })
return instance
end end
function commands_source:is_available() return api.nvim_get_current_buf() == self.bufnr end function commands_source:is_available() return api.nvim_get_current_buf() == self.bufnr end

View File

@ -1,18 +1,20 @@
local api = vim.api local api = vim.api
---@class mentions_source ---@class mentions_source : cmp.Source
---@field mentions {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[] ---@field mentions {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[]
---@field bufnr integer ---@field bufnr integer
local mentions_source = {} local mentions_source = {}
mentions_source.__index = mentions_source
---@param mentions {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[] ---@param mentions {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[]
---@param bufnr integer ---@param bufnr integer
function mentions_source.new(mentions, bufnr) function mentions_source:new(mentions, bufnr)
---@type cmp.Source local instance = setmetatable({}, mentions_source)
return setmetatable({
mentions = mentions, instance.mentions = mentions
bufnr = bufnr, instance.bufnr = bufnr
}, { __index = mentions_source })
return instance
end end
function mentions_source:is_available() return api.nvim_get_current_buf() == self.bufnr end function mentions_source:is_available() return api.nvim_get_current_buf() == self.bufnr end