Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added smart_path option for file name getters. #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lua/telescope/builtin/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ local conf = require('telescope.config').values
local lsp = {}

lsp.references = function(opts)
opts.shorten_path = utils.get_default(opts.shorten_path, true)

local params = vim.lsp.util.make_position_params()
params.context = { includeDeclaration = true }

Expand Down Expand Up @@ -243,8 +241,6 @@ lsp.range_code_actions = function(opts)
end

lsp.workspace_symbols = function(opts)
opts.shorten_path = utils.get_default(opts.shorten_path, true)

local params = {query = opts.query or ''}
local results_lsp, err = vim.lsp.buf_request_sync(0, "workspace/symbol", params, opts.timeout or 10000)
if err then
Expand Down
7 changes: 7 additions & 0 deletions lua/telescope/config.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local types = require('telescope.types')

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

Expand Down Expand Up @@ -113,6 +115,11 @@ 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(types.path_display_options.SMART_PATH, false)

set("get_status_text", function(self)
local xx = (self.stats.processed or 0) - (self.stats.filtered or 0)
local yy = self.stats.processed or 0
Expand Down
76 changes: 9 additions & 67 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,13 @@ do
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())

local disable_devicons = opts.disable_devicons
local shorten_path = opts.shorten_path

local mt_file_entry = {}

mt_file_entry.cwd = cwd
mt_file_entry.display = function(entry)
local hl_group
local display = path.make_relative(entry.value, cwd)
if shorten_path then
display = utils.path_shorten(display)
end
local display = utils.transform_filepath(opts, entry.value)

display, hl_group = utils.transform_devicons(entry.value, display, disable_devicons)

Expand Down Expand Up @@ -142,7 +138,6 @@ do
end

--- Special options:
--- - shorten_path: make the path appear short
--- - disable_coordinates: Don't show the line & row numbers
--- - only_sort_text: Only sort via the text. Ignore filename and other items
function make_entry.gen_from_vimgrep(opts)
Expand All @@ -151,7 +146,6 @@ do
opts = opts or {}

local disable_devicons = opts.disable_devicons
local shorten_path = opts.shorten_path
local disable_coordinates = opts.disable_coordinates
local only_sort_text = opts.only_sort_text

Expand Down Expand Up @@ -194,12 +188,7 @@ do
cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd()),

display = function(entry)
local display_filename
if shorten_path then
display_filename = utils.path_shorten(entry.filename)
else
display_filename = entry.filename
end
local display_filename = utils.transform_filepath(opts, entry.filename)

local coordinates = ""
if not disable_coordinates then
Expand Down Expand Up @@ -306,15 +295,7 @@ function make_entry.gen_from_quickfix(opts)
}

local make_display = function(entry)
local filename
if not opts.hide_filename then
filename = entry.filename
if opts.tail_path then
filename = utils.path_tail(filename)
elseif opts.shorten_path then
filename = utils.path_shorten(filename)
end
end
local filename = utils.transform_filepath(opts, entry.filename)

local line_info = {table.concat({entry.lnum, entry.col}, ":"), "TelescopeResultsLineNr"}

Expand Down Expand Up @@ -383,17 +364,7 @@ function make_entry.gen_from_lsp_symbols(opts)
)[1] or ''
msg = vim.trim(msg)
else
local filename = ""
opts.tail_path = get_default(opts.tail_path, true)

if not opts.hide_filename then -- hide the filename entirely
filename = entry.filename
if opts.tail_path then
filename = utils.path_tail(filename)
elseif opts.shorten_path then
filename = utils.path_shorten(filename)
end
end
local filename = utils.transform_filepath(opts, entry.filename)

if opts.show_line then -- show inline line info
filename = filename .. " [" ..entry.lnum .. ":" .. entry.col .. "]"
Expand Down Expand Up @@ -467,12 +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
if opts.shorten_path then
display_bufname = path.shorten(entry.filename)
else
display_bufname = entry.filename
end
local display_bufname = utils.transform_filepath(opts, entry.filename)

local icon, hl_group = utils.get_devicons(entry.filename, disable_devicons)

Expand Down Expand Up @@ -872,14 +838,7 @@ function make_entry.gen_from_ctags(opts)
}

local make_display = function(entry)
local filename
if not opts.hide_filename then
if opts.shorten_path then
filename = path.shorten(entry.filename)
else
filename = entry.filename
end
end
local filename = utils.transform_filepath(opts, entry.filename)

local scode
if opts.show_line then
Expand Down Expand Up @@ -935,7 +894,7 @@ end

function make_entry.gen_from_lsp_diagnostics(opts)
opts = opts or {}
opts.tail_path = utils.get_default(opts.tail_path, true)
opts.tail_path = get_default(opts.tail_path, true)

local signs
if not opts.no_sign then
Expand Down Expand Up @@ -963,15 +922,7 @@ function make_entry.gen_from_lsp_diagnostics(opts)
}

local make_display = function(entry)
local filename
if not opts.hide_filename then
filename = entry.filename
if opts.tail_path then
filename = utils.path_tail(filename)
elseif opts.shorten_path then
filename = utils.path_shorten(filename)
end
end
local filename = utils.transform_filepath(opts, entry.filename)

-- add styling of entries
local pos = string.format("%4d:%2d", entry.lnum, entry.col)
Expand Down Expand Up @@ -1156,7 +1107,6 @@ end

function make_entry.gen_from_jumplist(opts)
opts = opts or {}
opts.tail_path = get_default(opts.tail_path, true)

local displayer = entry_display.create {
separator = "▏",
Expand All @@ -1167,15 +1117,7 @@ function make_entry.gen_from_jumplist(opts)
}

local make_display = function(entry)
local filename
if not opts.hide_filename then
filename = entry.filename
if opts.tail_path then
filename = utils.path_tail(filename)
elseif opts.shorten_path then
filename = utils.path_shorten(filename)
end
end
local filename = utils.transform_filepath(opts, entry.filename)

local line_info = {table.concat({entry.lnum, entry.col}, ":"), "TelescopeResultsLineNr"}

Expand Down
21 changes: 21 additions & 0 deletions lua/telescope/types.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local types = {}

-- convert numbered index table into an enum
--
-- example:
-- enum { "foo", "bar" }
-- returns:
-- { foo = 1, bar = 2 }
-- these are going to be option names. to enable, add <option> = true
local paths = {"hide_path", "shorten_path", "tail_path", "smart_path"}
local enum = function(table)
for i = 1, #table do
table[table[i]] = paths[i]
table[i] = nil
end
return table
end

types.path_display_options = enum {"HIDE_PATH", "SHORTEN_PATH", "TAIL_PATH", "SMART_PATH"}

return types
78 changes: 78 additions & 0 deletions lua/telescope/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -147,6 +148,83 @@ utils.path_tail = (function()
end
end)()

utils.slice = function(tbl, s, e)
local pos, new = 1, {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you actually want to copy all elements between s and e into a new table? Thats not great.

Maybe this plenary module can help https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/iterators.lua
No idea how it works tho. But i saw a range function. This is the spec file https://github.com/nvim-lua/plenary.nvim/blob/master/tests/plenary/iterators_spec.lua


for i = s, e do
new[pos] = tbl[i]
pos = pos + 1
end

return new
end



PATHS = {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be a global right?

utils.path_smart = function(initial_path)
local final = initial_path
if (table.getn(PATHS) ~= 0) then
local diff_counts = { 1, }
local dirs = vim.split(initial_path, "/")
for _, path in pairs(PATHS) do
local _dirs = vim.split(path, "/")
local l = table.getn(_dirs)
local differs = 0
for i = 0, l do
if (dirs[i] ~= _dirs[i]) then
differs = i
break
end
end
table.insert(diff_counts, differs)
end
local max = math.max(unpack(diff_counts))
local length = table.getn(dirs)
local final_table = utils.slice(dirs, max, length)
if (max == length) then
final_table = utils.slice(dirs, length - 1, length)
end
final = table.concat(final_table, "/")
else
local length = string.len(final)
final = utils.slice(initial_path, length - 1, length)
final = table.concat(final, "/")
Comment on lines +191 to +192

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are only using slice for create a string afterwards you should look for a different method.

Copy link
Author

@qualious qualious May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i figured that but i was like “i’m already using it 🤷🏻‍♀️“.

it’s a quick hack, not production ready at all. i’ll fix these if i’m going to open the pr on the main repo. i appreciate the feedback

edit: and i honestly wasn’t hopeful about the initial PR. i didn’t know we would get you involved

end
table.insert(PATHS, initial_path)
if (final == initial_path) then
return final
else
return "../" .. final
end
end

utils.transform_filepath = function(opts, path)
local config = require('telescope.config').values
local types = types.path_display_options

if path == nil or utils.get_default(opts[types.HIDE_PATH], config[types.HIDE_PATH]) then
return ''
end

local transformed_path = path

if utils.get_default(opts[types.TAIL_PATH], config[types.TAIL_PATH]) then
transformed_path = utils.path_tail(transformed_path)
elseif utils.get_default(opts[types.SMART_PATH], config[types.SMART_PATH]) then
transformed_path = utils.path_smart(transformed_path)
else
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())
transformed_path = pathlib.make_relative(transformed_path, cwd)

if utils.get_default(opts[types.SHORTEN_PATH], config[types.SHORTEN_PATH]) then
transformed_path = pathlib.shorten(transformed_path)
end
end

return transformed_path
end

-- local x = utils.make_default_callable(function(opts)
-- return function()
-- print(opts.example, opts.another)
Expand Down