Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

NiLSPACE
Copy link
Member

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

@NiLSPACE NiLSPACE requested a review from madmaxoft March 12, 2023 15:07
@NiLSPACE
Copy link
Member Author

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.

@madmaxoft
Copy link
Member

Seeing the APIDump code makes me want to rewrite it :)

@NiLSPACE
Copy link
Member Author

I had the same urge when I looked at some (or most) of the WorldEdit code yesterday ;)

@NiLSPACE
Copy link
Member Author

NiLSPACE commented Mar 12, 2023

The 'not polluting _G' issue could also be fixed by immediately replacing _G with an empty table with __index pointing to the old values.

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 _G you'd loop through getmetatable(_G).__index

@madmaxoft
Copy link
Member

So is it VSCode-only or generic lua-language-server? Just to get the naming right.

@NiLSPACE
Copy link
Member Author

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.

@NiLSPACE NiLSPACE marked this pull request as ready for review March 12, 2023 22:44
@@ -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
Copy link
Member

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.

Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants