From 9d0e1cd4af3f739bdad87f2a48bc9348bfe40ee7 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 23 Nov 2024 20:23:05 +0800 Subject: [PATCH] fix: align lua oop (#889) --- lua/avante/prompt_input.lua | 4 +++- lua/avante/range.lua | 10 +++++----- lua/avante/selection.lua | 5 +++-- lua/avante/selection_result.lua | 10 +++++----- lua/avante/sidebar.lua | 4 ++-- lua/avante/utils/init.lua | 4 ++-- lua/cmp_avante/commands.lua | 16 +++++++++------- lua/cmp_avante/mentions.lua | 16 +++++++++------- 8 files changed, 38 insertions(+), 31 deletions(-) diff --git a/lua/avante/prompt_input.lua b/lua/avante/prompt_input.lua index 7fe5ea484..06b2f64e8 100644 --- a/lua/avante/prompt_input.lua +++ b/lua/avante/prompt_input.lua @@ -18,6 +18,7 @@ local Utils = require("avante.utils") ---@field spinner_timer uv_timer_t | nil ---@field spinner_active boolean local PromptInput = {} +PromptInput.__index = PromptInput ---@class PromptInputOptions ---@field start_insert? boolean @@ -29,7 +30,7 @@ local PromptInput = {} ---@param opts? PromptInputOptions function PromptInput:new(opts) opts = opts or {} - local obj = setmetatable({}, { __index = self }) + local obj = setmetatable({}, PromptInput) obj.bufnr = nil obj.winid = nil obj.shortcuts_hints_winid = nil @@ -235,6 +236,7 @@ function PromptInput:setup_keymaps() local bufnr = self.bufnr 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) return lines[1] or "" end diff --git a/lua/avante/range.lua b/lua/avante/range.lua index 1edd6e3c8..9d8044427 100644 --- a/lua/avante/range.lua +++ b/lua/avante/range.lua @@ -11,11 +11,11 @@ Range.__index = Range ---Create a selection range ---@param start avante.RangeSelection Selection start point ---@param finish avante.RangeSelection Selection end point -function Range.new(start, finish) - local self = setmetatable({}, Range) - self.start = start - self.finish = finish - return self +function Range:new(start, finish) + local instance = setmetatable({}, Range) + instance.start = start + instance.finish = finish + return instance end return Range diff --git a/lua/avante/selection.lua b/lua/avante/selection.lua index 27307eb92..f1fde0311 100644 --- a/lua/avante/selection.lua +++ b/lua/avante/selection.lua @@ -21,6 +21,7 @@ local PRIORITY = vim.highlight.priorities.user ---@field code_winid integer | nil ---@field prompt_input PromptInput | nil local Selection = {} +Selection.__index = Selection Selection.did_setup = false @@ -34,7 +35,7 @@ function Selection:new(id) cursor_pos = nil, code_winid = nil, prompt_input = nil, - }, { __index = self }) + }, Selection) end function Selection:get_virt_text_line() @@ -238,7 +239,7 @@ function Selection:create_editing_input() if has_cmp then cmp.register_source( "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({ enabled = true, diff --git a/lua/avante/selection_result.lua b/lua/avante/selection_result.lua index ea891bf3d..452b4b85f 100644 --- a/lua/avante/selection_result.lua +++ b/lua/avante/selection_result.lua @@ -7,11 +7,11 @@ SelectionResult.__index = SelectionResult -- Create a selection content and range ---@param content string Selected content ---@param range avante.Range Selection range -function SelectionResult.new(content, range) - local self = setmetatable({}, SelectionResult) - self.content = content - self.range = range - return self +function SelectionResult:new(content, range) + local instance = setmetatable({}, SelectionResult) + instance.content = content + instance.range = range + return instance end return SelectionResult diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 5b7f7286a..22c4fc4cc 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -1653,11 +1653,11 @@ function Sidebar:create_input(opts) if has_cmp then cmp.register_source( "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( "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({ enabled = true, diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index f5d8e8920..ae9ce0a99 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -190,7 +190,7 @@ function M.get_visual_selection_and_range() start_col, end_col = end_col, start_col end 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 if start_line == end_line then -- Get partial content of a single line @@ -213,7 +213,7 @@ function M.get_visual_selection_and_range() end if not content then return nil end -- Return the selected content and range - return SelectionResult.new(content, range) + return SelectionResult:new(content, range) end ---Wrapper around `api.nvim_buf_get_lines` which defaults to the current buffer diff --git a/lua/cmp_avante/commands.lua b/lua/cmp_avante/commands.lua index 9d53015b5..2b87143fa 100644 --- a/lua/cmp_avante/commands.lua +++ b/lua/cmp_avante/commands.lua @@ -1,18 +1,20 @@ local api = vim.api ----@class commands_source +---@class commands_source : cmp.Source ---@field commands AvanteSlashCommand[] ---@field bufnr integer local commands_source = {} +commands_source.__index = commands_source ---@param commands AvanteSlashCommand[] ---@param bufnr integer -function commands_source.new(commands, bufnr) - ---@type cmp.Source - return setmetatable({ - commands = commands, - bufnr = bufnr, - }, { __index = commands_source }) +function commands_source:new(commands, bufnr) + local instance = setmetatable({}, commands_source) + + instance.commands = commands + instance.bufnr = bufnr + + return instance end function commands_source:is_available() return api.nvim_get_current_buf() == self.bufnr end diff --git a/lua/cmp_avante/mentions.lua b/lua/cmp_avante/mentions.lua index 558640edc..fe4d9d1fd 100644 --- a/lua/cmp_avante/mentions.lua +++ b/lua/cmp_avante/mentions.lua @@ -1,18 +1,20 @@ 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 bufnr integer local mentions_source = {} +mentions_source.__index = mentions_source ---@param mentions {description: string, command: AvanteMentions, details: string, shorthelp?: string, callback?: AvanteMentionCallback}[] ---@param bufnr integer -function mentions_source.new(mentions, bufnr) - ---@type cmp.Source - return setmetatable({ - mentions = mentions, - bufnr = bufnr, - }, { __index = mentions_source }) +function mentions_source:new(mentions, bufnr) + local instance = setmetatable({}, mentions_source) + + instance.mentions = mentions + instance.bufnr = bufnr + + return instance end function mentions_source:is_available() return api.nvim_get_current_buf() == self.bufnr end