Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Add stylua diagnostics #9

Open
1 task done
AlejandroSuero opened this issue May 9, 2024 · 2 comments
Open
1 task done

[Feat] Add stylua diagnostics #9

AlejandroSuero opened this issue May 9, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@AlejandroSuero
Copy link
Contributor

AlejandroSuero commented May 9, 2024

Issues

  • 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 AlejandroSuero added the enhancement New feature or request label May 9, 2024
@mochaaP
Copy link
Member

mochaaP commented May 9, 2024

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 mochaaP transferred this issue from nvimtools/none-ls.nvim May 9, 2024
@AlejandroSuero
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants