Skip to content

Commit

Permalink
making changes to work with edgy
Browse files Browse the repository at this point in the history
  • Loading branch information
huynle committed Feb 3, 2024
1 parent fa6fa42 commit 8062ea4
Show file tree
Hide file tree
Showing 29 changed files with 335 additions and 1,080 deletions.
2 changes: 1 addition & 1 deletion lua/ogpt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function M.select_action(opts)
height = 0.5,
},
results_title = "Select Ollama action",
prompt_prefix = Config.options.popup_input.prompt,
prompt_prefix = Config.options.input_window.prompt,
selection_caret = Config.options.chat.answer_sign .. " ",
prompt_title = "actions",
finder = finder(action_definitions),
Expand Down
4 changes: 2 additions & 2 deletions lua/ogpt/api.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local job = require("plenary.job")
local Config = require("ogpt.config")
local logger = require("ogpt.common.logger")
local classes = require("ogpt.common.classes")
local Object = require("ogpt.common.object")
local utils = require("ogpt.utils")

local Api = classes.class()
local Api = Object("Api")

function Api:init(provider, action, opts)
self.opts = opts
Expand Down
95 changes: 0 additions & 95 deletions lua/ogpt/common/classes.lua

This file was deleted.

2 changes: 1 addition & 1 deletion lua/ogpt/common/input_widget.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ return function(name, on_submit)
winhighlight = "Normal:Normal,FloatBorder:FloatBorder",
},
}, {
prompt = Config.options.popup_input.prompt,
prompt = Config.options.input_window.prompt,
on_submit = on_submit,
})

Expand Down
170 changes: 170 additions & 0 deletions lua/ogpt/common/object.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
-- source: https://github.com/kikito/middleclass

local idx = {
subclasses = { "<nui.utils.object:subclasses>" },
}

local function __tostring(self)
return "class " .. self.name
end

local function __call(self, ...)
return self:new(...)
end

local function create_index_wrapper(class, index)
if type(index) == "table" then
return function(self, key)
local value = self.class.__meta[key]
if value == nil then
return index[key]
end
return value
end
elseif type(index) == "function" then
return function(self, key)
local value = self.class.__meta[key]
if value == nil then
return index(self, key)
end
return value
end
else
return class.__meta
end
end

local function propagate_instance_property(class, key, value)
value = key == "__index" and create_index_wrapper(class, value) or value

class.__meta[key] = value

for subclass in pairs(class[idx.subclasses]) do
if subclass.__properties[key] == nil then
propagate_instance_property(subclass, key, value)
end
end
end

local function declare_instance_property(class, key, value)
class.__properties[key] = value

if value == nil and class.super then
value = class.super.__meta[key]
end

propagate_instance_property(class, key, value)
end

local function is_subclass(subclass, class)
if not subclass.super then
return false
end
if subclass.super == class then
return true
end
return is_subclass(subclass.super, class)
end

local function is_instance(instance, class)
if instance.class == class then
return true
end
return is_subclass(instance.class, class)
end

local function create_class(name, super)
assert(name, "missing name")

local meta = {
is_instance_of = is_instance,
}
meta.__index = meta

local class = {
super = super,
name = name,
static = {
is_subclass_of = is_subclass,
},

[idx.subclasses] = setmetatable({}, { __mode = "k" }),

__meta = meta,
__properties = {},
}

setmetatable(class.static, {
__index = function(_, key)
local value = rawget(class.__meta, key)
if value == nil and super then
return super.static[key]
end
return value
end,
})

setmetatable(class, {
__call = __call,
__index = class.static,
__name = class.name,
__newindex = declare_instance_property,
__tostring = __tostring,
})

return class
end

---@param name string
local function create_object(_, name)
local Class = create_class(name)

---@return string
function Class:__tostring()
return "instance of " .. tostring(self.class)
end

---@return nil
function Class:init() end -- luacheck: no unused args

function Class.static:new(...)
local instance = setmetatable({ class = self }, self.__meta)
instance:init(...)
return instance
end

---@param name string
function Class.static:extend(name) -- luacheck: no redefined
local subclass = create_class(name, self)

for key, value in pairs(self.__meta) do
if not (key == "__index" and type(value) == "table") then
propagate_instance_property(subclass, key, value)
end
end

function subclass.init(instance, ...)
self.init(instance, ...)
end

self[idx.subclasses][subclass] = true

return subclass
end

return Class
end

--luacheck: push no max line length

---@type (fun(name: string): table)|{ is_subclass: (fun(subclass: table, class: table): boolean), is_instance: (fun(instance: table, class: table): boolean) }
local Object = setmetatable({
is_subclass = is_subclass,
is_instance = is_instance,
}, {
__call = create_object,
})

--luacheck: pop

return Object
15 changes: 15 additions & 0 deletions lua/ogpt/common/popup.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local NuiPopup = require("nui.popup")

Popup = NuiPopup:extend("OgptPopup")

function Popup:init(options, edgy)
options = options or {}
self.edgy = false
if options.edgy and options.border or edgy then
self.edgy = true
options.border = nil
end
Popup.super.init(self, options)
end

return Popup
Loading

0 comments on commit 8062ea4

Please sign in to comment.