mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 19:38:20 +00:00
Change uri_to_fname to not convert non-file URIs (#12351)
* Change uri_to_fname to not convert non-file URIs A URI with a scheme other than file doesn't have a local file path. * fixup! Change uri_to_fname to not convert non-file URIs * fixup! fixup! Change uri_to_fname to not convert non-file URIs
This commit is contained in:

committed by
GitHub

parent
044eb56ed2
commit
04a0486c66
@@ -65,9 +65,11 @@ local function uri_from_fname(path)
|
|||||||
return table.concat(uri_parts)
|
return table.concat(uri_parts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9+-.]*)://.*'
|
||||||
|
|
||||||
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("^([a-z]+)://.*")
|
local scheme = fname:match(URI_SCHEME_PATTERN)
|
||||||
if scheme then
|
if scheme then
|
||||||
return fname
|
return fname
|
||||||
else
|
else
|
||||||
@@ -76,6 +78,10 @@ local function uri_from_bufnr(bufnr)
|
|||||||
end
|
end
|
||||||
|
|
||||||
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)
|
||||||
|
if scheme ~= 'file' then
|
||||||
|
return uri
|
||||||
|
end
|
||||||
uri = uri_decode(uri)
|
uri = uri_decode(uri)
|
||||||
-- TODO improve this.
|
-- TODO improve this.
|
||||||
if is_windows_file_uri(uri) then
|
if is_windows_file_uri(uri) then
|
||||||
@@ -89,7 +95,7 @@ end
|
|||||||
|
|
||||||
-- Return or create a buffer for a uri.
|
-- Return or create a buffer for a uri.
|
||||||
local function uri_to_bufnr(uri)
|
local function uri_to_bufnr(uri)
|
||||||
local scheme = assert(uri:match("^([a-z]+)://.*"), '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
|
||||||
return vim.fn.bufadd(uri_to_fname(uri))
|
return vim.fn.bufadd(uri_to_fname(uri))
|
||||||
else
|
else
|
||||||
|
@@ -112,6 +112,29 @@ describe('URI methods', function()
|
|||||||
eq('C:\\xy\\åäö\\ɧ\\汉语\\↥\\🤦\\🦄\\å\\بِيَّ.txt', exec_lua(test_case))
|
eq('C:\\xy\\åäö\\ɧ\\汉语\\↥\\🤦\\🦄\\å\\بِيَّ.txt', exec_lua(test_case))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe('decode non-file URI', function()
|
||||||
|
it('uri_to_fname returns non-file URI unchanged', function()
|
||||||
|
eq('jdt1.23+x-z://content/%5C/', exec_lua [[
|
||||||
|
return vim.uri_to_fname('jdt1.23+x-z://content/%5C/')
|
||||||
|
]])
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('uri_to_fname returns non-file upper-case scheme URI unchanged', function()
|
||||||
|
eq('JDT://content/%5C/', exec_lua [[
|
||||||
|
return vim.uri_to_fname('JDT://content/%5C/')
|
||||||
|
]])
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('decode URI without scheme', function()
|
||||||
|
it('fails because URI must have a scheme', function()
|
||||||
|
eq(false, exec_lua [[
|
||||||
|
return pcall(vim.uri_to_fname, 'not_an_uri.txt')
|
||||||
|
]])
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('uri to bufnr', function()
|
describe('uri to bufnr', function()
|
||||||
|
Reference in New Issue
Block a user