vallode / dfhack-lua-definitions

LuaLS lua language server definitions for DFHack's library
5 stars 0 forks source link

Enum attrs bi-directional mapping #6

Open vallode opened 6 months ago

vallode commented 6 months ago

Enum attributes can be access either via the key or the value of the corresponding enum, right now we are only using the value. This results in some annoying behaviour:

---@type tiletype
local ttype = dfhack.maps.getTileType(engraving.pos)
local tileattrs = df.tiletype.attrs[ttype] -- Cast as `unknown` even though this is a valid index.

The biggest issue is going to be figuring out how to do this without doubling the size of enums (again):

---@field FrozenRampTrackNEW { caption: "ice ramp track NEW", shape: "RAMP", material: "FROZEN_LIQUID", variant: "NONE", special: "TRACK", direction: "NEW" }```

Would change into:

---@field FrozenRampTrackNEW { caption: "ice ramp track NEW", shape: "RAMP", material: "FROZEN_LIQUID", variant: "NONE", special: "TRACK", direction: "NEW" }
---@field [677] { caption: "ice ramp track NEW", shape: "RAMP", material: "FROZEN_LIQUID", variant: "NONE", special: "TRACK", direction: "NEW" }

For enums with hundreds of attributes, it is less than ideal.

vallode commented 6 months ago

Even more annoyingly, it looks like accessing known class fields by a known integer variable is unsupported in LuaLS:

---@class Foo
---@field [0] "I am a string"
local foo

local bar = 0

local test = foo[bar] -- This _should_ result in test being known, but it is marked as "unknown" instead.