Skip to content

Commit

Permalink
feat: added use_diagnostics option to populate diagnostics list (#50)
Browse files Browse the repository at this point in the history
* feat: added use_diagnostics option to populate diagnostics list as well as quickfix list

* fix: edited diagnostics for matching infos from error
  • Loading branch information
Grolaf authored May 14, 2024
1 parent c37d7b3 commit 0673476
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ By default, the plugin uses the default `tsc` command with the `--noEmit` flag t
auto_focus_qflist = false,
auto_start_watch_mode = false,
use_trouble_qflist = false,
use_diagnostics = false,
run_as_monorepo = false,
bin_path = utils.find_tsc_bin(),
enable_progress_notifications = true,
Expand Down Expand Up @@ -162,6 +163,14 @@ require('tsc').setup({

This will use Trouble for the quickfix list. This will work with all other options such as `auto_open_qflist`, `auto_close_qflist`, `auto_focus_qflist`.

### Can it show the error list directly in my file explorer (like nvim-tree)

Yes. As file explorers uses LSP diagnostics, you can use the `use_diagnostics` option in order to populate the diagnostics list as well as the quickfix list

```
use_diagnostics = true
```

## Contributing

Feel free to open issues or submit pull requests if you encounter any bugs or have suggestions for improvements. Your contributions are welcome!
Expand Down
24 changes: 24 additions & 0 deletions lua/tsc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ end
--- @field auto_focus_qflist boolean - (false) When true the quick fix list will automatically focus when errors are found
--- @field auto_start_watch_mode boolean - (false) When true the `tsc` process will be started in watch mode when a typescript buffer is opened
--- @field use_trouble_qflist boolean - (false) When true the quick fix list will be opened in Trouble if it is installed
--- @field use_diagnostics boolean - (false) When true the errors will be set as diagnostics
--- @field run_as_monorepo boolean - (false) When true the `tsc` process will be started mode for each tsconfig in the current working directory
--- @field bin_path string - Path to the tsc binary if it is not in the projects node_modules or globally
--- @field enable_progress_notifications boolean - (true) When false progress notifications will not be shown
Expand All @@ -29,6 +30,7 @@ local DEFAULT_CONFIG = {
auto_focus_qflist = false,
auto_start_watch_mode = false,
use_trouble_qflist = false,
use_diagnostics = false,
bin_path = utils.find_tsc_bin(),
enable_progress_notifications = true,
run_as_monorepo = false,
Expand Down Expand Up @@ -177,6 +179,28 @@ M.run = function()
use_trouble = config.use_trouble_qflist,
})

if config.use_diagnostics then
local namespace_id = vim.api.nvim_create_namespace("tsc_diagnostics")
vim.diagnostic.reset(namespace_id)

for _, error in ipairs(errors) do
local bufnr = vim.fn.bufnr(error.filename)
if bufnr == -1 then
vim.notify("Buffer not found for " .. error.filename, vim.log.levels.ERROR, get_notify_options())
return
end
local diagnostic = {
bufnr = bufnr,
lnum = error.lnum - 1,
col = error.col - 1,
severity = vim.diagnostic.severity.ERROR,
message = error.text,
source = "tsc",
}
vim.diagnostic.set(namespace_id, bufnr, { diagnostic }, {})
end
end

if not config.enable_progress_notifications then
return
end
Expand Down

0 comments on commit 0673476

Please sign in to comment.