fix: correctly capture uri scheme on windows (#16027)

closes https://github.com/neovim/neovim/issues/15261

* normalize uri path to forward slashes on windows
* use a capture group on windows that avoids mistaking drive letters as uri scheme
This commit is contained in:
Michael Lingelbach 2021-10-15 12:03:41 -07:00 committed by GitHub
parent 5fd4557573
commit 68b2a9e569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -75,13 +75,22 @@ local function uri_from_fname(path)
end end
local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9+-.]*):.*' 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 --- Get a URI from a bufnr
---@param bufnr (number): Buffer number ---@param bufnr (number): Buffer number
---@return URI ---@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 volume_path = fname:match("^([a-zA-Z]:).*")
local is_windows = volume_path ~= nil
local scheme
if is_windows then
fname = fname:gsub("\\", "/")
scheme = fname:match(WINDOWS_URI_SCHEME_PATTERN)
else
scheme = fname:match(URI_SCHEME_PATTERN)
end
if scheme then if scheme then
return fname return fname
else else

View File

@ -2,6 +2,7 @@ local helpers = require('test.functional.helpers')(after_each)
local clear = helpers.clear local clear = helpers.clear
local exec_lua = helpers.exec_lua local exec_lua = helpers.exec_lua
local eq = helpers.eq local eq = helpers.eq
local write_file = require('test.helpers').write_file
describe('URI methods', function() describe('URI methods', function()
before_each(function() before_each(function()
@ -158,6 +159,22 @@ describe('URI methods', function()
end) end)
describe('uri from bufnr', function()
it('Windows paths should not be treated as uris', function()
if not helpers.iswin() then return end
local file = helpers.tmpname()
write_file(file, 'Test content')
local test_case = string.format([[
local file = '%s'
return vim.uri_from_bufnr(vim.fn.bufadd(file))
]], file)
local expected_uri = 'file:///' .. file:gsub("\\", "/")
eq(expected_uri, exec_lua(test_case))
os.remove(file)
end)
end)
describe('uri to bufnr', function() describe('uri to bufnr', function()
it('uri_to_bufnr & uri_from_bufnr returns original uri for non-file uris', function() it('uri_to_bufnr & uri_from_bufnr returns original uri for non-file uris', function()
local uri = 'jdt://contents/java.base/java.util/List.class?=sql/%5C/home%5C/user%5C/.jabba%5C/jdk%5C/openjdk%5C@1.14.0%5C/lib%5C/jrt-fs.jar%60java.base=/javadoc_location=/https:%5C/%5C/docs.oracle.com%5C/en%5C/java%5C/javase%5C/14%5C/docs%5C/api%5C/=/%3Cjava.util(List.class' local uri = 'jdt://contents/java.base/java.util/List.class?=sql/%5C/home%5C/user%5C/.jabba%5C/jdk%5C/openjdk%5C@1.14.0%5C/lib%5C/jrt-fs.jar%60java.base=/javadoc_location=/https:%5C/%5C/docs.oracle.com%5C/en%5C/java%5C/javase%5C/14%5C/docs%5C/api%5C/=/%3Cjava.util(List.class'