From 0b20af5288aa81d0ffeafa9609e5afa69f19cd2e Mon Sep 17 00:00:00 2001 From: caojoshua Date: Tue, 25 May 2021 00:27:25 -0700 Subject: [PATCH] path_display options as list of raw strings instead of enums. --- lua/telescope/config.lua | 6 +----- lua/telescope/make_entry.lua | 16 ++++++++-------- lua/telescope/types.lua | 23 ----------------------- lua/telescope/utils.lua | 28 ++++++++++++++++++++-------- 4 files changed, 29 insertions(+), 44 deletions(-) delete mode 100644 lua/telescope/types.lua diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua index d803353acb..5800146e64 100644 --- a/lua/telescope/config.lua +++ b/lua/telescope/config.lua @@ -1,5 +1,3 @@ -local types = require('telescope.types') - -- Keep the values around between reloads _TelescopeConfigurationValues = _TelescopeConfigurationValues or {} @@ -115,9 +113,7 @@ function config.set_defaults(defaults) set("border", {}) set("borderchars", { '─', '│', '─', '│', '╭', '╮', '╯', '╰'}) - set(types.path_display_options.HIDE_PATH, false) - set(types.path_display_options.SHORTEN_PATH, false) - set(types.path_display_options.TAIL_PATH, false) + set("path_display", {}) set("get_status_text", function(self) local xx = (self.stats.processed or 0) - (self.stats.filtered or 0) diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 20b04c98bd..bf78b2006a 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -80,7 +80,7 @@ do mt_file_entry.cwd = cwd mt_file_entry.display = function(entry) local hl_group - local display = utils.transform_filepath(opts, entry.value) + local display = utils.transform_path(opts, entry.value) display, hl_group = utils.transform_devicons(entry.value, display, disable_devicons) @@ -188,7 +188,7 @@ do cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()), display = function(entry) - local display_filename = utils.transform_filepath(opts, entry.filename) + local display_filename = utils.transform_path(opts, entry.filename) local coordinates = "" if not disable_coordinates then @@ -295,7 +295,7 @@ function make_entry.gen_from_quickfix(opts) } local make_display = function(entry) - local filename = utils.transform_filepath(opts, entry.filename) + local filename = utils.transform_path(opts, entry.filename) local line_info = {table.concat({entry.lnum, entry.col}, ":"), "TelescopeResultsLineNr"} @@ -364,7 +364,7 @@ function make_entry.gen_from_lsp_symbols(opts) )[1] or '' msg = vim.trim(msg) else - local filename = utils.transform_filepath(opts, entry.filename) + local filename = utils.transform_path(opts, entry.filename) if opts.show_line then -- show inline line info filename = filename .. " [" ..entry.lnum .. ":" .. entry.col .. "]" @@ -438,7 +438,7 @@ function make_entry.gen_from_buffer(opts) local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()) local make_display = function(entry) - local display_bufname = utils.transform_filepath(opts, entry.filename) + local display_bufname = utils.transform_path(opts, entry.filename) local icon, hl_group = utils.get_devicons(entry.filename, disable_devicons) @@ -838,7 +838,7 @@ function make_entry.gen_from_ctags(opts) } local make_display = function(entry) - local filename = utils.transform_filepath(opts, entry.filename) + local filename = utils.transform_path(opts, entry.filename) local scode if opts.show_line then @@ -922,7 +922,7 @@ function make_entry.gen_from_lsp_diagnostics(opts) } local make_display = function(entry) - local filename = utils.transform_filepath(opts, entry.filename) + local filename = utils.transform_path(opts, entry.filename) -- add styling of entries local pos = string.format("%4d:%2d", entry.lnum, entry.col) @@ -1117,7 +1117,7 @@ function make_entry.gen_from_jumplist(opts) } local make_display = function(entry) - local filename = utils.transform_filepath(opts, entry.filename) + local filename = utils.transform_path(opts, entry.filename) local line_info = {table.concat({entry.lnum, entry.col}, ":"), "TelescopeResultsLineNr"} diff --git a/lua/telescope/types.lua b/lua/telescope/types.lua deleted file mode 100644 index c14ffc5adc..0000000000 --- a/lua/telescope/types.lua +++ /dev/null @@ -1,23 +0,0 @@ -local types = {} - --- convert numbered index table into an enum --- --- example: --- enum { "foo", "bar" } --- returns: --- { foo = 1, bar = 2 } -local enum = function(table) - for i = 1, #table do - table[table[i]] = i - table[i] = nil - end - return table -end - -types.path_display_options = enum { - "HIDE_PATH", - "SHORTEN_PATH", - "TAIL_PATH" -} - -return types diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index defd00f47e..7cf018cac2 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -2,7 +2,6 @@ local has_devicons, devicons = pcall(require, 'nvim-web-devicons') local pathlib = require('telescope.path') local Job = require('plenary.job') -local types = require('telescope.types') local utils = {} @@ -41,6 +40,15 @@ utils.reversed_ipairs = function(t) return reversedipairsiter, t, #t + 1 end +utils.contains = function(t, val) + for _, tval in pairs(t) do + if tval == val then + return true + end + end + return false +end + utils.default_table_mt = { __index = function(t, k) local obj = {} @@ -148,23 +156,27 @@ utils.path_tail = (function() end end)() -utils.transform_filepath = function(opts, path) +utils.transform_path = function(opts, path) local config = require('telescope.config').values - local types = types.path_display_options + local path_display = utils.get_default(opts.path_display, config.path_display) - if path == nil or utils.get_default(opts[types.HIDE_PATH], config[types.HIDE_PATH]) then + if path == nil or path_display == nil or type(path_display) ~= "table" or + utils.contains(path_display, "hidden") or path_display == "hidden" then return '' end local transformed_path = path - if utils.get_default(opts[types.TAIL_PATH], config[types.TAIL_PATH]) then + if utils.contains(path_display, "tail") then transformed_path = utils.path_tail(transformed_path) else - local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()) - transformed_path = pathlib.make_relative(transformed_path, cwd) + -- TODO: explicitly make paths absolute when the option is set + if not utils.contains(path_display, "absolute") then + local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()) + transformed_path = pathlib.make_relative(transformed_path, cwd) + end - if utils.get_default(opts[types.SHORTEN_PATH], config[types.SHORTEN_PATH]) then + if utils.contains(path_display, "shorten") then transformed_path = pathlib.shorten(transformed_path) end end