Skip to content

Commit

Permalink
refactor: improve class extending system
Browse files Browse the repository at this point in the history
Add extendable constructor and move overlay create logic to :create

Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam committed Nov 21, 2024
1 parent eb22ba3 commit 9f3366f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 51 deletions.
52 changes: 23 additions & 29 deletions lua/CopilotChat/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ local function match_header(header)
end

local Chat = class(function(self, help, on_buf_create)
Overlay.init(self, 'copilot-chat', help, on_buf_create)
vim.treesitter.language.register('markdown', self.name)

self.header_ns = vim.api.nvim_create_namespace('copilot-chat-headers')
self.name = 'copilot-chat'
self.help = help
self.on_buf_create = on_buf_create
self.bufnr = nil
self.winnr = nil
self.spinner = nil
self.sections = {}
Expand All @@ -73,35 +72,30 @@ local Chat = class(function(self, help, on_buf_create)
self.question_header = nil
self.answer_header = nil
self.separator = nil
end, Overlay)

vim.treesitter.language.register('markdown', self.name)
function Chat:create()
local bufnr = Overlay.create(self)
vim.bo[bufnr].syntax = 'markdown'
vim.bo[bufnr].textwidth = 0

self.buf_create = function()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, self.name)
vim.bo[bufnr].filetype = self.name
vim.bo[bufnr].syntax = 'markdown'
vim.bo[bufnr].textwidth = 0
vim.bo[bufnr].modifiable = false

vim.api.nvim_create_autocmd({ 'TextChanged', 'InsertLeave' }, {
buffer = bufnr,
callback = function()
utils.debounce(self.name, function()
self:render()
end, 100)
end,
})

if not self.spinner then
self.spinner = Spinner(bufnr)
else
self.spinner.bufnr = bufnr
end
vim.api.nvim_create_autocmd({ 'TextChanged', 'InsertLeave' }, {
buffer = bufnr,
callback = function()
utils.debounce(self.name, function()
self:render()
end, 100)
end,
})

return bufnr
if not self.spinner then
self.spinner = Spinner(bufnr)
else
self.spinner.bufnr = bufnr
end
end, Overlay)

return bufnr
end

function Chat:visible()
return self.winnr
Expand Down
12 changes: 1 addition & 11 deletions lua/CopilotChat/diff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,13 @@ local utils = require('CopilotChat.utils')
local class = utils.class

local Diff = class(function(self, help, on_buf_create)
Overlay.init(self, 'copilot-diff', help, on_buf_create)
self.hl_ns = vim.api.nvim_create_namespace('copilot-chat-highlights')
vim.api.nvim_set_hl(self.hl_ns, '@diff.plus', { bg = utils.blend_color('DiffAdd', 20) })
vim.api.nvim_set_hl(self.hl_ns, '@diff.minus', { bg = utils.blend_color('DiffDelete', 20) })
vim.api.nvim_set_hl(self.hl_ns, '@diff.delta', { bg = utils.blend_color('DiffChange', 20) })

self.name = 'copilot-diff'
self.help = help
self.on_buf_create = on_buf_create
self.bufnr = nil
self.diff = nil

self.buf_create = function()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(bufnr, self.name)
vim.bo[bufnr].filetype = self.name
return bufnr
end
end, Overlay)

function Diff:show(diff, winnr)
Expand Down
25 changes: 14 additions & 11 deletions lua/CopilotChat/overlay.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---@class CopilotChat.Overlay
---@field name string
---@field bufnr number
---@field create fun(self: CopilotChat.Overlay)
---@field valid fun(self: CopilotChat.Overlay)
---@field validate fun(self: CopilotChat.Overlay)
---@field show fun(self: CopilotChat.Overlay, text: string, filetype: string?, winnr: number)
Expand All @@ -11,19 +13,21 @@ local utils = require('CopilotChat.utils')
local class = utils.class

local Overlay = class(function(self, name, help, on_buf_create)
self.name = name
self.help = help
self.help_ns = vim.api.nvim_create_namespace('copilot-chat-help')
self.on_buf_create = on_buf_create
self.bufnr = nil

self.buf_create = function()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[bufnr].filetype = name
vim.bo[bufnr].modifiable = false
vim.api.nvim_buf_set_name(bufnr, name)
return bufnr
end
end)

function Overlay:create()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[bufnr].filetype = self.name
vim.bo[bufnr].modifiable = false
vim.api.nvim_buf_set_name(bufnr, self.name)
return bufnr
end

function Overlay:valid()
return utils.buf_valid(self.bufnr)
end
Expand All @@ -33,7 +37,7 @@ function Overlay:validate()
return
end

self.bufnr = self.buf_create(self)
self.bufnr = self:create()
self.on_buf_create(self.bufnr)
end

Expand Down Expand Up @@ -82,9 +86,8 @@ function Overlay:show_help(msg, offset)
end

self:validate()
local help_ns = vim.api.nvim_create_namespace('copilot-chat-help')
local line = vim.api.nvim_buf_line_count(self.bufnr) + offset
vim.api.nvim_buf_set_extmark(self.bufnr, help_ns, math.max(0, line - 1), 0, {
vim.api.nvim_buf_set_extmark(self.bufnr, self.help_ns, math.max(0, line - 1), 0, {
id = 1,
hl_mode = 'combine',
priority = 100,
Expand Down
4 changes: 4 additions & 0 deletions lua/CopilotChat/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function M.class(fn, parent)
return self
end

function out.init(self, ...)
fn(self, ...)
end

return out
end

Expand Down

0 comments on commit 9f3366f

Please sign in to comment.