with one click
lua-class
// Use when creating new lua classes or adding fields or methods to existing lua classes. It gives guide lines on how to work with Lua classes and style guides that must be followed in this project
// Use when creating new lua classes or adding fields or methods to existing lua classes. It gives guide lines on how to work with Lua classes and style guides that must be followed in this project
Use when ACP schema is necessary to clarify the work or when the user asks for ACP data, rules, events, or documentation
Use when neovim documentation is needed for the current task or the user asks for specific neovim features or neovim lua apis. It gives instructions on how to read current Neovim's documentation locally and offline, and also how to read online if it fails
Use when creating, listing, or managing GitHub discussions in carlos-algms/agentic.nvim repository
| name | lua-class |
| description | Use when creating new lua classes or adding fields or methods to existing lua classes. It gives guide lines on how to work with Lua classes and style guides that must be followed in this project |
Basic class structure:
--- @class Animal
local Animal = {}
Animal.__index = Animal
function Animal:new()
self = setmetatable({}, self)
return self
end
function Animal:move()
print("Animal moves")
end
Key points:
__index to self for inheritancesetmetatable to create instancesMethod definition syntax:
function Class:method() - Instance method, receives self implicitly
instance:method() or instance.method(instance)function Class.method() - Module function, static, does NOT receive self
Class.method() or instance.method() (both work, but no
self)Class setup (module-level):
local Parent = {}
Parent.__index = Parent
--- @class Child : Parent
local Child = setmetatable({}, { __index = Parent })
Child.__index = Child
Constructor with parent initialization:
function Parent:new(name)
local instance = {
name = name,
parent_state = {}
}
return setmetatable(instance, self)
end
function Child:new(name, extra)
-- Call parent constructor with Parent class
local instance = Parent.new(Parent, name)
-- Add child-specific state
instance.child_state = extra
-- Re-metatable to child class for proper inheritance chain
return setmetatable(instance, Child)
end
Critical rules:
Parent.new(Parent, ...) not
Parent.new(self, ...)instance → Child → ParentCalling parent methods:
function Child:move()
Parent.move(self) -- Explicit parent method call
print("Child-specific movement")
end
Minimize class properties - Only include properties that:
Use visibility prefixes for encapsulation - Control what external code can access:
Visibility levels (configured in .luarc.json):
_*: Private - Hidden from external consumers (applies to class
methods/fields ONLY)__*: Protected - Visible to subclassesIMPORTANT: Module-level local functions and variables do NOT need _
prefix:
local function helper() - correct (already private by local scope)local function _helper() - incorrect (redundant _ prefix)local config = {} - correctlocal _config = {} - incorrect (redundant _ prefix)function MyClass:_private_method() - correct (class method needs _)@field _private_field - correct (class field needs _)-- ❌ Bad: Unnecessary public exposure of `counter` property, not used externally
--- @class MyClass
--- @field counter number
local MyClass = {}
MyClass.__index = MyClass
function MyClass:new()
return setmetatable({ counter = 0 }, self)
end
-- ✅ Good: Proper visibility control
--- @class MyClass
local MyClass = {}
MyClass.__index = MyClass
function MyClass:new()
return setmetatable({
-- Counter is internal state, not exposed publicly
_counter = 0
}, self)
end
--- @protected
function MyClass:__protected_method()
self._counter = self._counter + 1
end
--- Module-level helper functions (no underscore prefix needed)
local function format_value(val)
return tostring(val)
end
--- @class Child : MyClass
function Child:use_parent_state()
self:__protected_method()
end
Note: The @private annotation is NOT necessary for private class methods
_ prefix automatically@protected for protected methods (__*, luals limitation)Document intent with LuaCATS - Use visibility annotations:
--- @class MyClass
--- @field public_field string Public API
--- @field __protected_field table For subclasses
--- @field _private_field number Internal only
Regular cleanup - When adding new code, review class definitions and remove: