Skip to content

davidosomething/format-ts-errors.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

format-ts-errors.nvim

Make ts_ls (the TypeScript LSP) errors a little nicer looking by formatting objects.

This plugin favors composability and direct API access over doing it for you.

screenshots

screenshot 1 screenshot 2

Installation

Lazy.nvim add:

{
    "davidosomething/format-ts-errors.nvim"
}

You can configure the output with a setup function, e.g.:

{
    "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,
}

Then in the lsp setup:

local lspconfig = require("lspconfig")
lspconfig.tsserver.setup({
  handlers = {
    ["textDocument/publishDiagnostics"] = function(
      _,
      result,
      ctx,
      config
    )
      if result.diagnostics == nil then
        return
      end

      -- ignore some tsserver diagnostics
      local idx = 1
      while idx <= #result.diagnostics do
        local entry = result.diagnostics[idx]

        local formatter = require('format-ts-errors')[entry.code]
        entry.message = formatter and formatter(entry.message) or entry.message

        -- codes: https://github.com/microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json
        if entry.code == 80001 then
          -- { message = "File is a CommonJS module; it may be converted to an ES module.", }
          table.remove(result.diagnostics, idx)
        else
          idx = idx + 1
        end
      end

      vim.lsp.diagnostic.on_publish_diagnostics(
        _,
        result,
        ctx,
        config
      )
    end,
  },
})

Config options

start_indent_level

start_indent_level = 0

Will yield:

```ts
some code (not indented)
```

Whereas 1:

start_indent_level = 1

Will yield:

```ts
  some code (indented)
```

About

Neovim plugin to format tsserver LSP errors

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages