简体   繁体   English

Neovim lua 无法从不同文件加载配置

[英]Neovim lua unable to load config from different file

I am unable to load config when using require.使用 require 时我无法加载配置。 Form below you can see that I added 2 files under the lua directory下面的表格可以看到我在lua目录下添加了2个文件

  1. lua/bufferline.lua lua/bufferline.lua
  2. lua/plugins/bufferline.lua lua/插件/bufferline.lua

I am tried loading both via我尝试通过加载

-- Bufferline
  use {
    "akinsho/bufferline.nvim",
    tag = "v3.*",
    requires = 'nvim-tree/nvim-web-devicons',
    config = [[require("bufferline")]]
    -- config = [[require("plugins/bufferline")]]
  } -- buffer list with icons

And both failed to load the config changes.并且都未能加载config更改。 But adding the config inline in the plugins.lua the config shows up when restring.但是在plugins.lua中添加内联配置,配置在重新串接时显示。

Please check below for my files请在下面查看我的文件

lua directory structure lua目录结构

> tree
├── bufferline.lua
├── colorscheme.lua
├── keymaps.lua
├── options.lua
├── plugins
│   └── bufferline.lua
├── plugins.lua

init.lua初始化.lua

require "options"
require "keymaps"
require "plugins"
require "colorscheme"

plugins.lua插件.lua

--
-- Automatically install packer
local install_path = fn.stdpath "data" .. "/site/pack/packer/start/packer.nvim"
if fn.empty(fn.glob(install_path)) > 0 then
  PACKER_BOOTSTRAP = fn.system {
    "git",
    "clone",
    "--depth",
    "1",
    "https://github.com/wbthomason/packer.nvim",
    install_path,
  }
  print "Installing packer close and reopen Neovim..."
  vim.cmd [[packadd packer.nvim]]
end

-- Autocommand that reloads neovim whenever you save the plugins.lua file
vim.cmd [[
  augroup packer_user_config
    autocmd!
    autocmd BufWritePost plugins.lua source <afile> | PackerSync
  augroup end
]]

-- Use a protected call so we don't error out on first use
local status_ok, packer = pcall(require, "packer")
if not status_ok then
  return
end

-- Have packer use a popup window
packer.init {
  display = {
    open_fn = function()
      return require("packer.util").float { border = "rounded" }
    end,
  },
}


-- Install your plugins here
return packer.startup(function(use)
  use "wbthomason/packer.nvim" -- Have packer manage itself
  use "nvim-lua/popup.nvim" -- An implementation of the Popup API from vim in Neovim
  use "nvim-lua/plenary.nvim" -- Useful lua functions used ny lots of plugins

  -- Bufferline
  use {
    "akinsho/bufferline.nvim",
    tag = "v3.*",
    requires = 'nvim-tree/nvim-web-devicons',
    config = [[require("bufferline")]]
    -- config = [[require("plugins/bufferline")]]
  } -- buffer list with icons

  -- require("bufferline").setup{
  --   options = {
  --     numbers = "buffer_id",
  --     always_show_bufferline = true,
  --     separator_style = "thin"
  --   }
  -- }

  use "christoomey/vim-tmux-navigator" -- navigate between tmux and nvim

  --
  -- Automatically set up your configuration after cloning packer.nvim
  -- Put this at the end after all plugins
  if PACKER_BOOTSTRAP then
    require("packer").sync()
  end

end)

I think the general problem here is, that you're using the double brackets for the config = option:我认为这里的一般问题是,您在config =选项中使用了双括号:

    config = [[require("bufferline")]]

As far as I know (my lua skills aren't that great) those double brackets ( [[ ) are used to create a raw string in lua, so you're basically giving config a string which, I assume, vim tries to execute it as vimscript-code.据我所知(我的 lua 技能不是那么好)那些双括号( [[ )用于在 lua 中创建一个原始字符串,所以你基本上是给config一个字符串,我假设,vim 试图执行它作为 vimscript 代码。

A solution for that is simply stick to lua and convert your config = line to the following:一个解决方案就是坚持使用 lua 并将您的config =行转换为以下内容:

config = function() require("bufferline") end,

so in total in should look like this:所以总的来说应该是这样的:

-- Bufferline
  use {
    "akinsho/bufferline.nvim",
    tag = "v3.*",
    requires = 'nvim-tree/nvim-web-devicons',
    config = function() require("bufferline") end,
  } -- buffer list with icons

Important hint重要提示

You may also need to rename your bufferline.lua file because the module-name of the plugin already uses the name bufferline so require("bufferline") will actually get the module but not your bufferline.lua !你可能还需要重命名你的bufferline.lua文件,因为插件的模块名称已经使用了名称bufferline所以require("bufferline")实际上会得到模块而不是你的bufferline.lua

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM