feat: API to change config.view.is_hidden_file at runtime (#69)

This commit is contained in:
Steven Arcangeli 2023-03-12 12:48:59 -07:00
parent 33d5701a8d
commit 12bea0f646
5 changed files with 53 additions and 6 deletions

View file

@ -234,6 +234,21 @@ Change the display columns for oil
| ----- | ------------------ | ---- |
| cols | `oil.ColumnSpec[]` | |
### set_is_hidden_file(is_hidden_file)
`set_is_hidden_file(is_hidden_file)` \
Change how oil determines if the file is hidden
| Param | Type | Desc |
| -------------- | ----------------------------------------------------- | -------------------------------------------- |
| is_hidden_file | `fun(filename: string, bufnr: nil\|integer): boolean` | Return true if the file/dir should be hidden |
### toggle_hidden()
`toggle_hidden()` \
Toggle hidden files and directories
### get_current_dir()
`get_current_dir(): nil|string` \

View file

@ -110,6 +110,17 @@ set_columns({cols}) *oil.set_column
Parameters:
{cols} `oil.ColumnSpec[]`
set_is_hidden_file({is_hidden_file}) *oil.set_is_hidden_file*
Change how oil determines if the file is hidden
Parameters:
{is_hidden_file} `fun(filename: string, bufnr: nil|integer): boolean` Retu
rn true if the file/dir should be hidden
toggle_hidden() *oil.toggle_hidden*
Toggle hidden files and directories
get_current_dir(): nil|string *oil.get_current_dir*
Get the current directory

View file

@ -53,5 +53,7 @@ oil.open_float oil.txt /*oil.open_float*
oil.save oil.txt /*oil.save*
oil.select oil.txt /*oil.select*
oil.set_columns oil.txt /*oil.set_columns*
oil.set_is_hidden_file oil.txt /*oil.set_is_hidden_file*
oil.setup oil.txt /*oil.setup*
oil.toggle_hidden oil.txt /*oil.toggle_hidden*
oil.txt oil.txt /*oil.txt*

View file

@ -135,6 +135,17 @@ M.set_columns = function(cols)
require("oil.view").set_columns(cols)
end
---Change how oil determines if the file is hidden
---@param is_hidden_file fun(filename: string, bufnr: nil|integer): boolean Return true if the file/dir should be hidden
M.set_is_hidden_file = function(is_hidden_file)
require("oil.view").set_is_hidden_file(is_hidden_file)
end
---Toggle hidden files and directories
M.toggle_hidden = function()
require("oil.view").toggle_hidden()
end
---Get the current directory
---@return nil|string
M.get_current_dir = function()

View file

@ -53,8 +53,7 @@ M.get_last_cursor = function(bufname)
end
local function are_any_modified()
local view = require("oil.view")
local buffers = view.get_all_buffers()
local buffers = M.get_all_buffers()
for _, bufnr in ipairs(buffers) do
if vim.bo[bufnr].modified then
return true
@ -64,25 +63,34 @@ local function are_any_modified()
end
M.toggle_hidden = function()
local view = require("oil.view")
local any_modified = are_any_modified()
if any_modified then
vim.notify("Cannot toggle hidden files when you have unsaved changes", vim.log.levels.WARN)
else
config.view_options.show_hidden = not config.view_options.show_hidden
view.rerender_all_oil_buffers({ refetch = false })
M.rerender_all_oil_buffers({ refetch = false })
end
end
---@param is_hidden_file fun(filename: string, bufnr: nil|integer): boolean
M.set_is_hidden_file = function(is_hidden_file)
local any_modified = are_any_modified()
if any_modified then
vim.notify("Cannot change is_hidden_file when you have unsaved changes", vim.log.levels.WARN)
else
config.view_options.is_hidden_file = is_hidden_file
M.rerender_all_oil_buffers({ refetch = false })
end
end
M.set_columns = function(cols)
local view = require("oil.view")
local any_modified = are_any_modified()
if any_modified then
vim.notify("Cannot change columns when you have unsaved changes", vim.log.levels.WARN)
else
config.columns = cols
-- TODO only refetch if we don't have all the necessary data for the columns
view.rerender_all_oil_buffers({ refetch = true })
M.rerender_all_oil_buffers({ refetch = true })
end
end