mfussenegger / nvim-lint

An asynchronous linter plugin for Neovim complementary to the built-in Language Server Protocol support.
GNU General Public License v3.0
1.76k stars 191 forks source link

Configuration for `selene` #603

Closed v3natio closed 1 week ago

v3natio commented 1 week ago

Hi, I've installed selene through Mason. This is my current configuration:

local present, lint = pcall(require, 'lint')
if not present then
  return
end

lint.linters_by_ft = {
  lua = { 'selene' },
  python = { 'pylint' },
}

local lint_augroup = vim.api.nvim_create_augroup('lint', { clear = true })
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost', 'InsertLeave' }, {
  group = lint_augroup,
  callback = function()
    lint.try_lint()
  end,
})

Now, my issue is that I'm not sure how to properly set up selene. The thing is, I've got a selene.toml file in the ./nvim/ folder, and here are it's contents:

[selene]
base = "lua51"

[rules]
mixed_table = "allow"

[vim]
any = true

If I cd into the ./nvim/ directory from my terminal, and then open a .lua file, then selene works as expected. Otherwise, if I don't open it from that directory, then the configuration won't be loaded. selene itself does work, but the configuration to ignore the vim globals won't.

v3natio commented 1 week ago

In the meantime, I've found this configuration by folke, here's a snippet:

  {
    "mfussenegger/nvim-lint",
    opts = {
      linters_by_ft = {
        lua = { "selene", "luacheck" },
      },
      linters = {
        selene = {
          condition = function(ctx)
            local root = LazyVim.root.get({ normalize = true })
            if root ~= vim.uv.cwd() then
              return false
            end
            return vim.fs.find({ "selene.toml" }, { path = root, upward = true })[1]
          end,
        },
        luacheck = {
          condition = function(ctx)
            local root = LazyVim.root.get({ normalize = true })
            if root ~= vim.uv.cwd() then
              return false
            end
            return vim.fs.find({ ".luacheckrc" }, { path = root, upward = true })[1]
          end,
        },
      },
    },
  },
v3natio commented 1 week ago

With the idea from the solution above I realized I could leverage lsp's functionality to get the root directory of a project, and pass that onto the cwd argument of nvim-lint.

So instead of:

callback = function()
  lint.try_lint()
end,

we replace it with:

callback = function()
  local client = vim.lsp.get_clients({ bufnr = 0 })[1] or {}
  lint.try_lint(nil, { cwd = client.root_dir })
end,

This either takes the cwd from lsp, or won't set one at all, making it usable in all cases.