neodev.nvim icon indicating copy to clipboard operation
neodev.nvim copied to clipboard

bug: no completions for single file

Open fcying opened this issue 10 months ago • 6 comments

Did you check docs and existing issues?

  • [X] I have read all the neodev.nvim docs
  • [X] I have searched the existing issues of neodev.nvim
  • [X] I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

NVIM v0.9.5

Operating system/version

debian 12

Describe the bug

I used override to force config library.enabled, but when modifying a single Lua file (without .git, without lua folder), completion still does not work. after creating an empty lua folder(or run git init) in the directory, completion works fine.

Steps To Reproduce

  1. rm -rf ./lua
  2. nvim -u init.lua .nvim.lua
  3. type vim., can't completion,
:lua print(vim.inspect(vim.lsp.get_active_clients({ name = "lua_ls" })[1].config.settings.Lua))
{
  runtime = {
    path = { "?.lua", "?/init.lua" },
    pathStrict = true,
    version = "LuaJIT"
  },
  workspace = {
    ignoreDir = { "types/nightly", "lua" },
    library = { "/home/fcying/test/nvim/.repro/plugins/neodev.nvim/types/stable", "/usr/local/share/nvim/runtime/lua" }
  }
}
  1. mkdir lua
  2. nvim -u init.lua .nvim.lua
  3. type vim., completion work fine.
:lua print(vim.inspect(vim.lsp.get_active_clients({ name = "lua_ls" })[1].config.settings.Lua))
{
  runtime = {
    path = { "?.lua", "?/init.lua" },
    pathStrict = true,
    version = "LuaJIT"
  },
  workspace = {
    ignoreDir = { "types/nightly", "lua" },
    library = { "/home/fcying/test/nvim/.repro/plugins/neodev.nvim/types/stable", "/usr/local/share/nvim/runtime/lua", "/home/fcying/test/nvim/lua" }
  }
}

Expected Behavior

completion work for single file

Repro

local g, fn = vim.g, vim.fn
g.mapleader = " "
g.config_dir = fn.fnamemodify(fn.resolve(fn.expand("<sfile>:p")), ":h")
g.runtime_dir = g.config_dir .. "/.repro"
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = g.runtime_dir .. "/" .. name
end

local lazypath = g.runtime_dir .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "--single-branch",
        "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    "folke/tokyonight.nvim",
    {
        "hrsh7th/nvim-cmp",
        version = false, -- last release is way too old
        dependencies = { { "hrsh7th/cmp-nvim-lsp" }, },
        config = function()
            local cmp = require("cmp")
            cmp.setup({ sources = cmp.config.sources({ { name = "nvim_lsp" }, }), })
        end,
    },
    {
        "neovim/nvim-lspconfig",
        dependencies = { { "folke/neodev.nvim" }, },
        config = function()
            require("neodev").setup({
                override = function(root_dir, library)
                    library.enabled = true
                    library.plugins = false
                    --print(root_dir .. " " .. vim.inspect(library))
                end,
            })

            local opts = {
                single_file_support = true,
                settings = {
                    Lua = { }
                },
            }
            require("lspconfig").lua_ls.setup(opts)
        end
    },
}

require("lazy").setup(plugins, {
    root = g.runtime_dir .. "/plugins",
})
vim.cmd.colorscheme("tokyonight")

fcying avatar Apr 01 '24 04:04 fcying

same here :/

pkos98 avatar Apr 20 '24 12:04 pkos98

I was able to use override to get completions working for .nvim.lua, since I use nvim-config-local:

The option must be a part of the neodev configuration, and that configuration must happen with neodev declared as a dependency of nvim-lspconfig. Assuming use of LazyVim or lazy.nvim, the spec looks like this:

  {
    "neovim/nvim-lspconfig",
    dependencies = {
      {
        "folke/neodev.nvim",
        opts = {
          override = function(_, opts)
            local path = vim.api.nvim_buf_get_name(0)
            if path then
              local name = vim.fs.basename(path)
              if name == ".nvim.lua" then
                opts.enabled = true
                opts.plugins = true
              end
            end
          end,
        },
      },
    },
  },

gotgenes avatar Apr 24 '24 19:04 gotgenes

I was able to use override to get completions working for .nvim.lua, since I use nvim-config-local:

The option must be a part of the neodev configuration, and that configuration must happen with neodev declared as a dependency of nvim-lspconfig. Assuming use of LazyVim or lazy.nvim, the spec looks like this:

  {
    "neovim/nvim-lspconfig",
    dependencies = {
      {
        "folke/neodev.nvim",
        opts = {
          override = function(_, opts)
            local path = vim.api.nvim_buf_get_name(0)
            if path then
              local name = vim.fs.basename(path)
              if name == ".nvim.lua" then
                opts.enabled = true
                opts.plugins = true
              end
            end
          end,
        },
      },
    },
  },

I tried putting neodev's configuration into it's 'opts', but the result is the same. If there is no lua directory or it's not a git repository, completion cannot be done.

local g, fn = vim.g, vim.fn
g.mapleader = " "
g.config_dir = fn.fnamemodify(fn.resolve(fn.expand("<sfile>:p")), ":h")
g.runtime_dir = g.config_dir .. "/.repro"
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = g.runtime_dir .. "/" .. name
end

local lazypath = g.runtime_dir .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "--single-branch",
        "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

function lsp_progress()
    local messages = vim.lsp.util.get_progress_messages()

    if vim.tbl_count(messages) > 0 then
        local message1 = messages[1]
        --vim.print(messages)

        local name = message1.name or nil
        local title = message1.title or nil
        local msg = message1.message or nil
        local percentage = message1.percentage or nil
        local progress = message1.progress or true

        if progress then
            return (name or "")
                .. (title and (" " .. title) or "")
                .. (msg and (" " .. msg) or "")
                .. (percentage and (" " .. percentage .. "%%") or "")
        end
    end

    return ""
end
--vim.opt.stl = lsp_progress()

local plugins = {
    "folke/tokyonight.nvim",
    {
        "hrsh7th/nvim-cmp",
        version = false, -- last release is way too old
        dependencies = { { "hrsh7th/cmp-nvim-lsp" }, },
        config = function()
            local cmp = require("cmp")
            cmp.setup({ sources = cmp.config.sources({ { name = "nvim_lsp" }, }), })
        end,
    },
    {
        "neovim/nvim-lspconfig",
        dependencies = { {
            "folke/neodev.nvim",
            opts = {
                override = function(root_dir, library)
                    library.enabled = true
                    library.plugins = true
                    --print(root_dir .. " " .. vim.inspect(library))
                end,
            }
        }, },
        config = function()
            --require("neodev").setup({
            --    override = function(root_dir, library)
            --        library.enabled = true
            --        library.plugins = true
            --        --print(root_dir .. " " .. vim.inspect(library))
            --    end,
            --})

            local opts = {
                single_file_support = true,
                settings = {
                    Lua = {}
                },
            }
            require("lspconfig").lua_ls.setup(opts)
        end
    },
}

require("lazy").setup(plugins, {
    root = g.runtime_dir .. "/plugins",
})
vim.cmd.colorscheme("tokyonight")

fcying avatar Apr 25 '24 00:04 fcying

BTW, If manually config lua_ls opts, completion works fine. With the following configuration, only install neodev, using its types without setup.

local g, fn = vim.g, vim.fn
g.mapleader = " "
g.config_dir = fn.fnamemodify(fn.resolve(fn.expand("<sfile>:p")), ":h")
g.runtime_dir = g.config_dir .. "/.repro"
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = g.runtime_dir .. "/" .. name
end

local lazypath = g.runtime_dir .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "--single-branch",
        "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    "folke/tokyonight.nvim",
    {
        "hrsh7th/nvim-cmp",
        version = false, -- last release is way too old
        dependencies = { { "hrsh7th/cmp-nvim-lsp" }, },
        config = function()
            local cmp = require("cmp")
            cmp.setup({ sources = cmp.config.sources({ { name = "nvim_lsp" }, }), })
        end,
    },
    {
        "neovim/nvim-lspconfig",
        dependencies = { { "folke/neodev.nvim", lazy = true, }, },
        config = function()
            local opts = {
                single_file_support = true,
                settings = {
                    Lua = {
                        workspace = { checkThirdParty = false, },
                        completion = { callSnippet = "Replace", },
                        diagnostics = {
                            globals = { "vim" },
                            enable = true,
                        },
                    },
                },
            }

            local filename = vim.fn.expand("%:t")
            if filename == ".nvim.lua" then
                local plugins_dir = g.runtime_dir .. "/plugins"
                opts.settings.Lua.workspace.library = {
                    plugins_dir .. "/neodev.nvim/types/stable",
                    vim.fn.expand("$VIMRUNTIME/lua"),
                }
            end
            require("lspconfig").lua_ls.setup(opts)
        end
    },
}

require("lazy").setup(plugins, {
    root = g.runtime_dir .. "/plugins",
})
vim.cmd.colorscheme("tokyonight")

fcying avatar Apr 25 '24 00:04 fcying

My apologies. I did have a .git directory, so a root was detected and the Lua language server ran in workspace mode.

This might have something to do with the Lua language server running in "single-file mode", which it seems to run in when no project root is detected. It might be outside the control of neodev, which still seems to be setting up everything it can correctly.

If you put

    vim.notify("config.settings.Lua: " .. vim.inspect(config.settings.Lua))

after line 111 in lua/neodev/lsp.lua, like so

    for _, dir in ipairs(ignoreDir) do
      table.insert(config.settings.Lua.workspace.ignoreDir, dir)
    end
    vim.notify("config.settings.Lua: " .. vim.inspect(config.settings.Lua))

you will see that neodev does configure the workspace settings, even if you are editing a Lua file outside of a project (e.g., /tmp/init.lua) where Lua LS runs in single-file mode.

I suppose it would make sense that the Lua LS would ignore these workspace settings when in single-file mode. So I think neodev and single-file mode are mutually exclusive, unfortunately.

gotgenes avatar Apr 25 '24 16:04 gotgenes

My apologies. I did have a .git directory, so a root was detected and the Lua language server ran in workspace mode.

This might have something to do with the Lua language server running in "single-file mode", which it seems to run in when no project root is detected. It might be outside the control of neodev, which still seems to be setting up everything it can correctly.

If you put

    vim.notify("config.settings.Lua: " .. vim.inspect(config.settings.Lua))

after line 111 in lua/neodev/lsp.lua, like so

    for _, dir in ipairs(ignoreDir) do
      table.insert(config.settings.Lua.workspace.ignoreDir, dir)
    end
    vim.notify("config.settings.Lua: " .. vim.inspect(config.settings.Lua))

you will see that neodev does configure the workspace settings, even if you are editing a Lua file outside of a project (e.g., /tmp/init.lua) where Lua LS runs in single-file mode.

I suppose it would make sense that the Lua LS would ignore these workspace settings when in single-file mode. So I think neodev and single-file mode are mutually exclusive, unfortunately.

Oddly, when I directly configure with lua_ls, it supports completion for single file, but when I add neodev setup, it complete failed, :lua vim.print(vim.lsp.get_active_clients({ name = "lua_ls" })[1].config.settings.Lua) only ignoreDir library has some duplicate items.

local g, fn = vim.g, vim.fn
g.mapleader = " "
g.config_dir = fn.fnamemodify(fn.resolve(fn.expand("<sfile>:p")), ":h")
g.runtime_dir = g.config_dir .. "/.repro"
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = g.runtime_dir .. "/" .. name
end

local lazypath = g.runtime_dir .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "--single-branch",
        "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

local plugins = {
    "folke/tokyonight.nvim",
    {
        "hrsh7th/nvim-cmp",
        version = false, -- last release is way too old
        dependencies = { { "hrsh7th/cmp-nvim-lsp" }, },
        config = function()
            local cmp = require("cmp")
            cmp.setup({ sources = cmp.config.sources({ { name = "nvim_lsp" }, }), })
        end,
    },
    {
        "neovim/nvim-lspconfig",
        dependencies = { { "folke/neodev.nvim" }, },
        config = function()
            -- if setup neodev, single_file complete failed
            --require("neodev").setup({
            --    override = function(root_dir, library)
            --        library.enabled = true
            --        library.plugins = false
            --    end,
            --})

            local opts = {
                single_file_support = true,
                settings = {
                    Lua = {
                        workspace = { 
                            checkThirdParty = false, 
                            ignoreDir = { "types/nightly", "lua" },
                        },
                        completion = { callSnippet = "Replace", },
                        diagnostics = {
                            globals = { "vim" },
                            enable = true,
                        },
                        runtime = {
                            path = { "?.lua", "?/init.lua" },
                            pathStrict = true,
                            version = "LuaJIT"
                        },
                    },
                },
            }

            local filename = vim.fn.expand("%:t")
            if filename == ".nvim.lua" then
                local plugins_dir = g.runtime_dir .. "/plugins"
                opts.settings.Lua.workspace.library = {
                    plugins_dir .. "/neodev.nvim/types/stable",
                    vim.fn.expand("$VIMRUNTIME/lua"),
                }
            end
            require("lspconfig").lua_ls.setup(opts)
        end
    },
}

require("lazy").setup(plugins, {
    root = g.runtime_dir .. "/plugins",
})
vim.cmd.colorscheme("tokyonight")

fcying avatar Apr 27 '24 00:04 fcying

work fine with lazydev.nvim

fcying avatar Jun 02 '24 13:06 fcying