aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/secure.lua
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2023-11-29 21:52:58 +0000
committerJosh Rahm <joshuarahm@gmail.com>2023-11-29 21:52:58 +0000
commit931bffbda3668ddc609fc1da8f9eb576b170aa52 (patch)
treed8c1843a95da5ea0bb4acc09f7e37843d9995c86 /runtime/lua/vim/secure.lua
parent142d9041391780ac15b89886a54015fdc5c73995 (diff)
parent4a8bf24ac690004aedf5540fa440e788459e5e34 (diff)
downloadrneovim-931bffbda3668ddc609fc1da8f9eb576b170aa52.tar.gz
rneovim-931bffbda3668ddc609fc1da8f9eb576b170aa52.tar.bz2
rneovim-931bffbda3668ddc609fc1da8f9eb576b170aa52.zip
Merge remote-tracking branch 'upstream/master' into userreguserreg
Diffstat (limited to 'runtime/lua/vim/secure.lua')
-rw-r--r--runtime/lua/vim/secure.lua34
1 files changed, 19 insertions, 15 deletions
diff --git a/runtime/lua/vim/secure.lua b/runtime/lua/vim/secure.lua
index 443b152273..d29c356af3 100644
--- a/runtime/lua/vim/secure.lua
+++ b/runtime/lua/vim/secure.lua
@@ -1,11 +1,10 @@
local M = {}
----@private
--- Reads trust database from $XDG_STATE_HOME/nvim/trust.
---
----@return (table) Contents of trust database, if it exists. Empty table otherwise.
+---@return table<string, string> Contents of trust database, if it exists. Empty table otherwise.
local function read_trust()
- local trust = {}
+ local trust = {} ---@type table<string, string>
local f = io.open(vim.fn.stdpath('state') .. '/trust', 'r')
if f then
local contents = f:read('*a')
@@ -22,16 +21,15 @@ local function read_trust()
return trust
end
----@private
--- Writes provided {trust} table to trust database at
--- $XDG_STATE_HOME/nvim/trust.
---
----@param trust (table) Trust table to write
+---@param trust table<string, string> Trust table to write
local function write_trust(trust)
vim.validate({ trust = { trust, 't' } })
local f = assert(io.open(vim.fn.stdpath('state') .. '/trust', 'w'))
- local t = {}
+ local t = {} ---@type string[]
for p, h in pairs(trust) do
t[#t + 1] = string.format('%s %s\n', h, p)
end
@@ -51,7 +49,7 @@ end
--- trusted, or nil otherwise.
function M.read(path)
vim.validate({ path = { path, 's' } })
- local fullpath = vim.loop.fs_realpath(vim.fs.normalize(path))
+ local fullpath = vim.uv.fs_realpath(vim.fs.normalize(path))
if not fullpath then
return nil
end
@@ -63,7 +61,7 @@ function M.read(path)
return nil
end
- local contents
+ local contents ---@type string?
do
local f = io.open(fullpath, 'r')
if not f then
@@ -110,6 +108,11 @@ function M.read(path)
return contents
end
+---@class vim.trust.opts
+---@field action string
+---@field path? string
+---@field bufnr? integer
+
--- Manage the trust database.
---
--- The trust database is located at |$XDG_STATE_HOME|/nvim/trust.
@@ -121,9 +124,8 @@ end
--- - path (string|nil): Path to a file to update. Mutually exclusive with {bufnr}.
--- Cannot be used when {action} is "allow".
--- - bufnr (number|nil): Buffer number to update. Mutually exclusive with {path}.
----@return (boolean, string) success, msg:
---- - true and full path of target file if operation was successful
---- - false and error message on failure
+---@return boolean success true if operation was successful
+---@return string msg full path if operation was successful, else error message
function M.trust(opts)
vim.validate({
path = { opts.path, 's', true },
@@ -137,6 +139,7 @@ function M.trust(opts)
},
})
+ ---@cast opts vim.trust.opts
local path = opts.path
local bufnr = opts.bufnr
local action = opts.action
@@ -147,15 +150,15 @@ function M.trust(opts)
assert(not path, '"path" is not valid when action is "allow"')
end
- local fullpath
+ local fullpath ---@type string?
if path then
- fullpath = vim.loop.fs_realpath(vim.fs.normalize(path))
+ fullpath = vim.uv.fs_realpath(vim.fs.normalize(path))
elseif bufnr then
local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname == '' then
return false, 'buffer is not associated with a file'
end
- fullpath = vim.loop.fs_realpath(vim.fs.normalize(bufname))
+ fullpath = vim.uv.fs_realpath(vim.fs.normalize(bufname))
else
error('one of "path" or "bufnr" is required')
end
@@ -168,7 +171,8 @@ function M.trust(opts)
if action == 'allow' then
local newline = vim.bo[bufnr].fileformat == 'unix' and '\n' or '\r\n'
- local contents = table.concat(vim.api.nvim_buf_get_lines(bufnr, 0, -1, false), newline)
+ local contents =
+ table.concat(vim.api.nvim_buf_get_lines(bufnr --[[@as integer]], 0, -1, false), newline)
if vim.bo[bufnr].endofline then
contents = contents .. newline
end