Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autopairs integration #118

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lua/care/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ config.defaults = {
position = "overlay",
},
},
integration = { autopairs = false },
snippet_expansion = function(snippet_body)
vim.snippet.expand(snippet_body)
end,
Expand Down
3 changes: 3 additions & 0 deletions lua/care/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ care.api = {
is_reversed = function()
return care.core.menu.reversed
end,
set_event = function (event, callback)
require("care.utils.events"):on(event, callback)
end
}

---@param options? care.config
Expand Down
4 changes: 4 additions & 0 deletions lua/care/menu/confirm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local Log = require("care.utils.log")
---@param entry care.entry
return function(entry)
local config = require("care.config").options
local event = require("care.utils.events")
Log.log("Confirming Entry")
Log.log("Completion item", function()
local item = vim.deepcopy(entry.completion_item)
Expand Down Expand Up @@ -51,6 +52,9 @@ return function(entry)
local is_snippet = completion_item.insertTextFormat == 2
local snippet_text

event:emit("confirm_done")


if not completion_item.textEdit then
---@diagnostic disable-next-line: missing-fields
completion_item.textEdit = {
Expand Down
3 changes: 3 additions & 0 deletions lua/care/types/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@
--- Indicated whether the menu is reversed
--- Only relevant when using sorting direction "away-from-cursor"
---@field is_reversed fun(): boolean
--- Sets an event listener.
--- Registers an event with the specified callback function.
---@field set_event fun(event: string, callback: fun()): nil
5 changes: 5 additions & 0 deletions lua/care/types/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@
--- Auto will prefer right if there is enough space
---@field position? "auto"|"left"|"right"

--- Configuration of integrations with other plugin
---@class care.config.integration
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to myself: add to docgen so it gets link after merging

--- Enables the autopairs integration (default false)
---@field autopairs? boolean

--- Additional data passed to format function to allow more advanced formatting
---@class care.format_data
--- Index of the entry in the completion menu
Expand Down
38 changes: 38 additions & 0 deletions lua/care/utils/events.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local event = {}

event.new = function()
local self = setmetatable({}, { __index = event })
self.events = {}
return self
end

event.on = function(self, name, callback)
if not self.events[name] then
self.events[name] = {}
end
table.insert(self.events[name], callback)
return function()
self:off(name, callback)
end
end

event.off = function(self, name, callback)
for i, callback_ in ipairs(self.events[name] or {}) do
if callback_ == callback then
table.remove(self.events[name], i)
break
end
end
end

event.clear = function(self)
self.events = {}
end

event.emit = function(self, name, ...)
for _, callback in ipairs(self.events[name] or {}) do
callback(...)
end
end

return event.new()