none-ls-extras.nvim icon indicating copy to clipboard operation
none-ls-extras.nvim copied to clipboard

[Feat] Add `stylua` diagnostics

Open AlejandroSuero opened this issue 1 year ago • 4 comments

Issues

  • [X] I have checked existing issues and there are no existing ones with the same request.

Feature description

Using the command stylua --check you can get diagnostic about the format style using stylua.

Help

Yes

Implementation help

Inside lua/null-ls/builtins/diagnostics, I would create a file stylua.lua with the following implementation.

[!note] I don't know exactly how the diagnostics implementation works 100%.

The command to use would be:

stylua --config-path <path-to-config> --check --color Never --output-format Json <files>
Command in action on project Stylua check
{
  "file": "./scripts/docgen.lua",
  "mismatches": [
    {
      "expected": "    txt = \"doc/colorscheme_configurations.txt\",\n",
      "expected_end_line": 259,
      "expected_start_line": 259,
      "original": "    txt = \"doc/colorscheme_configurations.txt\"\n",
      "original_end_line": 259,
      "original_start_line": 259
    }
  ]
}

Used jq to format

local h = require("null-ls.helpers")
local utils = require("null-ls.utils")
local methods = require("null-ls.methods")

local DIAGNOSTICS = methods.internal.DIAGNOSTICS

return h.make_builtin({
    name = "stylua",
    meta = {
        url = "https://github.com/JohnnyMorganz/StyLua",
        description = "An opinionated code formatter for Lua.",
    },
    method = DIAGNOSTICS,
    filetypes = { "lua", "luau" },
    generator_opts = {
        command = "stylua",
        args = { "--color", "never", "--output-format", "json", "--check", "$FILENAME"},
        format = "json",
        from_stderr = true,
        to_stdin = true,
        on_output = h.diagnostics.from_json({
            attributes = {
                message = "message",
                source = "stylua",
                code = "kind",
                severity = 1,
            },
        }),
        cwd = h.cache.by_bufnr(function(params)
            return utils.root_pattern("stylua.toml")(params.bufname)
        end),
    },
    factory = h.generator_factory,
})

[!note] When trying this configuration, I didn't get to show the diagnostics

:NullLsInfo

	Active source(s)
	* name: stylua
	* filetypes: lua | luau
	* methods: formatting | range_formatting
	
	* name: stylua
	* filetypes: lua | luau
	* methods: diagnostics
	
	* name: refactoring
	* filetypes: lua | go | typescript | python | javascript
	* methods: code_action
	
	* name: selene
	* filetypes: lua | luau
	* methods: diagnostics
	
	
	Supported source(s)
	* formatting: stylua
	* code_actions: refactoring
	* diagnostics: selene

:NullLsLog

[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/client.lua:114: starting null-ls client
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/rpc.lua:106: received LSP request for method initialize
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/client.lua:182: unable to notify client for method textDocument/didOpen (client not active): {
  textDocument = {
    uri = "file:///Users/aome/dev/nvim_plugins/colorscheme-installer.nvim/scripts/docgen.lua"
  }
}
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/sources.lua:85: not registering conditional source ansiblelint
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/sources.lua:82: registering conditional source selene
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/sources.lua:82: registering conditional source stylua
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/sources.lua:85: not registering conditional source luacheck
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/rpc.lua:131: received LSP notification for method initialized
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/rpc.lua:131: received LSP notification for method textDocument/didOpen
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_OPEN
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:329: spawning command "selene" at /Users/aome/dev/nvim_plugins/colorscheme-installer.nvim with args { "--display-style", "quiet", "-" }
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:329: spawning command "stylua" at /Users/aome/dev/nvim_plugins/colorscheme-installer.nvim with args { "--color", "never", "--output-format", "json", "--check", "/Users/aome/dev/nvim_plugins/colorscheme-installer.nvim/scripts/docgen.lua" }
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/rpc.lua:131: received LSP notification for method textDocument/didOpen
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_OPEN
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:329: spawning command "selene" at /Users/aome/dev/nvim_plugins/colorscheme-installer.nvim with args { "--display-style", "quiet", "-" }
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:329: spawning command "stylua" at /Users/aome/dev/nvim_plugins/colorscheme-installer.nvim with args { "--color", "never", "--output-format", "json", "--check", "/Users/aome/dev/nvim_plugins/colorscheme-installer.nvim/scripts/docgen.lua" }
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/rpc.lua:131: received LSP notification for method textDocument/didOpen
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/generators.lua:21: running generators for method NULL_LS_DIAGNOSTICS_ON_OPEN
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:329: spawning command "selene" at /Users/aome/dev/nvim_plugins/colorscheme-installer.nvim with args { "--display-style", "quiet", "-" }
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:329: spawning command "stylua" at /Users/aome/dev/nvim_plugins/colorscheme-installer.nvim with args { "--color", "never", "--output-format", "json", "--check", "/Users/aome/dev/nvim_plugins/colorscheme-installer.nvim/scripts/docgen.lua" }
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:207: error output: nil
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:208: output: Results:
0 errors
0 warnings
0 parse errors

[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:207: error output: nil
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:208: output: Results:
0 errors
0 warnings
0 parse errors

[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:181: received diagnostics from source 14
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:182: {}
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:185: buffer changed; ignoring received diagnostics
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:181: received diagnostics from source 14
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:182: {}
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:207: error output: nil
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:208: output: Results:
0 errors
0 warnings
0 parse errors

[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:181: received diagnostics from source 14
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:182: {}
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:207: error output: {"file":"/Users/aome/dev/nvim_plugins/colorscheme-installer.nvim/scripts/docgen.lua","mismatches":[{"expected":"    txt = \"doc/colorscheme_configurations.txt\",\n","expected_end_line":259,"expected_start_line":259,"original":"    txt = \"doc/colorscheme_configurations.txt\"\n","original_end_line":259,"original_start_line":259}]}

[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:208: output: nil
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:181: received diagnostics from source 15
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:182: {}
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:207: error output: {"file":"/Users/aome/dev/nvim_plugins/colorscheme-installer.nvim/scripts/docgen.lua","mismatches":[{"expected":"    txt = \"doc/colorscheme_configurations.txt\",\n","expected_end_line":259,"expected_start_line":259,"original":"    txt = \"doc/colorscheme_configurations.txt\"\n","original_end_line":259,"original_start_line":259}]}

[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:208: output: nil
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:181: received diagnostics from source 15
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:182: {}
[DEBUG Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:185: buffer changed; ignoring received diagnostics
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:207: error output: {"file":"/Users/aome/dev/nvim_plugins/colorscheme-installer.nvim/scripts/docgen.lua","mismatches":[{"expected":"    txt = \"doc/colorscheme_configurations.txt\",\n","expected_end_line":259,"expected_start_line":259,"original":"    txt = \"doc/colorscheme_configurations.txt\"\n","original_end_line":259,"original_start_line":259}]}

[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/helpers/generator_factory.lua:208: output: nil
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:181: received diagnostics from source 15
[TRACE Thu May  9 16:02:38 2024] /Users/aome/dev/nvim_plugins/none-ls.nvim/lua/null-ls/diagnostics.lua:182: {}

AlejandroSuero avatar May 09 '24 12:05 AlejandroSuero

I would like to keep formatters as format-only in this repository. If you really need this, please submit this to none-ls-extras.

mochaaP avatar May 09 '24 12:05 mochaaP

I would like to keep formatters as format-only in this repository. If you really need this, please submit this to none-ls-extras.

@mochaaP I get what you are saying.

What do you think is missing to be able to show the diagnostic on the buffer? I get the error output with the output formatted as JSON.

AlejandroSuero avatar May 09 '24 14:05 AlejandroSuero