Skip to content

Commit

Permalink
feat: API to change config.view.is_hidden_file at runtime (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Mar 12, 2023
1 parent 33d5701 commit 12bea0f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` \
Expand Down
11 changes: 11 additions & 0 deletions doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions doc/tags
Original file line number Diff line number Diff line change
Expand Up @@ -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*
11 changes: 11 additions & 0 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 14 additions & 6 deletions lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 12bea0f

Please sign in to comment.