davidosomething / format-ts-errors.nvim

Neovim plugin to format tsserver LSP errors
MIT License
74 stars 1 forks source link

feat: Markdown / custom processing support #6

Closed Picalines closed 6 days ago

Picalines commented 1 week ago

Hi, great plugin!

It'd be nice if this plugin could wrap TS types in a markdown symbols, like:

Type

```ts
  {
    field: number
  }

Is not assignable to number


So other plugins like `noice.nvim` could highlight the diagnostic popup using treesitter for example

I think there're two ways to implement it:
1. Direct option like `format = 'markdown'`, while the current behaviour is `format = 'plain'` or something
2. Two generic functions for formatting blocks and inlines, line:
```lua
format = {
  inline = function (text) return '`' .. text .. '`',
  block = function (text)
    return '```ts\n' .. text .. '\n```'
  return
}

Second approach is more difficult, because we'd need to choose what data to pass in function parameters (is text already prettified? How to handle nested indentation?)

davidosomething commented 1 week ago

updated with:

{
    "davidosomething/format-ts-errors.nvim",
    config = function()
      require("format-ts-errors").setup({
        add_markdown = true, -- wrap output with markdown ```ts ``` markers
        start_indent_level = 0, -- initial indent
      })
    end,
}

lmk if that works

Picalines commented 1 week ago

It seems like the TS blocks are not indented now

image

Not entirely sure what's happening, i'll share a better screenshot when i found one

davidosomething commented 1 week ago

if you could share the file as well it would help

Picalines commented 1 week ago

Here's a short example:

type Obj1 = { field: number }
type Obj2 = { field: string }
const x: Obj1 = {field: 123}
const y: Obj2 = x

Current output is:

Type

```ts
Obj1

is not assignable to type

Obj2

Types of property 'field' are incompatible. Type

number

is not assignable to type

string

(2322)



The ` (2322)` is part of [neovim configuration](https://neovim.io/doc/user/diagnostic.html#vim.diagnostic.Opts.Float), it can be fixed on the user side
davidosomething commented 1 week ago

are you using start_indent_level = 0, -- initial indent ? The default before this change was 1

Picalines commented 1 week ago

are you using start_indent_level = 0, -- initial indent ? The default before this change was 1

Yes, i'm using start_indent_level = 0

davidosomething commented 1 week ago

remove that line or change it to start_indent_level = 1 to bring back initial indent

Picalines commented 1 week ago

remove that line or change it to start_indent_level = 1 to bring back initial indent

It produces the same result for the example i posted above

davidosomething commented 6 days ago

i am unable to replicate, can you post a minimal config? here's what i see with start_indent_level = 1

image

Picalines commented 6 days ago

Sorry, it turn's out that render-markdown plugin removes the indentation in code blocks

That's my output without the plugin:

Type

```ts
  Obj1

is not assignable to type

  Obj2

Types of property 'field' are incompatible. Type

  number

is not assignable to type

  string

@davidosomething
Is the `Types of property 'field' are incompatible` indented correctly? You can close the issue if that's intended

---

If anyone stumbles across this, you can turn off render-markdown's `indent` feature to keep the highlighting

Here's how i've configured neovim to render "Diagnostics" as markdown:
```lua
local builtin_open_float = vim.diagnostic.open_float

---@diagnostic disable-next-line: duplicate-set-field
vim.diagnostic.open_float = function(...)
    local bufnr, winid = builtin_open_float(...)

    if bufnr and winid then
        vim.api.nvim_set_option_value('filetype', 'markdown', { buf = bufnr })
    end

    return bufnr, winid
end

And here's how to tell neovim not to apply DiagnosticError highlight to the whole popup:

vim.diagnostic.config {
    signs = {
        -- text & numhl tables
    },

    float = {
        -- NOTE: format can't return the highlight, so we use prefix & suffix without it
        format = function() return '' end,

        prefix = function(report)
            local signs = vim.diagnostic.config().signs or {}
            local icon = signs.text[report.severity]
            return icon .. ' ', signs.numhl[report.severity]
        end,

        suffix = function(report)
            local hl = 'Normal'
            local message = vim.trim(report.user_data.lsp.message)

            if string.find(message, '\n') then
                return string.format('%s\n^ (%s)', message, report.code), hl
            end

            return string.format('%s (%s)', message, report.code), hl
        end,
    },
}
davidosomething commented 6 days ago

Is the Types of property 'field' are incompatible indented correctly? You can close the issue if that's intended

Yea, it comes newline+indented from typescript server.

It's possible to change the output of that as well using some regex to strip the extra indent, but the messaging changes between typescript versions so I'll leave it as an exercise to the user ;P

Thanks for using and following up on the issue!