Skip to content

Commit

Permalink
add trouble integration option, expose open and close usercmds (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: ben feldberg collins <[email protected]>
  • Loading branch information
benfc1993 and BenFCSains authored Mar 21, 2024
1 parent ec4e6c2 commit 02856f0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 6 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ By default, the plugin uses the default `tsc` command with the `--noEmit` flag t
auto_close_qflist = false,
auto_focus_qflist = false,
auto_start_watch_mode = false,
use_trouble_qflist = false,
bin_path = utils.find_tsc_bin(),
enable_progress_notifications = true,
flags = {
Expand All @@ -99,6 +100,26 @@ With this configuration, you can use keys for flag names and their corresponding
flags = "--noEmit",
```

## Manual Opening and Closing the Quickfix List

There are two user commands you can use to open and close the quickfix list:

`TSCOpen` - open the quickfix list
`TSCClose` - close the quickfix list

These commands will respect your configuration options:

- `auto_open_qflist`
- `auto_close_qflist`
- `use_trouble_qflist`

### Example key maps:

```lua
vim.keymap.set('n', '<leader>to', ':TSCOpen<CR>')
vim.keymap.set('n', '<leader>tc', ':TSCClose<CR>')
```

## FAQs

### I'm using `nvim-notify` and being spammed by progress notifications, what's going on?
Expand Down Expand Up @@ -128,6 +149,18 @@ require('tsc').setup({

With this configuration, tsc.nvim will typecheck all projects in the monorepo, taking into account project references and incremental builds.

### Can I use `Trouble` for the quickfix list?

Yes, as long as you have the plugin installed you can set `use_trouble_qflist = true` in the configuration.

```lua
require('tsc').setup({
use_trouble_qflist = true,
})
```

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`.

## 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
10 changes: 10 additions & 0 deletions lua/tsc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local DEFAULT_CONFIG = {
auto_close_qflist = false,
auto_focus_qflist = false,
auto_start_watch_mode = false,
use_trouble_qflist = false,
bin_path = utils.find_tsc_bin(),
enable_progress_notifications = true,
flags = {
Expand Down Expand Up @@ -131,6 +132,7 @@ M.run = function()
auto_open = config.auto_open_qflist,
auto_close = config.auto_close_qflist,
auto_focus = config.auto_focus_qflist,
use_trouble = config.use_trouble_qflist,
})

if not config.enable_progress_notifications then
Expand Down Expand Up @@ -204,6 +206,14 @@ function M.setup(opts)
M.run()
end, { desc = "Run `tsc` asynchronously and load the results into a qflist", force = true })

vim.api.nvim_create_user_command("TSCOpen", function()
utils.open_qflist(config.use_trouble_qflist, config.auto_focus_qflist)
end, { desc = "Open the results in a qflist", force = true })

vim.api.nvim_create_user_command("TSCClose", function()
utils.close_qflist(config.use_trouble_qflist)
end, { desc = "Close the results qflist", force = true })

if config.flags.watch then
vim.api.nvim_create_autocmd("BufWritePre", {
pattern = "*.{ts,tsx}",
Expand Down
46 changes: 40 additions & 6 deletions lua/tsc/utils.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local better_messages = require("tsc.better-messages")

local has_trouble, pcall_trouble = pcall(require, "trouble")
local trouble = has_trouble and pcall_trouble or nil

local M = {}

M.is_executable = function(cmd)
Expand Down Expand Up @@ -95,20 +98,51 @@ M.parse_tsc_output = function(output, config)
end

M.set_qflist = function(errors, opts)
local DEFAULT_OPTS = { auto_open = true, auto_close = false }
local DEFAULT_OPTS = { auto_open = true, auto_close = false, use_trouble = false }
local final_opts = vim.tbl_extend("force", DEFAULT_OPTS, opts or {})

vim.fn.setqflist({}, "r", { title = "TSC", items = errors })

if #errors > 0 and final_opts.auto_open then
local win = vim.api.nvim_get_current_win()
M.open_qflist(final_opts.use_trouble, final_opts.auto_focus)
end

vim.cmd("copen")
if #errors == 0 then
-- trouble needs to be refreshed when list is empty.
if final_opts.use_trouble and trouble ~= nil then
trouble.refresh()
end

if not final_opts.auto_focus then
vim.api.nvim_set_current_win(win)
if final_opts.auto_close then
M.close_qflist(final_opts.use_trouble)
end
elseif #errors == 0 and final_opts.auto_close then
end
end

--- open the qflist
--- @param use_trouble boolean: if trouble should be used as qflist provider
--- @param auto_focus boolean: if the qflist should be focused on open
--- @return nil
M.open_qflist = function(use_trouble, auto_focus)
local win = vim.api.nvim_get_current_win()
if use_trouble and trouble ~= nil then
trouble.open("quickfix")
else
vim.cmd("copen")
end

if not auto_focus then
vim.api.nvim_set_current_win(win)
end
end

--- close the qflist
--- @param use_trouble boolean: if trouble should be used as qflist provider
--- @return nil
M.close_qflist = function(use_trouble)
if use_trouble and trouble ~= nil then
trouble.close()
else
vim.cmd("cclose")
end
end
Expand Down

0 comments on commit 02856f0

Please sign in to comment.