pkulchenko / ZeroBraneStudio

Lightweight Lua-based IDE for Lua with code completion, syntax highlighting, live coding, remote debugger, and code analyzer; supports Lua 5.1, 5.2, 5.3, 5.4, LuaJIT and other Lua interpreters on Windows, macOS, and Linux
http://studio.zerobrane.com/
Other
2.6k stars 519 forks source link

Provide way to store userdata in the editor object #1126

Closed moteus closed 2 years ago

moteus commented 2 years ago

Currently for plugins that requires several actions from user (such like code snippets or easy motion) plugin code has to store its state like data[editor] = state and tracking closing editor to clear one. I know it is possible to just set editor.state = ... but its looks like not very safe solution. I think more about editor:get_userdata(key) / editor:set_userdata(key, value). So each plugin can have its own namespace

pkulchenko commented 2 years ago

@moteus, good point, but I'd deem editor.state to be "safe" enough as long as the plugins can use specific prefixes or sub-tables, so editor.plugin.state or editor.plugin_state. editor:get_userdata(key) would still need to have some information about the caller/plugin. Where would this information come from?

Your original option should work as well, if you make data a weak table on keys. This should take care of data deletion.

pkulchenko commented 2 years ago

Actually, you already used something like this (local SET = setmetatable({}, {__mode = 'k'})) in #1124, so this should work ;). Or is this not working for you for some reason?