Skip to content

Commit

Permalink
path_display options as list of raw strings instead of enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
caojoshua committed May 25, 2021
1 parent 78ed5ec commit 0b20af5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 44 deletions.
6 changes: 1 addition & 5 deletions lua/telescope/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local types = require('telescope.types')

-- Keep the values around between reloads
_TelescopeConfigurationValues = _TelescopeConfigurationValues or {}

Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"}

Expand Down Expand Up @@ -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 .. "]"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"}

Expand Down
23 changes: 0 additions & 23 deletions lua/telescope/types.lua

This file was deleted.

28 changes: 20 additions & 8 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0b20af5

Please sign in to comment.