diff options
| author | anott03 <amitav_nott@ryecountryday.org> | 2021-07-14 13:25:00 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-14 19:25:00 +0200 |
| commit | df579bac425e4b6b69a0c8e694b154610cd5420b (patch) | |
| tree | fc4eb1bcbeeabe199bd997757bd72892fb4c07a0 /lua/telescope/builtin | |
| parent | a4896e5ef39a8006a8251d48d4cf24aac81a4f94 (diff) | |
refactor: move from telescope.path to plenary.path (#473)
This will deprecate telescope.path, we will remove it soon. Please move over to plenary.path
Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
Diffstat (limited to 'lua/telescope/builtin')
| -rw-r--r-- | lua/telescope/builtin/files.lua | 68 | ||||
| -rw-r--r-- | lua/telescope/builtin/init.lua | 1 | ||||
| -rw-r--r-- | lua/telescope/builtin/internal.lua | 11 |
3 files changed, 60 insertions, 20 deletions
diff --git a/lua/telescope/builtin/files.lua b/lua/telescope/builtin/files.lua index 9aee76a..d1e80f7 100644 --- a/lua/telescope/builtin/files.lua +++ b/lua/telescope/builtin/files.lua @@ -47,10 +47,9 @@ files.live_grep = function(opts) end, vim.api.nvim_list_bufs()) if not next(bufnrs) then return end - local tele_path = require'telescope.path' for _, bufnr in ipairs(bufnrs) do local file = vim.api.nvim_buf_get_name(bufnr) - table.insert(filelist, tele_path.make_relative(file, opts.cwd)) + table.insert(filelist, Path:new(file):make_relative(opts.cwd)) end elseif search_dirs then for i, path in ipairs(search_dirs) do @@ -242,6 +241,10 @@ end files.file_browser = function(opts) opts = opts or {} + local is_dir = function(value) + return value:sub(-1, -1) == os_sep + end + opts.depth = opts.depth or 1 opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd() opts.new_finder = opts.new_finder or function(path) @@ -258,18 +261,53 @@ files.file_browser = function(opts) }) table.insert(data, 1, '..' .. os_sep) - return finders.new_table { - results = data, - entry_maker = (function() - local tele_path = require'telescope.path' - local gen = make_entry.gen_from_file(opts) - return function(entry) - local tmp = gen(entry) - tmp.ordinal = tele_path.make_relative(entry, opts.cwd) - return tmp + local maker = function() + local mt = {} + mt.cwd = opts.cwd + mt.display = function(entry) + local hl_group + local display = utils.transform_path(opts, entry.value) + if is_dir(entry.value) then + display = display .. os_sep + if not opts.disable_devicons then + display = (opts.dir_icon or "") .. " " .. display + hl_group = "Default" + end + else + display, hl_group = utils.transform_devicons(entry.value, display, opts.disable_devicons) end - end)() - } + + if hl_group then + return display, { { {1, 3}, hl_group } } + else + return display + end + end + + mt.__index = function(t, k) + local raw = rawget(mt, k) + if raw then return raw end + + if k == "path" then + local retpath = Path:new({t.cwd, t.value}):absolute() + if not vim.loop.fs_access(retpath, "R", nil) then + retpath = t.value + end + if is_dir(t.value) then retpath = retpath .. os_sep end + return retpath + end + + return rawget(t, rawget({ value = 1 }, k)) + end + + return function(line) + local tbl = {line} + tbl.ordinal = Path:new(line):make_relative(opts.cwd) + return setmetatable(tbl, mt) + end + end + + return finders.new_table { results = data, entry_maker = maker() } end pickers.new(opts, { @@ -279,7 +317,7 @@ files.file_browser = function(opts) sorter = conf.file_sorter(opts), attach_mappings = function(prompt_bufnr, map) action_set.select:replace_if(function() - return action_state.get_selected_entry().path:sub(-1) == os_sep + return is_dir(action_state.get_selected_entry().path) end, function() local new_cwd = vim.fn.expand(action_state.get_selected_entry().path:sub(1, -2)) local current_picker = action_state.get_current_picker(prompt_bufnr) @@ -299,7 +337,7 @@ files.file_browser = function(opts) end local fpath = current_picker.cwd .. os_sep .. file - if string.sub(fpath, -1) ~= os_sep then + if not is_dir(fpath) then actions.close(prompt_bufnr) Path:new(fpath):touch({ parents = true }) vim.cmd(string.format(':e %s', fpath)) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 7cce3d9..8e7f5ec 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -101,6 +101,7 @@ builtin.fd = builtin.find_files ---@param opts table: options to pass to the picker ---@field cwd string: directory path to browse (default is cwd) ---@field depth number: file tree depth to display (default is 1) +---@field dir_icon string: change the icon for a directory. default: builtin.file_browser = require('telescope.builtin.files').file_browser --- Lists function names, variables, and other symbols from treesitter queries diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index ef34ca9..b157ee2 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -3,7 +3,7 @@ local action_set = require('telescope.actions.set') local action_state = require('telescope.actions.state') local finders = require('telescope.finders') local make_entry = require('telescope.make_entry') -local path = require('telescope.path') +local Path = require('plenary.path') local pickers = require('telescope.pickers') local previewers = require('telescope.previewers') local sorters = require('telescope.sorters') @@ -135,7 +135,7 @@ internal.symbols = function(opts) local results = {} for _, source in ipairs(sources) do - local data = vim.fn.json_decode(path.read_file(source)) + local data = vim.fn.json_decode(Path:new(source):read()) for _, entry in ipairs(data) do table.insert(results, entry) end @@ -346,8 +346,9 @@ end internal.vim_options = function(opts) -- Load vim options. - local vim_opts = loadfile(utils.data_directory() .. path.separator .. 'options' .. - path.separator .. 'options.lua')().options + local vim_opts = loadfile( + Path:new({utils.data_directory(), 'options', 'options.lua'}):absolute() + )().options pickers.new(opts, { prompt = 'options', @@ -449,7 +450,7 @@ 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.read_file(file), '\n', true) + local lines = vim.split(Path:new(file):read(), '\n', true) for _, line in ipairs(lines) do -- TODO: also ignore tagComment starting with ';' if not line:match'^!_TAG_' then |
