Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: send a subset of files to the quickfixlist #522

Open
1 task done
Janshagen opened this issue Nov 28, 2024 · 6 comments
Open
1 task done

feature request: send a subset of files to the quickfixlist #522

Janshagen opened this issue Nov 28, 2024 · 6 comments
Labels
enhancement New feature or request P2 Not a priority. PRs welcome

Comments

@Janshagen
Copy link

Did you check existing requests?

  • I have searched the existing issues

Describe the feature

The ability to send all files in a directory to the quickfixlist was recently added (#223). An obvious extension would be to only send some of the selected files, either only the visually selected files, or something more clever

Provide background

Example: You would like to do some quickfix-edit to some files in a directory, but there are also a lot of files that you do not want to edit. I want a good way to add only some of the files

What is the significance of this feature?

nice to have

Additional details

I have adapted the code mentioned above to work for visually selected lines (without breaking the prior behavior):

local function selected_oil_files_to_quickfix()
	if vim.bo.filetype ~= "oil" then
		return
	end

	local start_line = 1
	local end_line = vim.fn.line("$")

	local mode = vim.fn.mode()
	if mode == "v" or mode == "V" or mode == "\22" then
		start_line = vim.fn.line("v")
		end_line = vim.fn.line(".")

		if start_line > end_line then
			start_line, end_line = end_line, start_line
		end
	end

	local oil = require("oil")
	local dir = oil.get_current_dir()

	local entries = {}
	for line_number = start_line, end_line do
		local entry = oil.get_entry_on_line(0, line_number)
		if entry and entry.type == "file" then
			table.insert(entries, { filename = dir .. entry.name })
		end
	end

	if #entries == 0 then
		return
	end

	vim.fn.setqflist(entries)
	return vim.cmd.copen()
end
@Janshagen Janshagen added the enhancement New feature or request label Nov 28, 2024
@Janshagen
Copy link
Author

Here is another version (that breaks the prior behavior) which adds the files that match the latest search pattern:

local function searched_oil_files_to_quickfix()
	if vim.bo.filetype ~= "oil" then
		return
	end

	local pattern = vim.fn.getreg("/")
	if pattern == "" then
		return
	end

	local oil = require("oil")
	local dir = oil.get_current_dir()

	local entries = {}
	for line_number = 1, vim.fn.line("$") do
		local entry = oil.get_entry_on_line(0, line_number)
		if entry and entry.name:find(pattern) and entry.type == "file" then
			table.insert(entries, { filename = dir .. entry.name })
		end
	end

	if #entries == 0 then
		return
	end

	vim.fn.setqflist(entries)
	return vim.cmd.copen()
end

@Janshagen
Copy link
Author

Sorry for spamming. But I figured out a way to keep the original behavior and combining my two suggestions.

The following code adds all files that are 1. matched if highlighting of matched items is active else all files and 2. are visually selected if you are in any visual mode else all files in directory

local function selected_oil_files_to_quickfix()
	if vim.bo.filetype ~= "oil" then
		return
	end

	local oil = require("oil")
	local dir = oil.get_current_dir()

	local start_line = 1
	local end_line = vim.fn.line("$")

	local mode = vim.fn.mode()
	if mode == "v" or mode == "V" or mode == "\22" then
		start_line = vim.fn.line("v")
		end_line = vim.fn.line(".")

		if start_line > end_line then
			start_line, end_line = end_line, start_line
		end
	end

	local pattern = vim.fn.getreg("/")
	local entries = {}
	for line_number = start_line, end_line do
		local entry = oil.get_entry_on_line(0, line_number)
		if
			entry
			and entry.type == "file"
			and (vim.v.hlsearch == 0 or (vim.v.hlsearch == 1 and entry.name:find(pattern)))
		then
			table.insert(entries, { filename = dir .. entry.name })
		end
	end

	if #entries == 0 then
		return
	end

	vim.fn.setqflist(entries)
	return vim.cmd.copen()
end

@stevearc
Copy link
Owner

stevearc commented Dec 3, 2024

I would review a PR along these lines. I think it makes sense to transparently take just the selected rows when in visual mode, but I don't think it's intuitive to take search matches when search highlighting is active. For that, I'd want to either make a new action or add an option to this action that is off by default.

@stevearc stevearc added the P2 Not a priority. PRs welcome label Dec 3, 2024
@Janshagen
Copy link
Author

Janshagen commented Dec 3, 2024 via email

@stevearc
Copy link
Owner

stevearc commented Dec 3, 2024

To be clear, I think that is a fine feature I just think it is an unintuitive default

@Janshagen
Copy link
Author

I was going through the code and noticed that adding only the highlighted files is already an option through "actions.send_to_qflist". However, that action is not very easy to find in the documentation. Also, a detail is that my implementation automatically opens the quickfix list which I think is nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request P2 Not a priority. PRs welcome
Projects
None yet
Development

No branches or pull requests

2 participants