diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..911933099b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +lua/tests/fixtures/split_lines/carriage_newline.txt text eol=crlf +lua/tests/fixtures/split_lines/newline.txt text eol=lf diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index 9149ecb4db..faad006272 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -531,7 +531,7 @@ internal.oldfiles = function(opts) local results = {} if opts.include_current_session then - for _, buffer in ipairs(vim.split(vim.fn.execute ":buffers! t", "\n")) do + for _, buffer in ipairs(utils.split_lines(vim.fn.execute ":buffers! t")) do local match = tonumber(string.match(buffer, "%s*(%d+)")) local open_by_lsp = string.match(buffer, "line 0$") if match and not open_by_lsp then @@ -574,7 +574,7 @@ end internal.command_history = function(opts) local history_string = vim.fn.execute "history cmd" - local history_list = vim.split(history_string, "\n") + local history_list = utils.split_lines(history_string) local results = {} local filter_fn = opts.filter_fn @@ -614,7 +614,7 @@ end internal.search_history = function(opts) local search_string = vim.fn.execute "history search" - local search_list = vim.split(search_string, "\n") + local search_list = utils.split_lines(search_string) local results = {} for i = #search_list, 3, -1 do @@ -690,7 +690,7 @@ internal.help_tags = function(opts) opts.fallback = vim.F.if_nil(opts.fallback, true) opts.file_ignore_patterns = {} - local langs = vim.split(opts.lang, ",", true) + local langs = vim.split(opts.lang, ",", { trimempty = true }) if opts.fallback and not vim.tbl_contains(langs, "en") then table.insert(langs, "en") end @@ -729,11 +729,11 @@ internal.help_tags = function(opts) local delimiter = string.char(9) for _, lang in ipairs(langs) do for _, file in ipairs(tag_files[lang] or {}) do - local lines = vim.split(Path:new(file):read(), "\r?\n", { trimempty = true }) + local lines = utils.split_lines(Path:new(file):read(), { trimempty = true }) for _, line in ipairs(lines) do -- TODO: also ignore tagComment starting with ';' if not line:match "^!_TAG_" then - local fields = vim.split(line, delimiter, true) + local fields = vim.split(line, delimiter, { trimempty = true }) if #fields == 3 and not tags_map[fields[1]] then if fields[1] ~= "help-tags" or fields[2] ~= "tags" then table.insert(tags, { diff --git a/lua/telescope/init.lua b/lua/telescope/init.lua index acb56e79dc..d7cc544328 100644 --- a/lua/telescope/init.lua +++ b/lua/telescope/init.lua @@ -1,4 +1,5 @@ local _extensions = require "telescope._extensions" +local utils = require "telescope.utils" local telescope = {} @@ -164,7 +165,7 @@ telescope.__format_setup_keys = function() table.insert(result, "") table.insert(result, string.format("%s*telescope.defaults.%s*", string.rep(" ", 70 - 20 - #name), name)) table.insert(result, string.format("%s: ~", name)) - for _, line in ipairs(vim.split(desc, "\n")) do + for _, line in ipairs(utils.split_lines(desc)) do table.insert(result, string.format(" %s", line)) end end diff --git a/lua/telescope/previewers/buffer_previewer.lua b/lua/telescope/previewers/buffer_previewer.lua index a404c14cd7..72854e41a0 100644 --- a/lua/telescope/previewers/buffer_previewer.lua +++ b/lua/telescope/previewers/buffer_previewer.lua @@ -1090,7 +1090,7 @@ previewers.highlights = defaulter(function(_) define_preview = function(self, entry) if not self.state.bufname then - local output = vim.split(vim.fn.execute "highlight", "\n") + local output = utils.split_lines(vim.fn.execute "highlight") local hl_groups = {} for _, v in ipairs(output) do if v ~= "" then diff --git a/lua/telescope/previewers/utils.lua b/lua/telescope/previewers/utils.lua index 076bab4362..b430e5a509 100644 --- a/lua/telescope/previewers/utils.lua +++ b/lua/telescope/previewers/utils.lua @@ -5,12 +5,14 @@ local conf = require("telescope.config").values local Job = require "plenary.job" local Path = require "plenary.path" +local telescope_utils = require "telescope.utils" + local utils = {} local detect_from_shebang = function(p) local s = p:readbyterange(0, 256) if s then - local lines = vim.split(s, "\n") + local lines = telescope_utils.split_lines(s) return vim.filetype.match { contents = lines } end end @@ -24,7 +26,7 @@ end local detect_from_modeline = function(p) local s = p:readbyterange(-256, 256) if s then - local lines = vim.split(s, "\n") + local lines = telescope_utils.split_lines(s) local idx = lines[#lines] ~= "" and #lines or #lines - 1 if idx >= 1 then return parse_modeline(lines[idx]) diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 55d93698d3..c5ce0c233e 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -741,4 +741,16 @@ utils.reverse_table = function(input_table) return temp_table end +utils.split_lines = (function() + if utils.iswin then + return function(s, opts) + return vim.split(s, "\r?\n", opts) + end + else + return function(s, opts) + return vim.split(s, "\n", opts) + end + end +end)() + return utils diff --git a/lua/tests/automated/utils_spec.lua b/lua/tests/automated/utils_spec.lua index 490ce3e251..2734441f29 100644 --- a/lua/tests/automated/utils_spec.lua +++ b/lua/tests/automated/utils_spec.lua @@ -353,3 +353,46 @@ describe("path_tail", function() end) end end) + +describe("split_lines", function() + local function read_file(path) + local file = io.open(path, "rb") + if not file then + return nil + end + local content = file:read "*a" + file:close() + return content + end + + local newline = [[./lua/tests/fixtures/split_lines/newline.txt]] + local carriage_newline = [[./lua/tests/fixtures/split_lines/carriage_newline.txt]] + local expect = { + "", + "", + "line3 of the file", + "", + "line5 of the file", + "", + "", + "line8 of the file, last line of file", + "", + } + + if jit and jit.os:lower() == "windows" then + describe("handles files on Windows", function() + it("reads file with newline only", function() + assert.are.same(expect, utils.split_lines(read_file(newline))) + end) + it("reads file with carriage return and newline", function() + assert.are.same(expect, utils.split_lines(read_file(carriage_newline))) + end) + end) + else + describe("handles files on non Windows environment", function() + it("reads file with newline only", function() + assert.are.same(expect, utils.split_lines(read_file(newline))) + end) + end) + end +end) diff --git a/lua/tests/fixtures/split_lines/carriage_newline.txt b/lua/tests/fixtures/split_lines/carriage_newline.txt new file mode 100644 index 0000000000..682ff70c9b --- /dev/null +++ b/lua/tests/fixtures/split_lines/carriage_newline.txt @@ -0,0 +1,8 @@ + + +line3 of the file + +line5 of the file + + +line8 of the file, last line of file diff --git a/lua/tests/fixtures/split_lines/newline.txt b/lua/tests/fixtures/split_lines/newline.txt new file mode 100644 index 0000000000..682ff70c9b --- /dev/null +++ b/lua/tests/fixtures/split_lines/newline.txt @@ -0,0 +1,8 @@ + + +line3 of the file + +line5 of the file + + +line8 of the file, last line of file