doc: Add docs for uri functions (#12887)

This commit is contained in:
TJ DeVries 2020-09-14 09:12:17 -04:00 committed by GitHub
parent 20f7b94284
commit 4a2618c817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 1 deletions

View File

@ -1567,4 +1567,46 @@ validate({opt}) *vim.validate()*
• msg: (optional) error string if validation • msg: (optional) error string if validation
fails fails
==============================================================================
Lua module: uri *lua-uri*
uri_from_bufnr({bufnr}) *vim.uri_from_bufnr()*
Get a URI from a bufnr
Parameters: ~
{bufnr} (number): Buffer number
Return: ~
URI
uri_from_fname({path}) *vim.uri_from_fname()*
Get a URI from a file path.
Parameters: ~
{path} (string): Path to file
Return: ~
URI
uri_to_bufnr({uri}) *vim.uri_to_bufnr()*
Return or create a buffer for a uri.
Parameters: ~
{uri} (string): The URI
Return: ~
bufnr.
Note:
Creates buffer but does not load it
uri_to_fname({uri}) *vim.uri_to_fname()*
Get a filename from a URI
Parameters: ~
{uri} (string): The URI
Return: ~
Filename
vim:tw=78:ts=8:ft=help:norl: vim:tw=78:ts=8:ft=help:norl:

View File

@ -7,6 +7,9 @@
local uri_decode local uri_decode
do do
local schar = string.char local schar = string.char
--- Convert hex to char
--@private
local function hex_to_char(hex) local function hex_to_char(hex)
return schar(tonumber(hex, 16)) return schar(tonumber(hex, 16))
end end
@ -34,6 +37,8 @@ do
else else
tohex = function(b) return string.format("%02x", b) end tohex = function(b) return string.format("%02x", b) end
end end
--@private
local function percent_encode_char(char) local function percent_encode_char(char)
return "%"..tohex(sbyte(char), 2) return "%"..tohex(sbyte(char), 2)
end end
@ -45,10 +50,14 @@ do
end end
--@private
local function is_windows_file_uri(uri) local function is_windows_file_uri(uri)
return uri:match('^file:///[a-zA-Z]:') ~= nil return uri:match('^file:///[a-zA-Z]:') ~= nil
end end
--- Get a URI from a file path.
--@param path (string): Path to file
--@return URI
local function uri_from_fname(path) local function uri_from_fname(path)
local volume_path, fname = path:match("^([a-zA-Z]:)(.*)") local volume_path, fname = path:match("^([a-zA-Z]:)(.*)")
local is_windows = volume_path ~= nil local is_windows = volume_path ~= nil
@ -67,6 +76,9 @@ end
local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9+-.]*)://.*' local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9+-.]*)://.*'
--- Get a URI from a bufnr
--@param bufnr (number): Buffer number
--@return URI
local function uri_from_bufnr(bufnr) local function uri_from_bufnr(bufnr)
local fname = vim.api.nvim_buf_get_name(bufnr) local fname = vim.api.nvim_buf_get_name(bufnr)
local scheme = fname:match(URI_SCHEME_PATTERN) local scheme = fname:match(URI_SCHEME_PATTERN)
@ -77,6 +89,9 @@ local function uri_from_bufnr(bufnr)
end end
end end
--- Get a filename from a URI
--@param uri (string): The URI
--@return Filename
local function uri_to_fname(uri) local function uri_to_fname(uri)
local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri) local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri)
if scheme ~= 'file' then if scheme ~= 'file' then
@ -93,7 +108,10 @@ local function uri_to_fname(uri)
return uri return uri
end end
-- Return or create a buffer for a uri. --- Return or create a buffer for a uri.
--@param uri (string): The URI
--@return bufnr.
--@note Creates buffer but does not load it
local function uri_to_bufnr(uri) local function uri_to_bufnr(uri)
local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri) local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri)
if scheme == 'file' then if scheme == 'file' then

View File

@ -113,10 +113,12 @@ CONFIG = {
'section_order': [ 'section_order': [
'vim.lua', 'vim.lua',
'shared.lua', 'shared.lua',
'uri.lua',
], ],
'files': ' '.join([ 'files': ' '.join([
os.path.join(base_dir, 'src/nvim/lua/vim.lua'), os.path.join(base_dir, 'src/nvim/lua/vim.lua'),
os.path.join(base_dir, 'runtime/lua/vim/shared.lua'), os.path.join(base_dir, 'runtime/lua/vim/shared.lua'),
os.path.join(base_dir, 'runtime/lua/vim/uri.lua'),
]), ]),
'file_patterns': '*.lua', 'file_patterns': '*.lua',
'fn_name_prefix': '', 'fn_name_prefix': '',
@ -129,6 +131,7 @@ CONFIG = {
'module_override': { 'module_override': {
# `shared` functions are exposed on the `vim` module. # `shared` functions are exposed on the `vim` module.
'shared': 'vim', 'shared': 'vim',
'uri': 'vim',
}, },
'append_only': [ 'append_only': [
'shared.lua', 'shared.lua',