Skip to content

Commit

Permalink
feat: action to open entry in new tab (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Jan 29, 2023
1 parent 0e53d40 commit 48eec8b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lua/oil/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ M.select_split = {
end,
}

M.select_tab = {
desc = "Open the entry under the cursor in a new tab",
callback = function()
oil.select({ tab = true })
end,
}

M.preview = {
desc = "Open the entry under the cursor in a preview window, or close the preview window if already open",
callback = function()
Expand Down
1 change: 1 addition & 0 deletions lua/oil/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ local default_config = {
["<CR>"] = "actions.select",
["<C-s>"] = "actions.select_vsplit",
["<C-h>"] = "actions.select_split",
["<C-t>"] = "actions.select_tab",
["<C-p>"] = "actions.preview",
["<C-c>"] = "actions.close",
["<C-l>"] = "actions.refresh",
Expand Down
13 changes: 12 additions & 1 deletion lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ end
--- horizontal boolean Open the buffer in a horizontal split
--- split "aboveleft"|"belowright"|"topleft"|"botright" Split modifier
--- preview boolean Open the buffer in a preview window
--- tab boolean Open the buffer in a new tab
M.select = function(opts)
local cache = require("oil.cache")
opts = vim.tbl_extend("keep", opts or {}, {})
Expand All @@ -322,6 +323,9 @@ M.select = function(opts)
if opts.preview and not opts.horizontal and opts.vertical == nil then
opts.vertical = true
end
if opts.tab and (opts.preview or opts.split) then
error("Cannot set preview or split when tab = true")
end
local util = require("oil.util")
if util.is_floating_win() and opts.preview then
vim.notify("oil preview doesn't work in a floating window", vim.log.levels.ERROR)
Expand Down Expand Up @@ -405,7 +409,14 @@ M.select = function(opts)
if vim.tbl_isempty(mods) then
mods = nil
end
local cmd = opts.split and "split" or "edit"
local cmd
if opts.tab then
cmd = "tabedit"
elseif opts.split then
cmd = "split"
else
cmd = "edit"
end
vim.cmd({
cmd = cmd,
args = { url },
Expand Down
4 changes: 2 additions & 2 deletions tests/altbuf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ a.describe("Alternate buffer", function()
oil.open_float()
test_util.wait_for_autocmd("BufReadPost")
-- This is lazy, but testing the actual select logic is more difficult. We can simply
-- replicated it by closing the current window and then doing the edit
-- replicate it by closing the current window and then doing the edit
vim.api.nvim_win_close(0, true)
vim.cmd.edit({ args = { "bar" } })
assert.equals("foo", vim.fn.expand("#"))
Expand All @@ -88,7 +88,7 @@ a.describe("Alternate buffer", function()
oil.open_float()
test_util.wait_for_autocmd("BufReadPost")
-- This is lazy, but testing the actual select logic is more difficult. We can simply
-- replicated it by closing the current window and then doing the edit
-- replicate it by closing the current window and then doing the edit
vim.api.nvim_win_close(0, true)
vim.cmd.edit({ args = { "bar" } })
assert.equals("foo", vim.fn.expand("#"))
Expand Down
1 change: 1 addition & 0 deletions tests/url_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe("url", function()
it("get_url_for_path", function()
local cases = {
{ "", "oil://" .. util.addslash(vim.fn.getcwd()) },
{ "term://~/oil.nvim//52953:/bin/bash", "oil://" .. vim.loop.os_homedir() .. "/oil.nvim/" },
{ "/foo/bar.txt", "oil:///foo/", "bar.txt" },
{ "oil:///foo/bar.txt", "oil:///foo/", "bar.txt" },
{ "oil:///", "oil:///" },
Expand Down

0 comments on commit 48eec8b

Please sign in to comment.