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