-
Notifications
You must be signed in to change notification settings - Fork 463
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
Add typing to widgets and gui #4577
Conversation
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
@myk002 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like a great start! class names look good to me
library/lua/class.lua
Outdated
---@class dfhack.class: class.common_methods, class.class_obj | ||
---@field super any | ||
---@field ATTRS fun(attributes: table) | ||
---@overload fun(attributes: table): self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this attached to anything? or does it get combined into defclass
below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This overload is attached to the class itself, it's mostly boilerplate.
library/lua/gui.lua
Outdated
@@ -8,6 +8,20 @@ local utils = require('utils') | |||
local dscreen = dfhack.screen | |||
local getval = utils.getval | |||
|
|||
---@class dfhack.pen | |||
---@field ch? string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is more commonly an integer, though a string character also works (iirc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is probably a candidate for an alias/class like dfhack.pen
or dfhack.truthy
.
library/lua/gui/widgets.lua
Outdated
---@field pen? pen | ||
---@field dpen? pen | ||
---@field hpen? pen | ||
---@field on_activiate? function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
specifically, a function that takes no params and returns no value
library/lua/gui/widgets.lua
Outdated
---@field line? any | ||
---@field x1? any | ||
---@field x2? any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these three are internal; do they need to be declared here?
library/lua/gui/widgets.lua
Outdated
---@field text_pen? any | ||
---@field text_dpen? any | ||
---@field text_hpen? any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
colors or Pens
library/lua/gui/widgets.lua
Outdated
---@field auto_width? boolean | ||
---@field on_click? function | ||
---@field on_rclick? function | ||
---@field scroll_keys? widgets.Keys |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scroll_keys is a map from keybinding strings to either an integer or a string
This was causing issues in parsing and I didn't want to change the parser in the definitions repository for this single instance.
@myk002 getting closer to completion, feel free to ignore widgets.lua below |
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
scripts: master
library/lua/dfhack.lua
Outdated
---@alias dfhack.truthy | ||
---| true | ||
---| integer | ||
---| string Not an empty string "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
empty strings are truthy. only nil
and false
is falsey
if you really want to be complete, you can add userdata
to this list
library/lua/gui/widgets.lua
Outdated
local function show_view(view,vis) | ||
if view then | ||
view.visible = vis | ||
end | ||
end | ||
|
||
---@param tab table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tab? table<T>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned on Discord, I think this doesn't work. Full example would be something like:
---@generic T
---@param tab? table<T>
---@param idx integer
---@return T
local function map_opttab(tab,idx)
if tab then
return tab[idx]
else
return idx
end
end
---@type string[]
local foo
local bar = map_opttab(foo, 0)
Where bar
remains unknown
library/lua/gui/widgets.lua
Outdated
local function show_view(view,vis) | ||
if view then | ||
view.visible = vis | ||
end | ||
end | ||
|
||
---@param tab table | ||
---@param idx integer | ||
---@return table|integer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
T|integer
library/lua/gui/widgets.lua
Outdated
@@ -1814,13 +2110,48 @@ ToggleHotkeyLabel.ATTRS{ | |||
-- List -- | |||
---------- | |||
|
|||
---@class widgets.ListChoice | |||
---@field text string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can also be a list of text tokens, like Label
This PR aims to add type support to
widgets.lua
andgui.lua
and as a consequence extend the typing of a few other files as well (namely some globals andclass.lua
).There are some conventions being established here, feel free to critique anything and everything for the sake of establishing a good foundation.
When defining new classes we should follow this rough pattern for a good experience:
There are a few caveats:
defclass(ClassName)
plays weird with LuaLS andself
parameters in the module file will have to be manually cast as the class, i.e: