|
| 1 | +---@diagnostic disable |
| 2 | +-- TODO: This is implemented only for files currently. |
| 3 | +-- https://tools.ietf.org/html/rfc3986 |
| 4 | +-- https://tools.ietf.org/html/rfc2732 |
| 5 | +-- https://tools.ietf.org/html/rfc2396 |
| 6 | + |
| 7 | +local M = {} |
| 8 | +local sbyte = string.byte |
| 9 | +local schar = string.char |
| 10 | +local tohex = require('bit').tohex |
| 11 | +local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):.*' |
| 12 | +local WINDOWS_URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9.+-]*):[a-zA-Z]:.*' |
| 13 | +local PATTERNS = { |
| 14 | + -- RFC 2396 |
| 15 | + -- https://tools.ietf.org/html/rfc2396#section-2.2 |
| 16 | + rfc2396 = "^A-Za-z0-9%-_.!~*'()", |
| 17 | + -- RFC 2732 |
| 18 | + -- https://tools.ietf.org/html/rfc2732 |
| 19 | + rfc2732 = "^A-Za-z0-9%-_.!~*'()[]", |
| 20 | + -- RFC 3986 |
| 21 | + -- https://tools.ietf.org/html/rfc3986#section-2.2 |
| 22 | + rfc3986 = "^A-Za-z0-9%-._~!$&'()*+,;=:@/", |
| 23 | +} |
| 24 | + |
| 25 | +---Converts hex to char |
| 26 | +---@param hex string |
| 27 | +---@return string |
| 28 | +local function hex_to_char(hex) |
| 29 | + return schar(tonumber(hex, 16)) |
| 30 | +end |
| 31 | + |
| 32 | +---@param char string |
| 33 | +---@return string |
| 34 | +local function percent_encode_char(char) |
| 35 | + return '%' .. tohex(sbyte(char), 2) |
| 36 | +end |
| 37 | + |
| 38 | +---@param uri string |
| 39 | +---@return boolean |
| 40 | +local function is_windows_file_uri(uri) |
| 41 | + return uri:match('^file:/+[a-zA-Z]:') ~= nil |
| 42 | +end |
| 43 | + |
| 44 | +---URI-encodes a string using percent escapes. |
| 45 | +---@param str string string to encode |
| 46 | +---@param rfc "rfc2396" | "rfc2732" | "rfc3986" | nil |
| 47 | +---@return string encoded string |
| 48 | +function M.uri_encode(str, rfc) |
| 49 | + local pattern = PATTERNS[rfc] or PATTERNS.rfc3986 |
| 50 | + return (str:gsub('([' .. pattern .. '])', percent_encode_char)) -- clamped to 1 retval with () |
| 51 | +end |
| 52 | + |
| 53 | +---URI-decodes a string containing percent escapes. |
| 54 | +---@param str string string to decode |
| 55 | +---@return string decoded string |
| 56 | +function M.uri_decode(str) |
| 57 | + return (str:gsub('%%([a-fA-F0-9][a-fA-F0-9])', hex_to_char)) -- clamped to 1 retval with () |
| 58 | +end |
| 59 | + |
| 60 | +---Gets a URI from a file path. |
| 61 | +---@param path string Path to file |
| 62 | +---@return string URI |
| 63 | +function M.uri_from_fname(path) |
| 64 | + local volume_path, fname = path:match('^([a-zA-Z]:)(.*)') ---@type string? |
| 65 | + local is_windows = volume_path ~= nil |
| 66 | + if is_windows then |
| 67 | + path = volume_path .. M.uri_encode(fname:gsub('\\', '/')) |
| 68 | + else |
| 69 | + path = M.uri_encode(path) |
| 70 | + end |
| 71 | + local uri_parts = { 'file://' } |
| 72 | + if is_windows then |
| 73 | + table.insert(uri_parts, '/') |
| 74 | + end |
| 75 | + table.insert(uri_parts, path) |
| 76 | + return table.concat(uri_parts) |
| 77 | +end |
| 78 | + |
| 79 | +---Gets a URI from a bufnr. |
| 80 | +---@param bufnr integer |
| 81 | +---@return string URI |
| 82 | +function M.uri_from_bufnr(bufnr) |
| 83 | + local fname = vim.api.nvim_buf_get_name(bufnr) |
| 84 | + local volume_path = fname:match('^([a-zA-Z]:).*') |
| 85 | + local is_windows = volume_path ~= nil |
| 86 | + local scheme ---@type string? |
| 87 | + if is_windows then |
| 88 | + fname = fname:gsub('\\', '/') |
| 89 | + scheme = fname:match(WINDOWS_URI_SCHEME_PATTERN) |
| 90 | + else |
| 91 | + scheme = fname:match(URI_SCHEME_PATTERN) |
| 92 | + end |
| 93 | + if scheme then |
| 94 | + return fname |
| 95 | + else |
| 96 | + return M.uri_from_fname(fname) |
| 97 | + end |
| 98 | +end |
| 99 | + |
| 100 | +---Gets a filename from a URI. |
| 101 | +---@param uri string |
| 102 | +---@return string filename or unchanged URI for non-file URIs |
| 103 | +function M.uri_to_fname(uri) |
| 104 | + local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri) |
| 105 | + if scheme ~= 'file' then |
| 106 | + return uri |
| 107 | + end |
| 108 | + local fragment_index = uri:find('#') |
| 109 | + if fragment_index ~= nil then |
| 110 | + uri = uri:sub(1, fragment_index - 1) |
| 111 | + end |
| 112 | + uri = M.uri_decode(uri) |
| 113 | + --TODO improve this. |
| 114 | + if is_windows_file_uri(uri) then |
| 115 | + uri = uri:gsub('^file:/+', ''):gsub('/', '\\') |
| 116 | + else |
| 117 | + uri = uri:gsub('^file:/+', '/') ---@type string |
| 118 | + end |
| 119 | + return uri |
| 120 | +end |
| 121 | + |
| 122 | +---Gets the buffer for a uri. |
| 123 | +---Creates a new unloaded buffer if no buffer for the uri already exists. |
| 124 | +---@param uri string |
| 125 | +---@return integer bufnr |
| 126 | +function M.uri_to_bufnr(uri) |
| 127 | + return vim.fn.bufadd(M.uri_to_fname(uri)) |
| 128 | +end |
| 129 | + |
| 130 | +return M |
0 commit comments