jose-elias-alvarez / null-ls.nvim

Use Neovim as a language server to inject LSP diagnostics, code actions, and more via Lua.
Other
3.62k stars 790 forks source link

Adding phpcs diagnostics #170

Closed marwan38 closed 3 years ago

marwan38 commented 3 years ago

Issues

Feature description

Null-ls has existing feature support for php formatting (phpcbf, and the newly added php-cs-fixer). php-cs is also a diagnostics tool and a linter for php. I'm new to neovim/vim land so it'd be quicker and more efficient for perhaps one of the more learned contributors here to add support for phpcs.

Help

Yes, but I don't know how to start. I would need guidance

Implementation help

Would null-ls be able to support it without any added logic?

jose-elias-alvarez commented 3 years ago

It really depends on the source itself. If it’s well-behaved (supports stdin and has well-formatted output), it should be simple, but otherwise it may require a little work.

I’m not at all familiar with PHP or the ecosystem, but maybe @gbprod (who has added some sources recently) could provide input?

marwan38 commented 3 years ago

It should be well-behaved according to your description. I use it with diagnosticls just fine, here is the config I use (found it floating on the internet):

   phpcs = {
      sourceName = "phpcs",
      command = "./vendor/bin/phpcs",
      debounce = 100,
      args = { "--report=emacs", "-s", "-" },
      offsetLine = 0,
      offsetColumn = 0,
      formatLines = 1,
      formatPattern = {
         [[^.*:(\d+):(\d+):\s+(.*)\s+-\s+(.*)(\r|\n)*$]],
         { line = 1, column = 2, security = 3, message = { "[phpcs] ", 4 } },
      },
      securities = { error = "error", warning = "warning" },
      rootPatterns = { ".git", "vendor", "composer.json" },
   },
gbprod commented 3 years ago

I'll take a look. IMHO, we should use phpcs json output that is more reliable that the emacs' output. The output looks like the phpstan's one (https://github.com/squizlabs/PHP_CodeSniffer/wiki/Reporting#printing-a-json-report), the difference is that phpcs can use stdin.

I try to make a PR before the end of the week ;)

marwan38 commented 3 years ago

@gbprod that's kind of you to do, thank you! :)

gbprod commented 3 years ago

@marwan38 Can you try #178 and tell me if it works for you ?

Regarding to you're diagnosticls settings, I think you should set it up like this :

use { "gbprod/null-ls", branch="feat-phpcs" } -- to use my branch (if you use packer)

local null_ls = require("null-ls")
null_ls.register(null_ls.builtins.diagnostics.phpcs.with({
    command = "./vendor/bin/phpcs",
}))
marwan38 commented 3 years ago

It was working earlier today when I re-synced after the pr merge. It stopped working for some reason, idk why. Tried 2 configs. I think something is wrong with my system, I will check again tomorrow.

marwan38 commented 3 years ago

@gbprod I can't figure out why it's not working. I have debug on, the log is not reporting anything broken. The log even indicates that it picked up errors and what not, but is not showing.

EDIT: Btw, I have started to play around with your nvim config, and this is what you see in the screenshots. :)

[DEBUG Sat Sep 11 10:28:24 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: received LSP notification for method textDocument/didChange
[DEBUG Sat Sep 11 10:28:24 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: running generators for method NULL_LS_DIAGNOSTICS
[DEBUG Sat Sep 11 10:28:24 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: spawning command ./vendor/bin/phpcs with args:
[DEBUG Sat Sep 11 10:28:24 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: { "--report=json", "-s", "-" }
[DEBUG Sat Sep 11 10:28:24 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: error output: Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(

[DEBUG Sat Sep 11 10:28:24 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: output: {"totals":{"errors":4,"warnings":0,"fixable":4},"files":{"STDIN":{"errors":4,"warnings":0,"messages":[{"message":"Line indented incorrectly; expected 4 spaces, found 0","source":"Generic.WhiteSpace.ScopeIndent.IncorrectExact","severity":5,"fixable":true,"type":"ERROR","line":5,"column":1},{"message":"Opening brace should be on a new line","source":"Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine","severity":5,"fixable":true,"type":"ERROR","line":5,"column":30},{"message":"Closing brace indented incorrectly; expected 0 spaces, found 4","source":"Squiz.WhiteSpace.ScopeClosingBrace.Indent","severity":5,"fixable":true,"type":"ERROR","line":6,"column":5},{"message":"Expected 1 blank line at end of file; 2 found","source":"PSR2.Files.EndFileNewline.TooMany","severity":5,"fixable":true,"type":"ERROR","line":7,"column":1}]}}}

[DEBUG Sat Sep 11 10:28:24 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: received diagnostics from generators
[DEBUG Sat Sep 11 10:28:24 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: {}
[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: received LSP request for method textDocument/formatting
[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: running generators for method NULL_LS_FORMATTING
[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: spawning command php-cs-fixer with args:
[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: { "--no-interaction", "--quiet", "fix", "/tmp/null-ls_5hWeKw.php" }
[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: error output: Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(

[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: output: nil
[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: received edits from generators:
[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: {}
[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: successfully applied edits:
[DEBUG Sat Sep 11 10:28:25 2021] ...ite/pack/packer/start/null-ls.nvim/lua/null-ls/utils.lua:51: {}

image image

sirbrillig commented 3 years ago

FWIW, I was trying to use this today and I also can't get it to report any messages. The debug shows that the cli tool is working and there's messages being returned:

[DEBUG Sat Sep 11 20:38:13 2021] ...rs/payton/.vim/bundle/null-ls.nvim/lua/null-ls/utils.lua:51: output: {"totals":{"errors":30,"warnings":13,"fixable":22},"files":{"STDIN":{"errors":30,"warnings":13,"messages":[{"message":"Found unused symbol 'function'.","source":"ImportDetection.Imports.RequireImports.Import","severity":5,"fixable":false,"type":"ERROR","line":13,"column":5},{"message":"Return type is missing [ TRUNCATED ] Use gmdate() instead.","source":"WordPress.DateTime.RestrictedFunctions.date_date","severity":5,"fixable":false,"type":"ERROR","line":487,"column":19}]}}}
[DEBUG Sat Sep 11 20:38:13 2021] ...rs/payton/.vim/bundle/null-ls.nvim/lua/null-ls/utils.lua:51: received diagnostics from generators
[DEBUG Sat Sep 11 20:38:13 2021] ...rs/payton/.vim/bundle/null-ls.nvim/lua/null-ls/utils.lua:51: {}

... but nothing comes out of the parser. I'm exploring this myself and my best guess right now is that the empty call to from_json({}) might be incorrect (I also am totally new to this codebase so I might be way off).

sirbrillig commented 3 years ago

After a bunch of debugging, I found a few things (at least for me). Firstly, it appears that the output from phpcs is being thrown away in this from_stderr check (because of this config). With that line changed, the errors pass through correctly!

Here's how I think from_json() should be called, though there's much I do not understand:

            local parser = h.diagnostics.from_json({
                attributes = {
                    severity = "type",
                    code = "source",
                },
                severities = {
                    ERROR = h.diagnostics.severities["error"],
                    WARNING = h.diagnostics.severities["warning"],
                }
            })
sirbrillig commented 3 years ago

I made PR https://github.com/jose-elias-alvarez/null-ls.nvim/pull/185 to hopefully resolve this.