wishlist icon indicating copy to clipboard operation
wishlist copied to clipboard

Linter/Fixer

Open kristijanhusak opened this issue 4 years ago • 38 comments

What? Basically https://github.com/dense-analysis/ale, but only linter and fixer. Other things are already implemented via lsp.

Why? Linting is a thing that runs almost constantly, especially when real time execution is enabled. I believe having it in lua would improve the performance.

Potential existing implementations:

  • https://github.com/dense-analysis/ale
  • https://github.com/neomake/neomake
  • https://github.com/vim-syntastic/syntastic

Potential pitfalls: Migrating all linter/fixer implementations that exist in ale, from vimscript to lua. Guess it can be done slowly over time.

I'm not a lua developer, but I can definitely help on this.

kristijanhusak avatar Sep 08 '20 09:09 kristijanhusak

Just to clarify, this differs from the LSP-provided diagnostics in that it focuses on running standalone linters?

wbthomason avatar Sep 08 '20 13:09 wbthomason

@wbthomason yeah, standalone linters and fixers. For example, most JS projects use eslint for linting, and prettier for fixing.

kristijanhusak avatar Sep 08 '20 13:09 kristijanhusak

@kristijanhusak I just started using https://github.com/neovim/nvim-lspconfig#diagnosticls right now dealing with some issues configuring but looks like it is a replacement of ale. But I still need some time to confirm that is an alternative.

carlitux avatar Sep 08 '20 13:09 carlitux

@carlitux I wasn't aware of that. I'll give it a try, thanks!

kristijanhusak avatar Sep 08 '20 13:09 kristijanhusak

@kristijanhusak were you able to setup? I had many issues diagnosticls

carlitux avatar Sep 15 '20 16:09 carlitux

@carlitux i haven't tried yet. Will get back to you when i try it.

kristijanhusak avatar Sep 15 '20 16:09 kristijanhusak

How you describe "fixers" sounds actually more like a code formatter. At least that's what your example Prettier is doing. It does not fix your code. It just makes it standardized formatted. But it would deny to format if there are syntax errors for example. A tool that support this would be neoformat

weilbith avatar Sep 15 '20 19:09 weilbith

Also https://github.com/mhartington/formatter.nvim

wbthomason avatar Sep 15 '20 19:09 wbthomason

@wbthomason plans to support linters?

carlitux avatar Sep 15 '20 19:09 carlitux

@carlitux Sorry, support linters in what? formatter.nvim is not my project; this repo is just a place to collect ideas for people to adopt and implement.

wbthomason avatar Sep 15 '20 19:09 wbthomason

@wbthomason sorry, I didn't read well. Thanks!

carlitux avatar Sep 15 '20 21:09 carlitux

@carlitux I managed to set up linting with eslint, and prettier, with this config:

nvim_lsp.diagnosticls.setup{
  filetypes={'javascript'},
  init_options = {
    linters = {
      eslint = {
        command = './node_modules/.bin/eslint',
        rootPatterns = {'.git'},
        debounce = 100,
        args = {
          '--stdin',
          '--stdin-filename',
          '%filepath',
          '--format',
          'json'
        },
        sourceName = 'eslint',
        parseJson = {
          errorsRoot = '[0].messages',
          line = 'line',
          column = 'column',
          endLine = 'endLine',
          endColumn = 'endColumn',
          message = '${message} [${ruleId}]',
          security = 'severity'
        },
        securities = {
          [2] = 'error',
          [1] = 'warning',
        },
      },
    },
    filetypes = {
      javascript = 'eslint'
    },
    formatters = {
      prettier = {
        command = "./node_modules/.bin/prettier",
        args = {"--stdin-filepath" ,"%filepath", '--single-quote', '--print-width 120'}
      }
    },
    formatFiletypes = {
      javascript = "prettier"
    },
  }
}

I wasn't able to set up eslint fix or prettier-eslint, it failed to spawn those, even though I have them installed. Linting looked fine. It doesn't work for me because I thought it supports textDocument/range_formatting, which is the only thing I'm currently missing from Ale.

kristijanhusak avatar Sep 16 '20 10:09 kristijanhusak

@kristijanhusak issues with 'textDocument/definition' or 'textDocument/documentSymbol'? for me breaks others

carlitux avatar Sep 16 '20 14:09 carlitux

@carlitux I didn't try to run any other LSP server besides it. Most likely there would be some issues in that case. I don't think it can work if you have more than 1 LSP per filetype.

kristijanhusak avatar Sep 16 '20 15:09 kristijanhusak

Great stuff .. the config you posted worked perfectly @kristijanhusak

It would be nice to see the bugs worked out with multiple LSPs & single files. I was running a JS lang server & diagnosticls; unfortunately, ran into similar issues as @carlitux .

Here's one of multiple LSP bugs that's currently being addressed: https://github.com/neovim/neovim/pull/12764

vinnyA3 avatar Sep 18 '20 01:09 vinnyA3

@carlitux I managed to set up linting with eslint, and prettier, with this config:

nvim_lsp.diagnosticls.setup{
  filetypes={'javascript'},
  init_options = {
    linters = {
      eslint = {
        command = './node_modules/.bin/eslint',
        rootPatterns = {'.git'},
        debounce = 100,
        args = {
          '--stdin',
          '--stdin-filename',
          '%filepath',
          '--format',
          'json'
        },
        sourceName = 'eslint',
        parseJson = {
          errorsRoot = '[0].messages',
          line = 'line',
          column = 'column',
          endLine = 'endLine',
          endColumn = 'endColumn',
          message = '${message} [${ruleId}]',
          security = 'severity'
        },
        securities = {
          [2] = 'error',
          [1] = 'warning',
        },
      },
    },
    filetypes = {
      javascript = 'eslint'
    },
    formatters = {
      prettier = {
        command = "./node_modules/.bin/prettier",
        args = {"--stdin-filepath" ,"%filepath", '--single-quote', '--print-width 120'}
      }
    },
    formatFiletypes = {
      javascript = "prettier"
    },
  }
}

I wasn't able to set up eslint fix or prettier-eslint, it failed to spawn those, even though I have them installed. Linting looked fine. It doesn't work for me because I thought it supports textDocument/range_formatting, which is the only thing I'm currently missing from Ale.

Awesome! I was trying to do this today and this worked really well. Maybe we need some docs for right configuration?

lucastrvsn avatar Sep 30 '20 20:09 lucastrvsn

Great stuff .. the config you posted worked perfectly @kristijanhusak

It would be nice to see the bugs worked out with multiple LSPs & single files. I was running a JS lang server & diagnosticls; unfortunately, ran into similar issues as @carlitux .

Here's one of multiple LSP bugs that's currently being addressed: neovim/neovim#12764

looks like this is fixed and working really well, just trying to find the best way to format before save right now with this config

  if client.resolved_capabilities.document_formatting then
    vim.cmd("autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync{}")
  end

carlitux avatar Oct 26 '20 15:10 carlitux

Newer programming languages provide one tool for the linting. Older programming languages have many.

Is the goal to simplify or give any sort of reference on this?

matu3ba avatar Dec 25 '20 01:12 matu3ba

@matu3ba literally a tool same as Ale mentioned in the first comment, but only the linter and fixer part. Ale has some other things that are not necessary since they are already implemented (LSP for example).

kristijanhusak avatar Dec 26 '20 18:12 kristijanhusak

Does the linter/fixer need to be in lua when there is already an option to use the efm-languageserver (written in go) with lspconfig? The suggestion to use efm-language server for linting and formatting in neovim was made here efm-languageserver. I have been using this solution for a variety of languages and it works very well my implementation can be referenced here dots.

icew4ll avatar Feb 13 '21 13:02 icew4ll

nvim-lint is promising: https://github.com/mfussenegger/nvim-lint/

fsouza avatar Feb 26 '21 02:02 fsouza

I would suggest to keep linter and fixer separate programs as the fixer rewrites the file with the possibilities to mess up your files. (So I prefer to invoke simple binaries that may optionally read a project-local config and always work/never fail as to limit any possibilities of errors.)

The linter should only do static code analysis (on source- or intermediate representation) of the code.

matu3ba avatar Mar 04 '21 13:03 matu3ba

@matu3ba agreed, right now I'm looking at nvim-lint and formatter.nvim as two strong candidates, but I still use efm personally.

fsouza avatar Mar 04 '21 13:03 fsouza

Can anyone help me setting up nvim-lint for pylint(python)

FardeenCodes avatar Mar 11 '21 18:03 FardeenCodes

Can anyone help me setting up nvim-lint for pylint(python)

I would suggest that you ask on the nvim-lint repo - this repo is for gathering wishes and candidates for their fulfillment.

wbthomason avatar Mar 12 '21 02:03 wbthomason

You can try using null-ls. This plugin allows pretty much anything to be hooked up to the existing lsp protocol.

It includes plenty of builtins for different linters an formatters

sarmong avatar Jan 03 '22 11:01 sarmong

I guess this is already done right? via what @sarmong mentioned, https://github.com/jose-elias-alvarez/null-ls.nvim

luisiacc avatar Jan 29 '22 17:01 luisiacc

I would recommend nvim-lint, between the two of these what else are people missing?

mjlbach avatar Jan 29 '22 17:01 mjlbach

I'm currently using https://github.com/creativenull/diagnosticls-configs-nvim, which provides predefined configs for diagnosticls.

kristijanhusak avatar Jan 29 '22 17:01 kristijanhusak

@mjlbach Would you mind elaborating a bit on why you recommend using nvim-lint over null-ls, please? Genuinely curious.

gegoune avatar Jan 30 '22 07:01 gegoune