Skip to content

Commit

Permalink
feat: new action open_cmdline_dir (stevearc#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Jan 22, 2023
1 parent 7649866 commit 6c4a3da
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
3 changes: 3 additions & 0 deletions doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ close *actions.clos
open_cmdline *actions.open_cmdline*
Open vim cmdline with current entry as an argument

open_cmdline_dir *actions.open_cmdline_dir*
Open vim cmdline with current directory as an argument

open_cwd *actions.open_cwd*
Open oil in Neovim's current working directory

Expand Down
1 change: 1 addition & 0 deletions doc/tags
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Oil oil.txt /*Oil*
actions.cd oil.txt /*actions.cd*
actions.close oil.txt /*actions.close*
actions.open_cmdline oil.txt /*actions.open_cmdline*
actions.open_cmdline_dir oil.txt /*actions.open_cmdline_dir*
actions.open_cwd oil.txt /*actions.open_cwd*
actions.open_terminal oil.txt /*actions.open_terminal*
actions.parent oil.txt /*actions.parent*
Expand Down
32 changes: 25 additions & 7 deletions lua/oil/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ M.refresh = {
end,
}

local function open_cmdline_with_args(args)
local escaped = vim.api.nvim_replace_termcodes(
": " .. args .. string.rep("<Left>", args:len() + 1),
true,
false,
true
)
vim.api.nvim_feedkeys(escaped, "n", true)
end

M.open_cmdline = {
desc = "Open vim cmdline with current entry as an argument",
callback = function()
Expand All @@ -171,18 +181,26 @@ M.open_cmdline = {
end
local bufname = vim.api.nvim_buf_get_name(0)
local scheme, path = util.parse_url(bufname)
if not scheme then
return
end
local adapter = config.get_adapter_by_scheme(scheme)
if not adapter or not path or adapter.name ~= "files" then
return
end
local fullpath = fs.shorten_path(fs.posix_to_os_path(path) .. entry.name)
local escaped = vim.api.nvim_replace_termcodes(
": " .. fullpath .. string.rep("<Left>", fullpath:len() + 1),
true,
false,
true
)
vim.api.nvim_feedkeys(escaped, "n", true)
open_cmdline_with_args(fullpath)
end,
}

M.open_cmdline_dir = {
desc = "Open vim cmdline with current directory as an argument",
callback = function()
local fs = require("oil.fs")
local dir = oil.get_current_dir()
if dir then
open_cmdline_with_args(fs.shorten_path(dir))
end
end,
}

Expand Down

0 comments on commit 6c4a3da

Please sign in to comment.