fix: type annotations and type errors

This commit is contained in:
Steven Arcangeli 2023-08-12 20:32:52 -07:00
parent 0ccf95ae5d
commit 47c7737618
16 changed files with 83 additions and 22 deletions

View file

@ -46,6 +46,7 @@ local file_columns = {}
local fs_stat_meta_fields = {
stat = function(parent_url, entry, cb)
local _, path = util.parse_url(parent_url)
assert(path)
local dir = fs.posix_to_os_path(path)
vim.loop.fs_stat(fs.join(dir, entry[FIELD_NAME]), cb)
end,
@ -108,6 +109,7 @@ if not fs.is_windows then
render_action = function(action)
local _, path = util.parse_url(action.url)
assert(path)
return string.format(
"CHMOD %s %s",
permissions.mode_to_octal_str(action.value),
@ -117,11 +119,13 @@ if not fs.is_windows then
perform_action = function(action, callback)
local _, path = util.parse_url(action.url)
assert(path)
path = fs.posix_to_os_path(path)
vim.loop.fs_stat(path, function(err, stat)
if err then
return callback(err)
end
assert(stat)
-- We are only changing the lower 12 bits of the mode
local mask = bit.bnot(bit.lshift(1, 12) - 1)
local old_mode = bit.band(stat.mode, mask)
@ -178,6 +182,7 @@ end
---@param callback fun(url: string)
M.normalize_url = function(url, callback)
local scheme, path = util.parse_url(url)
assert(path)
local os_path = vim.fn.fnamemodify(fs.posix_to_os_path(path), ":p")
vim.loop.fs_realpath(os_path, function(err, new_os_path)
local realpath = new_os_path or os_path
@ -210,6 +215,7 @@ end
---@param callback fun(err: nil|string, entries: nil|oil.InternalEntry[])
M.list = function(url, column_defs, callback)
local _, path = util.parse_url(url)
assert(path)
local dir = fs.posix_to_os_path(path)
local fetch_meta = columns.get_metadata_fetcher(M, column_defs)
cache.begin_update_url(url)
@ -300,6 +306,7 @@ end
M.is_modifiable = function(bufnr)
local bufname = vim.api.nvim_buf_get_name(bufnr)
local _, path = util.parse_url(bufname)
assert(path)
local dir = fs.posix_to_os_path(path)
local stat = vim.loop.fs_stat(dir)
if not stat then
@ -329,6 +336,7 @@ end
M.render_action = function(action)
if action.type == "create" then
local _, path = util.parse_url(action.url)
assert(path)
local ret = string.format("CREATE %s", M.to_short_os_path(path, action.entry_type))
if action.link then
ret = ret .. " -> " .. fs.posix_to_os_path(action.link)
@ -336,12 +344,15 @@ M.render_action = function(action)
return ret
elseif action.type == "delete" then
local _, path = util.parse_url(action.url)
assert(path)
return string.format("DELETE %s", M.to_short_os_path(path, action.entry_type))
elseif action.type == "move" or action.type == "copy" then
local dest_adapter = config.get_adapter_by_scheme(action.dest_url)
if dest_adapter == M then
local _, src_path = util.parse_url(action.src_url)
assert(src_path)
local _, dest_path = util.parse_url(action.dest_url)
assert(dest_path)
return string.format(
" %s %s -> %s",
action.type:upper(),
@ -362,6 +373,7 @@ end
M.perform_action = function(action, cb)
if action.type == "create" then
local _, path = util.parse_url(action.url)
assert(path)
path = fs.posix_to_os_path(path)
if action.entry_type == "directory" then
vim.loop.fs_mkdir(path, 493, function(err)
@ -387,6 +399,7 @@ M.perform_action = function(action, cb)
end
elseif action.type == "delete" then
local _, path = util.parse_url(action.url)
assert(path)
path = fs.posix_to_os_path(path)
if config.delete_to_trash then
trash.recursive_delete(path, cb)
@ -397,7 +410,9 @@ M.perform_action = function(action, cb)
local dest_adapter = config.get_adapter_by_scheme(action.dest_url)
if dest_adapter == M then
local _, src_path = util.parse_url(action.src_url)
assert(src_path)
local _, dest_path = util.parse_url(action.dest_url)
assert(dest_path)
src_path = fs.posix_to_os_path(src_path)
dest_path = fs.posix_to_os_path(dest_path)
fs.recursive_move(action.entry_type, src_path, dest_path, vim.schedule_wrap(cb))
@ -409,7 +424,9 @@ M.perform_action = function(action, cb)
local dest_adapter = config.get_adapter_by_scheme(action.dest_url)
if dest_adapter == M then
local _, src_path = util.parse_url(action.src_url)
assert(src_path)
local _, dest_path = util.parse_url(action.dest_url)
assert(dest_path)
src_path = fs.posix_to_os_path(src_path)
dest_path = fs.posix_to_os_path(dest_path)
fs.recursive_copy(action.entry_type, src_path, dest_path, cb)

View file

@ -261,9 +261,11 @@ M.render_action = function(action)
local dest = action.dest_url
if config.get_adapter_by_scheme(src) == M then
local _, path = util.parse_url(dest)
assert(path)
dest = files.to_short_os_path(path, action.entry_type)
else
local _, path = util.parse_url(src)
assert(path)
src = files.to_short_os_path(path, action.entry_type)
end
return string.format(" %s %s -> %s", action.type:upper(), src, dest)
@ -328,9 +330,11 @@ M.perform_action = function(action, cb)
if src_adapter == M then
src_arg = url_to_scp(M.parse_url(action.src_url))
local _, path = util.parse_url(action.dest_url)
assert(path)
dest_arg = fs.posix_to_os_path(path)
else
local _, path = util.parse_url(action.src_url)
assert(path)
src_arg = fs.posix_to_os_path(path)
dest_arg = url_to_scp(M.parse_url(action.dest_url))
end
@ -353,7 +357,9 @@ M.read_file = function(bufnr)
local tmpdir = fs.join(vim.fn.stdpath("cache"), "oil")
fs.mkdirp(tmpdir)
local fd, tmpfile = vim.loop.fs_mkstemp(fs.join(tmpdir, "ssh_XXXXXX"))
vim.loop.fs_close(fd)
if fd then
vim.loop.fs_close(fd)
end
local tmp_bufnr = vim.fn.bufadd(tmpfile)
shell.run({ "scp", "-C", scp_url, tmpfile }, function(err)
@ -371,7 +377,10 @@ M.read_file = function(bufnr)
vim.api.nvim_buf_set_lines(bufnr, 0, 1, true, {})
end
vim.bo[bufnr].modified = false
vim.bo[bufnr].filetype = vim.filetype.match({ buf = bufnr, filename = basename })
local filetype = vim.filetype.match({ buf = bufnr, filename = basename })
if filetype then
vim.bo[bufnr].filetype = filetype
end
vim.cmd.doautocmd({ args = { "BufReadPost", bufname }, mods = { silent = true } })
vim.api.nvim_buf_delete(tmp_bufnr, { force = true })
end)
@ -385,7 +394,9 @@ M.write_file = function(bufnr)
local scp_url = url_to_scp(url)
local tmpdir = fs.join(vim.fn.stdpath("cache"), "oil")
local fd, tmpfile = vim.loop.fs_mkstemp(fs.join(tmpdir, "ssh_XXXXXXXX"))
vim.loop.fs_close(fd)
if fd then
vim.loop.fs_close(fd)
end
vim.cmd.doautocmd({ args = { "BufWritePre", bufname }, mods = { silent = true } })
vim.cmd.write({ args = { tmpfile }, bang = true, mods = { silent = true } })
local tmp_bufnr = vim.fn.bufadd(tmpfile)

View file

@ -3,6 +3,8 @@ local util = require("oil.util")
---@class oil.sshConnection
---@field meta {user?: string, groups?: string[]}
---@field private term_bufnr integer
---@field private jid integer
local SSHConnection = {}
local function output_extend(agg, output)
@ -76,20 +78,21 @@ function SSHConnection.new(url)
-- anything prior to that, it *will* appear. The first line gets swallowed.
"echo '_make_newline_'; echo '===READY==='; exec /bin/bash --norc",
})
local term_bufnr = vim.api.nvim_create_buf(false, true)
local self = setmetatable({
meta = {},
commands = {},
connected = false,
connection_error = nil,
term_bufnr = term_bufnr,
}, {
__index = SSHConnection,
})
self.term_bufnr = vim.api.nvim_create_buf(false, true)
local term_id
local mode = vim.api.nvim_get_mode().mode
util.run_in_fullscreen_win(self.term_bufnr, function()
term_id = vim.api.nvim_open_term(self.term_bufnr, {
util.run_in_fullscreen_win(term_bufnr, function()
term_id = vim.api.nvim_open_term(term_bufnr, {
on_input = function(_, _, _, data)
pcall(vim.api.nvim_chan_send, self.jid, data)
end,
@ -195,6 +198,7 @@ function SSHConnection:_handle_output(start_i)
end
else
for i = start_i, #self._stdout - 1 do
---@type string
local line = self._stdout[i]
if line:match("^===BEGIN===%s*$") then
self._stdout = util.tbl_slice(self._stdout, i + 1)

View file

@ -7,7 +7,7 @@ M.normalize_url = function(url, callback)
callback(url)
end
---@param path string
---@param url string
---@param column_defs string[]
---@param cb fun(err: nil|string, entries: nil|oil.InternalEntry[])
M.list = function(url, column_defs, cb)