folke / lazy.nvim

💤 A modern plugin manager for Neovim
https://lazy.folke.io/
Apache License 2.0
14.07k stars 336 forks source link

`ft` loading doesn't work when i have ftplugins #672

Closed KaitlynEthylia closed 1 year ago

KaitlynEthylia commented 1 year ago

So I'm currently working on moving everything from packer to lazy, mainly because i want to split my spec across multiple file. I have some plugins that i have set to ft = 'haskell' because i only want them to load in Haskell files, but i also have a files nvim/ftplugin/haskell/settings.lua with a few Haskell specific settings. If i open a .hs file and run :Lazy i can see that the Haskell plugins aren't loaded, but if i remove the ftplugin folder and open a .hs file, they do load

Is there any way i can get these two things working together? And even better is there a way to put part of my spec in the ftplugin folder to keep it more organised

folke commented 1 year ago

Sorry, but this is too vague. If you want me to look at it, I need a repro as is instructed in the issue template.

KaitlynEthylia commented 1 year ago

I couldn't find an issue template, but the simplest way to recreate the issue is this

~/.config/nvim/
  ftplugin/
    haskell.lua
  lua/
    plugins.lua
  init.lua

init.lua looks like this

local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

plugins.lua looks like this

return {
  {
    'folke/tokyonight.nvim',
    ft = 'haskell',
  }
}

and haskell.lua looks like this

vim.cmd.colorscheme('tokyonight-moon')

if i try to open a .hs file, it gives me an error that the colourscheme doesn't exist and if i run :Lazy the plugin isn't loaded

if i remove haskell.lua and open a .hs file, there is no error and :Lazy shows that the plugin loaded

folke commented 1 year ago

I looked into this and it's impossible to fix. You will need to remove ft=haskell, or move your ftplugin stuff somewhere else.

The reason this can't work, is that NEovim loads the ftplugins before lazy's filetype handler triggers. The NEovim filetype autocmd is also not nested, so the colorscheme can't also be auto-loaded.

Another way you can make this work is in your ftplugin, have a function and pcall that function without triggering an error. This way, lazy's ft handler will kick in and will also load your ftplugin when the plugin is loaded.

loic86 commented 1 year ago

I'm trying to setup nvim-jdtls and the plugin requires to create a config in nvim/ftplugin/java.lua like this:

local config = {
    cmd = {'/path/to/jdt-language-server/bin/jdtls'},
    root_dir = vim.fs.dirname(vim.fs.find({'gradlew', '.git', 'mvnw'}, { upward = true })[1]),
}
require('jdtls').start_or_attach(config)

I can somewhat manage to run by doing this on my lua/custom/plugins/nvim-jdtls.lua:

return {
    "mfussenegger/nvim-jdtls",
    ft = "java",
    config = function()
        local config = {
            cmd = { home .. "/.local/share/nvim/mason/bin/jdtls" },
            root_dir = vim.fs.dirname(vim.fs.find({ "gradlew", ".git", "mvnw" }, { upward = true })[1]),
        }
        require("jdtls").start_or_attach(config)
    end,
}

but now jdtls config is running always, how can I limit it running the java filetype? I'm sorry my lua is not perfect and I cannot understand what "Another way you can make this work is in your ftplugin, have a function and pcall that function without triggering an error. This way, lazy's ft handler will kick in and will also load your ftplugin when the plugin is loaded." means, is there a code example where I can learn from? Thank you for your time.