|
| 1 | +local config = require('bufignore.config') |
| 2 | + |
| 3 | +--- @class FileProcessor |
| 4 | +local M = {} |
| 5 | + |
| 6 | +--- Checks if a buffer meets the requirements for unlisting. |
| 7 | +--- @param bufnr number The buffer number. |
| 8 | +--- @param file_path string The path of the file associated with the buffer. |
| 9 | +--- @return boolean `true` if the buffer meets the unlisting requirements, otherwise `false`. |
| 10 | +M._is_valid_for_unlisting = function(bufnr, file_path) |
| 11 | + local user_config = config.get_user_config() |
| 12 | + |
| 13 | + -- Buffer is not visible |
| 14 | + return vim.fn.bufwinid(bufnr) == -1 |
| 15 | + -- Buffer is valid |
| 16 | + and vim.api.nvim_buf_is_valid(bufnr) |
| 17 | + -- Buffer is loaded |
| 18 | + and vim.api.nvim_buf_is_loaded(bufnr) |
| 19 | + -- Buffer is listed |
| 20 | + and vim.api.nvim_buf_get_option(bufnr, 'buflisted') |
| 21 | + -- User-defined callback is either falsy or returns true. |
| 22 | + and ( |
| 23 | + not user_config.callback |
| 24 | + or user_config.callback({ bufnr = bufnr, file_path = file_path }) |
| 25 | + ) |
| 26 | +end |
| 27 | + |
| 28 | +--- Unlists a buffer for an ignored file. |
| 29 | +--- @param file_path string The path of the ignored file. |
| 30 | +--- @private |
| 31 | +M._unlist_ignored_file = function(file_path) |
| 32 | + -- Schedules main event-loop invocation to preventing textlock issues. |
| 33 | + vim.schedule(function() |
| 34 | + ---@diagnostic disable-next-line: param-type-mismatch |
| 35 | + local bufnr = vim.fn.bufnr(file_path) |
| 36 | + |
| 37 | + if M._is_valid_for_unlisting(bufnr, file_path) then |
| 38 | + -- Unlist git ignored file buffer. |
| 39 | + vim.api.nvim_buf_set_option(bufnr, 'buflisted', false) |
| 40 | + end |
| 41 | + end) |
| 42 | +end |
| 43 | + |
| 44 | +--- Unlists buffers for ignored files. |
| 45 | +---@param file_paths string[] The paths of the ignored files. |
| 46 | +M.unlist_ignored_files = function(file_paths) |
| 47 | + for _, file_path in ipairs(file_paths) do |
| 48 | + M._unlist_ignored_file(file_path) |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | +return M |
0 commit comments