this post was submitted on 10 Jan 2024
8 points (100.0% liked)

Neovim

2186 readers
1 users here now

founded 1 year ago
MODERATORS
 

Hi

As the title suggests, I want to set something up so that while trouble is_open(), I have a keyboard shortcut that cycles through the various modes listed at https://github.com/folke/trouble.nvim/tree/main#commands.

I see how I can assign shortcuts to jump directly to the different modes, but I want to remember only one shortcut.

Writing a bit of lua to do this is easy enough, but I can't find any way to see what mode it's currently in, in order to write some function that toggles to the next one.

Is there any way to accomplish this?

Bonus question: Is there any way to get trouble to display on screen what mode it's currently in? Such as at the top or bottom of the trouble panel.

top 6 comments
sorted by: hot top controversial new old
[–] [email protected] 2 points 10 months ago (2 children)

I don’t know any specifics about trouble, but would it be good enough to just track the current mode in your own global option/variable whenever you toggle it with your shortcut?

If not I‘d start by taking a look at the trouble source code.

[–] [email protected] 2 points 10 months ago* (last edited 10 months ago)

For the sake of sharing... I took your idea and found this worked just fine:

TroubleMode = "workspace_diagnostics"

local function cycle_trouble_mode()
	local trouble = require("trouble")

	local modes = {
		"document_diagnostics",
		"workspace_diagnostics",
		"lsp_references",
		"lsp_definitions",
		"lsp_type_definitions",
		"quickfix",
		"loclist",
	}

	if trouble.is_open() then
		local function get_next_mode(mode)
			local next_index = 1
			for i = 1, #modes do
				if modes[i] == mode then
					next_index = i == #modes and 1 or i + 1
				end
			end
			return modes[next_index]
		end

		TroubleMode = get_next_mode(TroubleMode)
		trouble.toggle(TroubleMode)
	end
end
[–] [email protected] 1 points 10 months ago

I did end up looking at the code a bit and couldn't find what I wanted. But what you suggest is a good idea. I may give that a try.

[–] [email protected] 2 points 10 months ago
[–] autokludge 2 points 10 months ago* (last edited 10 months ago) (1 children)

~~Are you trying to recreate the default action keys?~~ Nope, that just bounces between workspace and document

~~Does :lua require("trouble").action("toggle_mode") do anything for you?~~ is just doing the same thing as above.

[–] [email protected] 1 points 10 months ago

I tried after writing the function I posted below. This works to flip between workspace and document mode but I can't get to all of the modes like this. Thank you though.