nvimdev / guard-collection

collection the tools config for guard
MIT License
31 stars 9 forks source link

feat: clippy-driver (rust) #39

Closed LennyLizowzskiy closed 4 months ago

LennyLizowzskiy commented 6 months ago

Checklist (adding a new tool):

Need help with testing this

At first I forgot to change branches in my Neovim config, but now I fixed it and checked everything but there's no output from the plugin.

Example input:

use std::sync::Arc; // warn

fn main() {
    let x = 5; // warn

    100u8 << 10; // deny
}

Config:

local ft = require("guard.filetype")

ft("rust")
  :lint("clippy-driver")

require("guard").setup({
  fmt_on_save = false,
})
LennyLizowzskiy commented 6 months ago

TODO: In the final version --edition=2021 should be removed. It's here now only for tests

LennyLizowzskiy commented 6 months ago

@xiaoshihou514 can you help, please?

xiaoshihou514 commented 6 months ago

@xiaoshihou514 can you help, please?

Sure, what's the problem?

LennyLizowzskiy commented 6 months ago

@xiaoshihou514 can you help, please?

Sure, what's the problem?

As I previously mentioned, I don't know how to check if it's working. I attached my config to the first message

xiaoshihou514 commented 6 months ago

Hmm, unfourtunately I don't have a pc right now, here's what I would suggest :)

LennyLizowzskiy commented 6 months ago

I checked and yes, linter is outputting stuff to stderr. Is there any way I can get this string in guard?

xiaoshihou514 commented 6 months ago

Unfourtunately the stderr is not passed to the parse function right now. @glepnir seems to be busy contributing to neovim core :( Maybe check clippy and see if there's a cmdline option for that?

LennyLizowzskiy commented 6 months ago

Unfortunately, clippy-driver's API is very minimal because it's meant to be used as cargo clippy that checks all files in the project altogether (while clippy-driver is able to check a single file; also, clippy implementation in rust-analyzer doesn't support checking unsaved buffers for that exact reason). There's no way I can manually forward the output to the stdout

xiaoshihou514 commented 6 months ago

I see, in that case it's not possible to leverage clippy-driver with guard. Feel free to make prs to guard.nvim itself though! Some of the vim.uv code was tricky but you may want to give it a try.

LennyLizowzskiy commented 4 months ago

I'm not using Neovim anymore, so I won't be able to complete this PR.

I'm posting this formatter as plain text in case if I delete the PR branch (I'm not really sure if the changes I made will be saved otherwise in this case).

local lint = require('guard.lint')

local function trim(s)
  return s:gsub('^%s*(.-)%s*$', '%1')
end

return {
  cmd = 'clippy-driver',
  args = { '-', '--error-format=json', '--edition=2021' },
  stdin = true,
  parse = lint.from_json({
    get_diagnostics = function(result, bufnr)
      if result == '' then
        return {}
      end
      local res = '{' .. result:gsub('({[^\n]+})\n', '%1,\n') .. '}'
      return vim.json.decode(res)
    end,
    attributes = {
      lnum = 'spans.line_start',
      end_lnum = 'spans.line_end',
      code = function(json)
        -- concat all 'spans.text[n].text'
        local str = ''
        for k, v in pairs(json.spans.text) do
          str = str .. trim(v.text)
          if k ~= #json.spans.text then -- add " | " if current iteration is not last
            str = str .. ' | '
          end
        end
        return str
      end,
      col = 'spans.column_start',
      end_col = 'spans.column_end',
      severity = 'level',
      message = 'message',
    },
    severities = {
      lint.severities.info,
      lint.severities.warning,
      lint.severities.error,
    },
  }),
}