Lua 尝试索引 nil 值与代码

问题描述 投票:0回答:1

我正在尝试运行此代码,但 lua 一直给我错误。该包存在于指定位置。这是代码 - 谢谢!:

package.path = package.path .. ';C:/Users/xyz/AppData/Roaming/luarocks/share/lua/5.4/?.lua;C:/Users/xyz/AppData/Roaming/luarocks/share/lua/5.4/?/init.lua'
package.cpath = package.cpath .. ';C:/Users/xyz/AppData/Roaming/luarocks/lib/lua/5.4/?.so'

-- Require the modules using their module names
local npairs = require('nvim-autopairs')
local Rule = require('nvim-autopairs.rule')
local cond = require('nvim-autopairs.conds')

-- Setup nvim-autopairs
npairs.setup({})

-- Add custom rules
npairs.add_rules({
  Rule("$", "$", "tex")
    :with_move(cond.after_text("$"))
    :with_move(cond.before_text("$")),
  Rule("$$", "$$", "tex")
    :with_move(cond.after_text("$$"))
    :with_move(cond.before_text("$$"))
})
lua
1个回答
0
投票

当它说“索引一个零值”时,它的意思是你试图从无(nil)中得到一些东西(索引)。 Lua 有两种主要的索引方式,

thing["index"]
thing.index

我高度怀疑您在某处遇到了未使用的全局变量,默认为

nil
。它可能不在您发布的代码中,我无法直接找到全局变量的任何问题。

您在这里使用的唯一全局变量是

package
require
。也许你的 Lua 运行时缺少其中之一。尝试
print(package, require)
看看是否在任何地方看到 nil 这个词,那么你就错过了其中一个。

另一个选项是

npairs
cond
为零。这将是
nvim-autopairs
库的问题。

无论哪种方式,每当出现此错误时,都应该检查是否为零。

© www.soinside.com 2019 - 2024. All rights reserved.