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: on file created hook #527

Closed
1 task done
Skyppex opened this issue Dec 3, 2024 · 2 comments
Closed
1 task done

feature request: on file created hook #527

Skyppex opened this issue Dec 3, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@Skyppex
Copy link

Skyppex commented Dec 3, 2024

Did you check existing requests?

  • I have searched the existing issues

Describe the feature

When i create new files with oil, i want the file to be filled with some basic content. A hook which takes the file path would be perfect. I can handle writing to the file in the hook. This makes it a bit more versatile as you're not locked into writing content to the file, it can be used for anything else where you might need the file path or just the filetype.

Provide background

When creating .cs files i (almost) always want it to contain a line with the namespace that c# code in the file should exist within. In the most popular IDEs this just happens by itself, but since nvim is a text editor, i have to manually write it in. I just want to avoid that because its boilerplate and i don't really care about it, but it has to be done. Adding a hook which is invoked after a file is created will allow me to do this with a little configuration on my part.

I tried using an autocmd with "BufNewFile", but that falls short because its only when

What is the significance of this feature?

strongly desired

Additional details

I would say this is strongly desired for me, but i'm betting its a nice to have for most.

i imagine the configuration like this:

{
    file_created_hooks = {
        lua = function(path) ... end,
        cs = function(path) ... end,
    }
}

the key (lua, cs) is the neovim filetype and the path is the full path to the file

@Skyppex Skyppex added the enhancement New feature or request label Dec 3, 2024
@stevearc
Copy link
Owner

stevearc commented Dec 3, 2024

There is a User autocmd that is dispatched after performing file mutations

vim.api.nvim_exec_autocmds(
"User",
{ pattern = "OilActionsPost", modeline = false, data = { err = err, actions = actions } }
)

The type definition for actions is above

---@alias oil.Action oil.CreateAction|oil.DeleteAction|oil.MoveAction|oil.CopyAction|oil.ChangeAction
---@class (exact) oil.CreateAction
---@field type "create"
---@field url string
---@field entry_type oil.EntryType
---@field link nil|string
---@class (exact) oil.DeleteAction
---@field type "delete"
---@field url string
---@field entry_type oil.EntryType
---@class (exact) oil.MoveAction
---@field type "move"
---@field entry_type oil.EntryType
---@field src_url string
---@field dest_url string
---@class (exact) oil.CopyAction
---@field type "copy"
---@field entry_type oil.EntryType
---@field src_url string
---@field dest_url string
---@class (exact) oil.ChangeAction
---@field type "change"
---@field entry_type oil.EntryType
---@field url string
---@field column string
---@field value any

You'll need to process the urls to get the actual filename, but it should be relatively straightforward

@stevearc stevearc closed this as completed Dec 3, 2024
@Skyppex
Copy link
Author

Skyppex commented Dec 4, 2024

There is a User autocmd that is dispatched after performing file mutations

vim.api.nvim_exec_autocmds(
"User",
{ pattern = "OilActionsPost", modeline = false, data = { err = err, actions = actions } }
)

The type definition for actions is above

---@alias oil.Action oil.CreateAction|oil.DeleteAction|oil.MoveAction|oil.CopyAction|oil.ChangeAction
---@class (exact) oil.CreateAction
---@field type "create"
---@field url string
---@field entry_type oil.EntryType
---@field link nil|string
---@class (exact) oil.DeleteAction
---@field type "delete"
---@field url string
---@field entry_type oil.EntryType
---@class (exact) oil.MoveAction
---@field type "move"
---@field entry_type oil.EntryType
---@field src_url string
---@field dest_url string
---@class (exact) oil.CopyAction
---@field type "copy"
---@field entry_type oil.EntryType
---@field src_url string
---@field dest_url string
---@class (exact) oil.ChangeAction
---@field type "change"
---@field entry_type oil.EntryType
---@field url string
---@field column string
---@field value any

You'll need to process the urls to get the actual filename, but it should be relatively straightforward

:o huge news, ty :)

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

No branches or pull requests

2 participants