s1n7ax/nvim-window-picker

Treesitter-context gets picked up on

svampkorg opened this issue · 5 comments

When using treesitter-context, the context part gets picked up as possible windows to switch to. Attempting to switch to any of those windows indeed shows there is an ID (thought it changes every time for some reason)

Picking another window from one without any visible treesitter-context:
no-treesitter-context

Illustration of what I mean with treesitter-context:
window-with-treesitter-context

Picking another window from a window that had visible treesitter-context:
trying-to-pick-from-window-with-treesitter-context

I'm not sure how to filter out those picked up windows :) I've tried with disabling treesitter context, and those rogue window does not show up, so I tried incorporating this in my window picker function, which disables treesitter-context before running window-picker. It looks something like this:

function()
vim.cmd("TSContextDisable")
local picked_window_id = require('window-picker').pick_window()
if vim.fn.win_gettype(picked_window_id) == "unknown" then
goto enablecontext
else
vim.api.nvim_set_current_win(picked_window_id)
goto enablecontext
end
::enablecontext::
vim.cmd("TSContextEnable")
end

But this does not work, which is strange because manually disabling treesitter-context before picking a window works.
I think I need some help :)

for anyone with a similar problem. I kind of solved it after looking a bit at treesitter-context code, by setting this filter rule in window-picker config:
wo = {
winhl = { "NormalFloat:TreesitterContext", "NormalFloat:TreesitterContextLineNumber" },
},
The picker function remains tho, altho I only check if window type is unknown. I tried to use the filter_func to filter out unknown, but it does not seem to work:
filter_func = function(window_ids, filters)
local include_windows = {}
for id in window_ids do
if vim.fn.win_gettype(id) == "unknown"
then
goto continue
else
table.insert(include_windows, id)
end
::continue::
end
return include_windows
end,

Anyway, thank you for making this plugin. It's very useful :)

9ary commented

For what it's worth, you were probably running into #66, which is why it wouldn't work.

Here's a filter that works, based on the observation that ts-context makes its windows unfocusable:

filter_func = function(windows, rules)
	local function predicate(wid)
		cfg = vim.api.nvim_win_get_config(wid)
		if not cfg.focusable then
			return false
		end
		return true
	end
	local filtered = vim.tbl_filter(predicate, windows)

	local dfilter = require("window-picker.filters.default-window-filter"):new()
	dfilter:set_config(rules)
	return dfilter:filter_windows(filtered)
end,

This probably ought to be included in the default filter, I'll open a PR for it.