Skip to content

Commit

Permalink
feat: make gf work in ssh files (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 30, 2023
1 parent bfc5a4c commit ee81363
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lua/oil/adapters/ssh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local shell = require("oil.shell")
local util = require("oil.util")
local M = {}

local FIELD_NAME = constants.FIELD_NAME
local FIELD_META = constants.FIELD_META

---@class (exact) oil.sshUrl
Expand Down Expand Up @@ -385,6 +386,7 @@ M.read_file = function(bufnr)
end
vim.cmd.doautocmd({ args = { "BufReadPost", bufname }, mods = { silent = true } })
vim.api.nvim_buf_delete(tmp_bufnr, { force = true })
vim.keymap.set("n", "gf", M.goto_file, { buffer = bufnr })
end)
end

Expand Down Expand Up @@ -416,4 +418,45 @@ M.write_file = function(bufnr)
end)
end

M.goto_file = function()
local url = M.parse_url(vim.api.nvim_buf_get_name(0))
local word = vim.fn.expand("<cWORD>")
local fname = vim.fn.matchlist(word, "\\v\\f+")[1]
local fullpath = fname
if not fs.is_absolute(fname) then
local pardir = vim.fs.dirname(url.path)
fullpath = fs.join(pardir, fname)
end
url.path = vim.fs.dirname(fullpath)
local parurl = url_to_str(url)

util.adapter_list_all(M, parurl, {}, function(err, entries)
if err then
vim.notify(string.format("Error finding file '%s': %s", fname, err), vim.log.levels.ERROR)
return
end
assert(entries)
local name_map = {}
for _, entry in ipairs(entries) do
name_map[entry[FIELD_NAME]] = entry
end

local basename = vim.fs.basename(fullpath)
if name_map[basename] then
url.path = fullpath
vim.cmd.edit({ args = { url_to_str(url) } })
return
end
for suffix in vim.gsplit(vim.o.suffixesadd, ",", { plain = true, trimempty = true }) do
local suffixname = basename .. suffix
if name_map[suffixname] then
url.path = fullpath .. suffix
vim.cmd.edit({ args = { url_to_str(url) } })
return
end
end
vim.notify(string.format("Can't find file '%s'", fname), vim.log.levels.ERROR)
end)
end

return M
29 changes: 29 additions & 0 deletions lua/oil/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,33 @@ M.buf_get_win = function(bufnr, preferred_win)
return nil
end

---@param adapter oil.Adapter
---@param url string
---@param opts {columns?: string[], no_cache?: boolean}
---@param callback fun(err: nil|string, entries: nil|oil.InternalEntry[])
M.adapter_list_all = function(adapter, url, opts, callback)
local cache = require("oil.cache")
if not opts.no_cache then
local entries = cache.list_url(url)
if not vim.tbl_isempty(entries) then
return callback(nil, vim.tbl_values(entries))
end
end
local ret = {}
adapter.list(url, opts.columns or {}, function(err, entries, fetch_more)
if err then
callback(err)
return
end
if entries then
vim.list_extend(ret, entries)
end
if fetch_more then
vim.defer_fn(fetch_more, 4)
else
callback(nil, ret)
end
end)
end

return M

0 comments on commit ee81363

Please sign in to comment.