From ccd2cc1abd69f677edabd0b66dfd9689eeee54b9 Mon Sep 17 00:00:00 2001 From: Jaehwang Jung Date: Sat, 4 Mar 2023 22:05:16 +0900 Subject: docs(uri): number → integer (#22515) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- runtime/lua/vim/uri.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'runtime/lua/vim/uri.lua') diff --git a/runtime/lua/vim/uri.lua b/runtime/lua/vim/uri.lua index d6b0b7410e..38759fcdc0 100644 --- a/runtime/lua/vim/uri.lua +++ b/runtime/lua/vim/uri.lua @@ -80,7 +80,7 @@ local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):.*' local WINDOWS_URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):[a-zA-Z]:.*' --- Get a URI from a bufnr ----@param bufnr number +---@param bufnr integer ---@return string URI local function uri_from_bufnr(bufnr) local fname = vim.api.nvim_buf_get_name(bufnr) @@ -123,7 +123,7 @@ end --- Creates a new unloaded buffer if no buffer for the uri already exists. -- ---@param uri string ----@return number bufnr +---@return integer bufnr local function uri_to_bufnr(uri) return vim.fn.bufadd(uri_to_fname(uri)) end -- cgit From bfb28b62dab756ec76a73506c2070ddf491a0cdd Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Thu, 13 Apr 2023 15:29:13 -0600 Subject: refactor: remove modelines from Lua files Now that we have builtin EditorConfig support and a formatting check in CI, these are not necessary. --- runtime/lua/vim/uri.lua | 1 - 1 file changed, 1 deletion(-) (limited to 'runtime/lua/vim/uri.lua') diff --git a/runtime/lua/vim/uri.lua b/runtime/lua/vim/uri.lua index 38759fcdc0..08ed829114 100644 --- a/runtime/lua/vim/uri.lua +++ b/runtime/lua/vim/uri.lua @@ -134,4 +134,3 @@ return { uri_to_fname = uri_to_fname, uri_to_bufnr = uri_to_bufnr, } --- vim:sw=2 ts=2 et -- cgit From 766f4978d6cb146511cf0b676c01e5327db46647 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 10 Jul 2023 19:38:15 +0800 Subject: fix(lint): lint warnings #24226 --- runtime/lua/vim/uri.lua | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'runtime/lua/vim/uri.lua') diff --git a/runtime/lua/vim/uri.lua b/runtime/lua/vim/uri.lua index 08ed829114..dec7840eb0 100644 --- a/runtime/lua/vim/uri.lua +++ b/runtime/lua/vim/uri.lua @@ -30,14 +30,8 @@ do -- https://tools.ietf.org/html/rfc3986#section-2.2 rfc3986 = "^A-Za-z0-9%-._~!$&'()*+,;=:@/", } - local sbyte, tohex = string.byte - if jit then - tohex = require('bit').tohex - else - tohex = function(b) - return string.format('%02x', b) - end - end + local sbyte = string.byte + local tohex = require('bit').tohex ---@private local function percent_encode_char(char) -- cgit From be74807eef13ff8c90d55cf8b22b01d6d33b1641 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 18 Jul 2023 15:42:30 +0100 Subject: docs(lua): more improvements (#24387) * docs(lua): teach lua2dox how to table * docs(lua): teach gen_vimdoc.py about local functions No more need to mark local functions with @private * docs(lua): mention @nodoc and @meta in dev-lua-doc * fixup! Co-authored-by: Justin M. Keyes --------- Co-authored-by: Justin M. Keyes --- runtime/lua/vim/uri.lua | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'runtime/lua/vim/uri.lua') diff --git a/runtime/lua/vim/uri.lua b/runtime/lua/vim/uri.lua index dec7840eb0..eaa64a6fad 100644 --- a/runtime/lua/vim/uri.lua +++ b/runtime/lua/vim/uri.lua @@ -8,7 +8,6 @@ do local schar = string.char --- Convert hex to char - ---@private local function hex_to_char(hex) return schar(tonumber(hex, 16)) end @@ -33,7 +32,6 @@ do local sbyte = string.byte local tohex = require('bit').tohex - ---@private local function percent_encode_char(char) return '%' .. tohex(sbyte(char), 2) end @@ -46,15 +44,16 @@ do end end ----@private local function is_windows_file_uri(uri) return uri:match('^file:/+[a-zA-Z]:') ~= nil end +local M = {} + --- Get a URI from a file path. ---@param path string Path to file ---@return string URI -local function uri_from_fname(path) +function M.uri_from_fname(path) local volume_path, fname = path:match('^([a-zA-Z]:)(.*)') local is_windows = volume_path ~= nil if is_windows then @@ -76,7 +75,7 @@ local WINDOWS_URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):[a-zA-Z]:.*' --- Get a URI from a bufnr ---@param bufnr integer ---@return string URI -local function uri_from_bufnr(bufnr) +function M.uri_from_bufnr(bufnr) local fname = vim.api.nvim_buf_get_name(bufnr) local volume_path = fname:match('^([a-zA-Z]:).*') local is_windows = volume_path ~= nil @@ -90,14 +89,14 @@ local function uri_from_bufnr(bufnr) if scheme then return fname else - return uri_from_fname(fname) + return M.uri_from_fname(fname) end end --- Get a filename from a URI ---@param uri string ---@return string filename or unchanged URI for non-file URIs -local function uri_to_fname(uri) +function M.uri_to_fname(uri) local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri) if scheme ~= 'file' then return uri @@ -118,13 +117,8 @@ end -- ---@param uri string ---@return integer bufnr -local function uri_to_bufnr(uri) - return vim.fn.bufadd(uri_to_fname(uri)) +function M.uri_to_bufnr(uri) + return vim.fn.bufadd(M.uri_to_fname(uri)) end -return { - uri_from_fname = uri_from_fname, - uri_from_bufnr = uri_from_bufnr, - uri_to_fname = uri_to_fname, - uri_to_bufnr = uri_to_bufnr, -} +return M -- cgit From 0804034c07ad5883bc653d054e549a87d429a8b7 Mon Sep 17 00:00:00 2001 From: Tyler Miller Date: Tue, 1 Aug 2023 08:28:28 -0700 Subject: fix(loader): cache path ambiguity #24491 Problem: cache paths are derived by replacing each reserved/filesystem- path-sensitive char with a `%` char in the original path. With this method, two different files at two different paths (each containing `%` chars) can erroneously resolve to the very same cache path in certain edge-cases. Solution: derive cache paths by url-encoding the original (path) instead using `vim.uri_encode()` with `"rfc2396"`. Increment `Loader.VERSION` to denote this change. --- runtime/lua/vim/uri.lua | 103 +++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 50 deletions(-) (limited to 'runtime/lua/vim/uri.lua') diff --git a/runtime/lua/vim/uri.lua b/runtime/lua/vim/uri.lua index eaa64a6fad..9dce80b77e 100644 --- a/runtime/lua/vim/uri.lua +++ b/runtime/lua/vim/uri.lua @@ -1,65 +1,71 @@ ---- TODO: This is implemented only for files now. +---TODO: This is implemented only for files currently. -- https://tools.ietf.org/html/rfc3986 -- https://tools.ietf.org/html/rfc2732 -- https://tools.ietf.org/html/rfc2396 -local uri_decode -do - local schar = string.char +local M = {} +local sbyte = string.byte +local schar = string.char +local tohex = require('bit').tohex +local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):.*' +local WINDOWS_URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):[a-zA-Z]:.*' +local PATTERNS = { + ---RFC 2396 + ---https://tools.ietf.org/html/rfc2396#section-2.2 + rfc2396 = "^A-Za-z0-9%-_.!~*'()", + ---RFC 2732 + ---https://tools.ietf.org/html/rfc2732 + rfc2732 = "^A-Za-z0-9%-_.!~*'()[]", + ---RFC 3986 + ---https://tools.ietf.org/html/rfc3986#section-2.2 + rfc3986 = "^A-Za-z0-9%-._~!$&'()*+,;=:@/", +} - --- Convert hex to char - local function hex_to_char(hex) - return schar(tonumber(hex, 16)) - end - uri_decode = function(str) - return str:gsub('%%([a-fA-F0-9][a-fA-F0-9])', hex_to_char) - end +---Converts hex to char +---@param hex string +---@return string +local function hex_to_char(hex) + return schar(tonumber(hex, 16)) end -local uri_encode -do - local PATTERNS = { - --- RFC 2396 - -- https://tools.ietf.org/html/rfc2396#section-2.2 - rfc2396 = "^A-Za-z0-9%-_.!~*'()", - --- RFC 2732 - -- https://tools.ietf.org/html/rfc2732 - rfc2732 = "^A-Za-z0-9%-_.!~*'()[]", - --- RFC 3986 - -- https://tools.ietf.org/html/rfc3986#section-2.2 - rfc3986 = "^A-Za-z0-9%-._~!$&'()*+,;=:@/", - } - local sbyte = string.byte - local tohex = require('bit').tohex - - local function percent_encode_char(char) - return '%' .. tohex(sbyte(char), 2) - end - uri_encode = function(text, rfc) - if not text then - return - end - local pattern = PATTERNS[rfc] or PATTERNS.rfc3986 - return text:gsub('([' .. pattern .. '])', percent_encode_char) - end +---@param char string +---@return string +local function percent_encode_char(char) + return '%' .. tohex(sbyte(char), 2) end +---@param uri string +---@return boolean local function is_windows_file_uri(uri) return uri:match('^file:/+[a-zA-Z]:') ~= nil end -local M = {} +---URI-encodes a string using percent escapes. +---@param str string string to encode +---@param rfc "rfc2396" | "rfc2732" | "rfc3986" | nil +---@return string encoded string +function M.uri_encode(str, rfc) + local pattern = PATTERNS[rfc] or PATTERNS.rfc3986 + return (str:gsub('([' .. pattern .. '])', percent_encode_char)) -- clamped to 1 retval with () +end + +---URI-decodes a string containing percent escapes. +---@param str string string to decode +---@return string decoded string +function M.uri_decode(str) + return (str:gsub('%%([a-fA-F0-9][a-fA-F0-9])', hex_to_char)) -- clamped to 1 retval with () +end ---- Get a URI from a file path. +---Gets a URI from a file path. ---@param path string Path to file ---@return string URI function M.uri_from_fname(path) local volume_path, fname = path:match('^([a-zA-Z]:)(.*)') local is_windows = volume_path ~= nil if is_windows then - path = volume_path .. uri_encode(fname:gsub('\\', '/')) + path = volume_path .. M.uri_encode(fname:gsub('\\', '/')) else - path = uri_encode(path) + path = M.uri_encode(path) end local uri_parts = { 'file://' } if is_windows then @@ -69,10 +75,7 @@ function M.uri_from_fname(path) return table.concat(uri_parts) end -local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):.*' -local WINDOWS_URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):[a-zA-Z]:.*' - ---- Get a URI from a bufnr +---Gets a URI from a bufnr. ---@param bufnr integer ---@return string URI function M.uri_from_bufnr(bufnr) @@ -93,7 +96,7 @@ function M.uri_from_bufnr(bufnr) end end ---- Get a filename from a URI +---Gets a filename from a URI. ---@param uri string ---@return string filename or unchanged URI for non-file URIs function M.uri_to_fname(uri) @@ -101,8 +104,8 @@ function M.uri_to_fname(uri) if scheme ~= 'file' then return uri end - uri = uri_decode(uri) - -- TODO improve this. + uri = M.uri_decode(uri) + --TODO improve this. if is_windows_file_uri(uri) then uri = uri:gsub('^file:/+', '') uri = uri:gsub('/', '\\') @@ -112,8 +115,8 @@ function M.uri_to_fname(uri) return uri end ---- Get the buffer for a uri. ---- Creates a new unloaded buffer if no buffer for the uri already exists. +---Gets the buffer for a uri. +---Creates a new unloaded buffer if no buffer for the uri already exists. -- ---@param uri string ---@return integer bufnr -- cgit From bc0bf9d030bbcb01db69c44cf88b95ca41dd3065 Mon Sep 17 00:00:00 2001 From: Maria José Solano Date: Wed, 20 Sep 2023 19:03:40 -0700 Subject: docs: fix type warnings --- runtime/lua/vim/uri.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'runtime/lua/vim/uri.lua') diff --git a/runtime/lua/vim/uri.lua b/runtime/lua/vim/uri.lua index 9dce80b77e..2dc817c5c1 100644 --- a/runtime/lua/vim/uri.lua +++ b/runtime/lua/vim/uri.lua @@ -60,7 +60,7 @@ end ---@param path string Path to file ---@return string URI function M.uri_from_fname(path) - local volume_path, fname = path:match('^([a-zA-Z]:)(.*)') + local volume_path, fname = path:match('^([a-zA-Z]:)(.*)') ---@type string? local is_windows = volume_path ~= nil if is_windows then path = volume_path .. M.uri_encode(fname:gsub('\\', '/')) @@ -82,7 +82,7 @@ function M.uri_from_bufnr(bufnr) local fname = vim.api.nvim_buf_get_name(bufnr) local volume_path = fname:match('^([a-zA-Z]:).*') local is_windows = volume_path ~= nil - local scheme + local scheme ---@type string? if is_windows then fname = fname:gsub('\\', '/') scheme = fname:match(WINDOWS_URI_SCHEME_PATTERN) @@ -107,10 +107,9 @@ function M.uri_to_fname(uri) uri = M.uri_decode(uri) --TODO improve this. if is_windows_file_uri(uri) then - uri = uri:gsub('^file:/+', '') - uri = uri:gsub('/', '\\') + uri = uri:gsub('^file:/+', ''):gsub('/', '\\') else - uri = uri:gsub('^file:/+', '/') + uri = uri:gsub('^file:/+', '/') ---@type string end return uri end -- cgit