dense-analysis / ale

Check syntax in Vim/Neovim asynchronously and fix files, with Language Server Protocol (LSP) support
BSD 2-Clause "Simplified" License
13.57k stars 1.44k forks source link

Allow to specify different arguments for same tool depending on whether it's linting or fixing #4543

Closed MaxG87 closed 1 year ago

MaxG87 commented 1 year ago

Hi there,

thank you for the great tool! I depend really on it!

tl;dr: I would like to specify let g:ale_python_ruff_options = '--select=I', but only when running fixers, not when generating lints. I am sure other use cases would benefit from that too.

I recently onboarded ruff to my Python tool chain. I have enabled all sorts of lints, but in particular the usual flake8 and isort rules are enabled. I want to replace my usage of isort with it, among other things.

When I got rid of isort in some project, I noticed that the imports were not automatically ordered when saving. So I added ruff to the list of ALE Fixers. Unfortunately, this is annoying, as now, upon saving, unused imports and unused variables are removed by ruff.

So, I could add let g:ale_python_ruff_options = '--select=I' to my VIM settings. Unfortunately, with that I would disable all interesting and helpful lints too. What I actually would like to do is to pass settings to ruff only when running it as fixer, not as linter.

pbnj commented 1 year ago

Hi @MaxG87

You can try using ALEFixPre and ALEFixPost autocmds to control this. I have not tried this myself, but something similar to this might be what you're looking for:

augroup PythonRuffFixer
  autocmd!
  autocmd User ALEFixPre  let b:ale_python_ruff_options = '--select=I'
  autocmd User ALEFixPost let b:ale_python_ruff_options = ''
augroup END
MaxG87 commented 1 year ago

This works. Thank you very much.

MaxG87 commented 1 month ago

I had to translate the autocommand group to Lua yesterday. It was quite hard to figure out, so I want to share the result with anyone dropping in here:

local python_ruff_fixer = vim.api.nvim_create_augroup("PythonRuffFixer", { clear = true })
vim.api.nvim_create_autocmd({ "User" }, {
    pattern = "ALEFixPre",
    group = python_ruff_fixer,
    callback = function() vim.g.ale_python_ruff_options = '--select=I' end,
})
vim.api.nvim_create_autocmd({ "User" }, {
    pattern = "ALEFixPost",
    group = python_ruff_fixer,
    callback = function() vim.g.ale_python_ruff_options = '' end ,
})