Server.is_installed() -> attempt to index local 'self' a nil value #535
-
Hey, I've been trying to create a simple "ensure those servers are installed everytime nvim is launched" function and I came up with this: local nvim_lsp_installer = require('nvim-lsp-installer')
local lspinstaller_ensure_installed = function()
local servers = {'jsonls', 'sumneko_lua', 'eslint', 'pyright', 'omnisharp'}
for _, name in pairs(servers) do
local ok, server = nvim_lsp_installer.get_server(name)
print(server.name, ok)
print(server.is_installed)
print(server.is_installed()) -- fails here
if ok and not server.is_installed() then
server.install()
end
end
end
lspinstaller_ensure_installed() But on the print(server.is_installed()) -- fails here line I get the following output+error:
I suspect this is some stupid mistake on my part that I can't see, but I haven't been able to resolve it. Anybody know what's up? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello! The On phone atm so I can't verify whether I'm 100% correct now, but the following should explain it in code: -- You need to provide the server instance as an argument to the metatable (class) functions!
server.is_installed(server)
server2.is_installed(server2)
-- The above looks really awkward though, so there's some syntactic
-- sugar which does this for you (this is the conventional way of doing it)
server:is_installed() -- use a : instead of .
server2:is_installed()
-- Other examples
local my_str = "hello"
string.find(my_str, "something")
-- or
my_str:find("something") Makes sense? |
Beta Was this translation helpful? Give feedback.
Hello! The
server
variable you're interacting with is an instance of a metatable (this is conceptually very similar to normal classes found in other languages). This metatable have different methods such asis_installed()
, which all operate on an individual instance. Without going into too much detail - the problem is that you're not providing which instance the method should target.On phone atm so I can't verify whether I'm 100% correct now, but the following should explain it in code: