folke / trouble.nvim

🚦 A pretty diagnostics, references, telescope results, quickfix and location list to help you solve all the trouble your code is causing.
Apache License 2.0
5.11k stars 173 forks source link

change the format in sources/diagnostics.lua #508

Closed xz47sv closed 2 weeks ago

xz47sv commented 2 weeks ago

Did you check the docs?

Is your feature request related to a problem? Please describe.

The current format at https://github.com/folke/trouble.nvim/blob/e0b35a273df58b03612255783cf475a782ede7e0/lua/trouble/sources/diagnostics.lua#L29 contains parentheses around the code so if a user wants to register a custom formatter for the code the parentheses will stick around.

Describe the solution you'd like

Remove the parentheses and change the format to

format = "{severity_icon} {message:md} {item.source} {code} {pos}",

add

  code = function(ctx)
    return {
      text = ctx.item.code and "(" .. ctx.item.code .. ")" or "",
    }
  end,

to https://github.com/folke/trouble.nvim/blob/e0b35a273df58b03612255783cf475a782ede7e0/lua/trouble/format.lua#L46

Additional context

This could probably be done for other formats that contain braces or any other special characters for the sake of more customizability.

(or optionally just make the format itself directly customizable)

folke commented 2 weeks ago

Yiou can just override the format too in your config.

opts.modes.diagnostics.format = ...

xz47sv commented 2 weeks ago

Ok, but the current default leaves an empty pair of parentheses "()" if the diagnostic doesn't contain a code, wouldn't it be objectively better to not do that by default and return ""?

Plus {pos} also adds the brackets in the formatter and not in the format string, why not do it for each of them so the user can override just the specific formatter instead of the entire string.

b0ae989c commented 2 weeks ago

User defined formatter is already implemented in trouble, but it is not yet documented fully. In your example, we can do the following

-- Lazy config
{
  'folke/trouble.nvim',
  opts = {
    modes = {
      my_diagnostics = {
        mode = 'diagnostics',
        format = '{my_pos} {my_code}',
      },
    },
    formatters = {
        my_pos = function(ctx)
          return { text = ctx.item.pos[1] .. ':' .. (ctx.item.pos[2] + 1) },
        end,
        my_code = function(ctx)
          return { text = ctx.item.code and '(' .. ctx.item.code .. ')' or '' },
        end,
    },
  },
}
folke commented 2 weeks ago

I've updated the default format for code. Better indeed