-
-
Notifications
You must be signed in to change notification settings - Fork 633
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
Added code to export definitions for a lua-language-server #5475
base: master
Are you sure you want to change the base?
Conversation
I now see why everything in the APIDump plugin was in a single file ;) It's so the functions don't show up in the global environment. I'll move the functions over to the main file later. |
Seeing the APIDump code makes me want to rewrite it :) |
I had the same urge when I looked at some (or most) of the WorldEdit code yesterday ;) |
The 'not polluting _G' issue could also be fixed by immediately replacing local newEnv, oldEnv = {}, _G
local setmetatable = setmetatable
for k, v in pairs(_G) do
newEnv[k] = v;
oldEnv[k] = nil;
end
_G = setmetatable(oldEnv, {__index = newEnv}); Then instead of looping through |
So is it VSCode-only or generic lua-language-server? Just to get the naming right. |
I've only tested it on VSCode, but it should work on any lua-language-server. But you're right, I'll change the naming. |
@@ -136,7 +136,7 @@ local function CreateAPITables() | |||
return res; | |||
end | |||
|
|||
for i, v in pairs(_G) do | |||
for i, v in pairs(getmetatable(_G).__index) do | |||
if ( | |||
(v ~= _G) and -- don't want the global namespace |
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.
Will this comparison still work? I doubt that.
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.
It actually does still work.
The _preload
code doesn't replace the actual _G
table. It just takes its values and moves it to a new table. So once it gets to the _G
key it takes the reference to itself and moves it to the new table.
Put in another way: Usually _G
references to itself like this:
_G._G._G._G...
but with _preload
this changes to
_G.__index._G.__index...
▲
because of getmetatable() we start here which means its _G value
references back to the global _G environment
I still need to write an article to explain how to configure the language server so this PR is still a work in progress, but the definition generation seems to be working now.
The definitions should work with any IDE that can use lua-language-server. Here is an example with Visual Studio Code:
2023-03-12.14-40-15.mp4