Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow linter's cwd to be specifed as function #674

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fredrikaverpil
Copy link
Contributor

@fredrikaverpil fredrikaverpil commented Oct 3, 2024

Why this change?

When I set per-linter cwd I'm noticing that the value gets evaluated too early, when Neovim is launched while the lazy package manager is assembling all plugins' opts. This becomes problematic as I have some logic that needs to find a certain configuration file in order to give me back the desired path for the cwd. And it's also only applicable if I open a certain file type in a neovim buffer.

So, I'd like to instead provide cwd as a function, so that it will be called when actually performing the linting and only for relevant file types.

To better explain what I mean, I define my nvim-lint linters in per-language lua config files, and then I have a main nvim-lint lua file which merges all the opts before actually passing the opts into nvim-lint:

You can see how I need to specify the cwd for protobuf and I don't want that logic to run for any other linter.

What was changed?

  • Add the capability to handle a linter's opts.cwd in case a function was passed in.

Notes

  • Should probably add a note about this in the docs txt and/or README?
  • I'm a little confused by Set the cwd for each linter #646 which seems to indicate setting the cwd per linter is not even possible (or desired)... It feels like I've misunderstood something quite fundamental. But the docs mention it here and uv.spawn does receive the cwd as part of the linter_opts passed into it (here). It seems to me I can set cwd per-linter just fine... and this PR change seems to also work... 🤔

@fredrikaverpil fredrikaverpil marked this pull request as ready for review October 3, 2024 17:40
@mfussenegger
Copy link
Owner

I'm a little confused by #646 which seems to indicate setting the cwd per linter is not even possible (or desired).

yeah I kinda forgot that I added support for this, and also regret it a bit. Linter definitions should be mostly static & shareable between users and the cwd is more of a dynamic thing as it depends on the environment.

Looking at your configuration I wonder if this wouldn't be better solved by setting up different try_lint triggers.

E.g. I do have a generic:

  create_autocmd({'BufWritePost', 'BufEnter'}, {
    group = api.nvim_create_augroup('lint', { clear = true }),
    callback = function() lint.try_lint() end,
  })

For the linters defined in linters_by_ft but I also set up ad-hoc cmds like the following in filetype plugins:

local checkstyle_config = vim.uv.cwd() .. "/checkstyle.xml"
local has_checkstyle = vim.uv.fs_stat(checkstyle_config) and vim.fn.executable("checkstyle")
local is_main = api.nvim_buf_get_name(0):find("src/main/java") ~= nil
if has_checkstyle and is_main then
  api.nvim_create_autocmd({"BufEnter", "BufWritePost"}, {
    buffer = bufnr,
    group = api.nvim_create_augroup("checkstyle-" .. bufnr, { clear = true }),
    callback = function()
      if not vim.bo[bufnr].modified then
        require("lint.linters.checkstyle").config_file = checkstyle_config
        require("lint").try_lint("checkstyle")
      end
    end
  })
end

With that approach you could pass along the { cwd = ... } options.

fredrikaverpil added a commit to fredrikaverpil/dotfiles that referenced this pull request Oct 25, 2024
@fredrikaverpil
Copy link
Contributor Author

fredrikaverpil commented Oct 25, 2024

Thanks, that works. The buf_lint_cwd() call is only executed when I enter or save a *.proto file with this:

      vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost" }, {
        pattern = { "*.proto" },
        callback = function()
          require("lint").try_lint("buf_lint", {
            args = buf_lint_args,
            cwd = buf_lint_cwd(),
            append_fname = false,
          })
        end,
      })

However, since cwd is already supported/implemented in the opts, I don't quite understand why it wouldn't be okay to support cwd as a function (as proposed in this PR), without requiring this kind of workaround (the cwd feature is already there). It would go very well in hand with args as functions.

Either way, thanks for your hard work on nvim-lint and this suggestion! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants