Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Is it possible for the language server to deduce the members of a metatable?#3270

drcxd started this conversation inGeneral
Discussion options

Assume I am trying to use OOP in Lua in the following manner:

localClass= {}Class.__index=ClassClass.myMethod=function ()print("hellow world")endClass.new=function ()localo= {}setmetatable(o,Class)returnoendlocalinst=Class.new()inst:myMethod()^

It seems that when I input till the colon, the language server can not figure out that inst has a metatable which__index is set toClass, and thus can not prompt completion formyMythod. Is this impossible to figure out since it is a runtime thing? Do you have any recommended pattern to implement the similar effect, but make the language server know the existence of the methods or member variables?

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

LuaLS requires annotations to perform better type infer.
I think you will need some@class annotation in your provided snippet
https://luals.github.io/wiki/annotations/#class

---@classClasslocalClass= {}Class.__index=ClassClass.myMethod=function ()print("hellow world")endClass.new=function ()---@classClasslocalo= {}-- you can define other fields for `o` here-- which will be auto "injected" into the class `Class` as member fieldso.x=1o.y=2setmetatable(o,Class)returnoendlocalinst=Class.new()-- server will infer that `Class.new -> Class`inst:my--> now this will have completion for `myMethod`
You must be logged in to vote
1 reply
@drcxd
Comment options

Thanks a lot. This perfectly solves my problem. I did not know that annotation is necessary, should have read the documentation more carefully.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
2 participants
@drcxd@tomlau10

[8]ページ先頭

©2009-2025 Movatter.jp