Skip to content

Commit

Permalink
fix: align lua oop (yetone#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored Nov 23, 2024
1 parent da41105 commit 9d0e1cd
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 31 deletions.
4 changes: 3 additions & 1 deletion lua/avante/prompt_input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lua/avante/range.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions lua/avante/selection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions lua/avante/selection_result.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 9 additions & 7 deletions lua/cmp_avante/commands.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 9 additions & 7 deletions lua/cmp_avante/mentions.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 9d0e1cd

Please sign in to comment.