diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 6f3ef7e237..40f18bde46 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -173,12 +173,32 @@ utils.is_path_hidden = function(opts, path_display) or type(path_display) == "table" and (vim.tbl_contains(path_display, "hidden") or path_display.hidden) end -local URI_SCHEME_PATTERN = "^([a-zA-Z]+[a-zA-Z0-9.+-]*):.*" -local WINDOWS_ROOT_PATTERN = "^[a-zA-Z]:\\" utils.is_uri = function(filename) - local is_uri_match = filename:match(URI_SCHEME_PATTERN) ~= nil - local is_windows_root_match = filename:match(WINDOWS_ROOT_PATTERN) - return is_uri_match and not is_windows_root_match + local char = string.byte(filename, 1) + + -- is alpha? + if char < 65 or (char > 90 and char < 97) or char > 122 then + return false + end + + for i = 2, #filename do + char = string.byte(filename, i) + if char == 58 then -- `:` + return i < #filename and string.byte(filename, i + 1) ~= 92 -- `\` + elseif + not ( + (char >= 48 and char <= 57) -- 0-9 + or (char >= 65 and char <= 90) -- A-Z + or (char >= 97 and char <= 122) -- a-z + or char == 43 -- `+` + or char == 46 -- `.` + or char == 45 -- `-` + ) + then + return false + end + end + return false end local calc_result_length = function(truncate_len) diff --git a/lua/tests/automated/utils_spec.lua b/lua/tests/automated/utils_spec.lua index 2a783bf4f7..491f55ac37 100644 --- a/lua/tests/automated/utils_spec.lua +++ b/lua/tests/automated/utils_spec.lua @@ -1,7 +1,7 @@ local utils = require "telescope.utils" describe("is_uri", function() - it("detects valid uris", function() + describe("detects valid uris", function() local uris = { [[https://www.example.com/index.html]], [[ftp://ftp.example.com/files/document.pdf]], @@ -16,40 +16,64 @@ describe("is_uri", function() } for _, uri in ipairs(uris) do - assert.True(utils.is_uri(uri)) + it(uri, function() + assert.True(utils.is_uri(uri)) + end) end end) - it("handles windows paths", function() + describe("detects invalid uris/paths", function() + local inputs = { + "hello", + "hello:", + "123", + } + for _, input in ipairs(inputs) do + it(input, function() + assert.False(utils.is_uri(input)) + end) + end + end) + + describe("handles windows paths", function() local paths = { [[C:\Users\Usuario\Documents\archivo.txt]], [[D:\Projects\project_folder\source_code.py]], [[E:\Music\song.mp3]], } - for _, path in ipairs(paths) do - assert.False(utils.is_uri(path)) + + for _, uri in ipairs(paths) do + it(uri, function() + assert.False(utils.is_uri(uri)) + end) end end) - it("handles linux paths", function() + describe("handles linux paths", function() local paths = { [[/home/usuario/documents/archivo.txt]], [[/var/www/html/index.html]], [[/mnt/backup/backup_file.tar.gz]], } + for _, path in ipairs(paths) do - assert.False(utils.is_uri(path)) + it(path, function() + assert.False(utils.is_uri(path)) + end) end end) - it("handles macos paths", function() + describe("handles macos paths", function() local paths = { [[/Users/Usuario/Documents/archivo.txt]], [[/Applications/App.app/Contents/MacOS/app_executable]], [[/Volumes/ExternalDrive/Data/file.xlsx]], } + for _, path in ipairs(paths) do - assert.False(utils.is_uri(path)) + it(path, function() + assert.False(utils.is_uri(path)) + end) end end) end)