GithubHelp home page GithubHelp logo

Comments (14)

ooloth avatar ooloth commented on July 30, 2024 4

Another vote for a run_if / condition option (based on the condition option in conform.nvim).

My use case is wanting my nvim config to include support for lots of linters per language, but only attach linters that are actually installed in a given project (so the linting in my editor matches the project's linting in CI). I use LazyVim, so while it's easy to pass a custom option (to opts), it's not easy to wrap the try_lint() call without overriding LazyVim's existing custom logic around that call.

As a Python example, with conform, I use a helper function that returns true if the formatter is installed in the project's virtual environment. It would be convenient to reuse that same helper for linters if a condition / run_if option could be made available here as well.

I assume that it would be hard to come up with good run_if defaults

I think defaulting to condition = true (or a function that returns true) would be totally fine.

from nvim-lint.

mfussenegger avatar mfussenegger commented on July 30, 2024 1

You could setup the autocmd that triggers try_lint conditionally on whether this config file exists or not.

For example, I've something like this triggered on FileType:

function M.enable_lint()
  if not require('lint').linters_by_ft[vim.bo.filetype] then
    return
  end
  local bufnr = api.nvim_get_current_buf()
  vim.cmd(string.format("augroup lint_%d", bufnr))
  vim.cmd("au!")
  vim.cmd(string.format("au BufWritePost <buffer=%d> lua require'lint'.try_lint()", bufnr))
  vim.cmd(string.format("au BufEnter <buffer=%d> lua require'lint'.try_lint()", bufnr))
  vim.cmd("augroup end")
  local opts = {
    silent = true;
  }
  api.nvim_buf_set_keymap(bufnr, "n", "]w", "<Cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
  api.nvim_buf_set_keymap(bufnr, "n", "[w", "<Cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
  api.nvim_buf_set_keymap(bufnr, "n", "<space>", "<Cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
end

I'm not really sure if it's worth to add a run_if predicate to the linters themselves. I feel like this may be something that is quite opinionated and then people end up wanting lots of different options to configure it.

from nvim-lint.

mfussenegger avatar mfussenegger commented on July 30, 2024 1

#26 adds support for passing the linter names directly to try_lint. (Main motivation was to be able to try out a linter without having to change the configuration file, but might be useful in the context of this issue here as well)

from nvim-lint.

yorickpeterse avatar yorickpeterse commented on July 30, 2024

@mfussenegger Maybe a simpler way would be to allow passing linter names to try_lint(), then have it only run those linters (if any are given)? That way you wouldn't have to define autocmd groups on the fly, or enable/disable the linter globally on every write.

from nvim-lint.

vinicius507 avatar vinicius507 commented on July 30, 2024

I'm using nvim-lint with the lint program of my school. I was thinking of a way to disable linting for specific files(like test files or projects outside of school). I didn't manage to do it but it would be a nice feature to have.

from nvim-lint.

mfussenegger avatar mfussenegger commented on July 30, 2024

Maybe a simpler way would be to allow passing linter names to try_lint(), then have it only run those linters (if any are given)? That way you wouldn't have to define autocmd groups on the fly, or enable/disable the linter globally on every write.

Sounds okay but I think I don't fully understand how that would be helpful. How would you use that?
You'll still need to setup some kind of autocmd that then decides which linters to use, no?

from nvim-lint.

vinicius507 avatar vinicius507 commented on July 30, 2024

In my case I created a wrapper for the try_lint() function.

It is something like this:

M.lint = function()
    if M.should_lint() then
        -- ...
        require('nvim-lint').try_lint()
    end
end

Then I just configured the autocmd's to use it.

from nvim-lint.

mfussenegger avatar mfussenegger commented on July 30, 2024

So if I understand this right, you'd continue using this pattern, except that should_lint would decide which linters to use?

If so - sounds good to me to extend try_lint to take an optional linter_names argument

from nvim-lint.

yorickpeterse avatar yorickpeterse commented on July 30, 2024

Thinking of it: passing the linter names to try_lint could be a bit odd. Imagine that for buffer X we have enabled (globally) linters A and B. Using a custom function of sorts we call try_lint and pass it linter C, which doesn't support the buffer's file type. What's supposed to happen in this case? Does it simply get ignored, or do we just run the linter and hope for the best?

from nvim-lint.

vinicius507 avatar vinicius507 commented on July 30, 2024

So if I understand this right, you'd continue using this pattern, except that should_lint would decide which linters to use?

If so - sounds good to me to extend try_lint to take an optional linter_names argument

should_lint verifies if the current file is one that we want to be linted. This way you can control the linting process in any way you want. If you wish to add a feature where the user can disable linting globally and/or per file, it could be verified by should_lint.

In my case I did it because my lint configuration can only be used on projects of my school. Projects outside of my school don't need the linting configuration of my school :)

from nvim-lint.

yorickpeterse avatar yorickpeterse commented on July 30, 2024

I ended up piecing together a custom linter setup in my dotfiles repository; mostly so I have full control over it and can tweak it to my liking. My setup supports both conditionally enabling linters (example) and more dynamic commands/arguments (example). Feel free to steal some of these ideas if they're useful 😄

from nvim-lint.

mfussenegger avatar mfussenegger commented on July 30, 2024

Closing this for now as I don't have a concrete plan how to improve this and I think the current options are good enough.

If there are further concrete suggestions how the APIs could be improved I'm open for suggestions .

from nvim-lint.

AckslD avatar AckslD commented on July 30, 2024

@mfussenegger I think having eg an optional run_if callback would make a lot of sense. Lets say you have multiple linters for a given filetype but only want some of them to run based on come condition. Setting up autocommands which trigger try_lint with then linter name based on some condition seems a lot more cumbersome than being able to define a condition for the linter to be used as a single function.

Although maybe I'm not fully understanding how you suggest this to be done at the moment. Lets say that I have flake8 and mypy for python but I only want mypy to run if there is a tox.ini in the current directory, how would I do that?

Thanks for the amazing plugin ❤️

from nvim-lint.

mfussenegger avatar mfussenegger commented on July 30, 2024

Although maybe I'm not fully understanding how you suggest this to be done at the moment. Lets say that I have flake8 and mypy for python but I only want mypy to run if there is a tox.ini in the current directory, how would I do that?

I'd probably run a function in init.lua that modifies the linters_by_ft table depending on whether the file exists or not.
Or if you need to do the check each time you could have your custom try_lint function that does that before triggering the lint.try_lint() function.

I don't see a big difference in difficulty between such an approach or having a run_if predicate on each linter. Given that I assume that it would be hard to come up with good run_if defaults, I'd rather not introduce it at this point.

from nvim-lint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.