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

feat(path_display): move lnum/col next to filename for filename_first #3200

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 17 additions & 7 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2622,19 +2622,29 @@ utils.path_expand({path}) *telescope.utils.path_expand()*
string


utils.transform_path({opts}, {path}) *telescope.utils.transform_path()*
utils.transform_path({opts}, {path}, {coordinates}) *telescope.utils.transform_path()*
Transform path is a util function that formats a path based on path_display
found in `opts` or the default value from config. It is meant to be used in
make_entry to have a uniform interface for builtins as well as extensions
utilizing the same user configuration Note: It is only supported inside
`make_entry`/`make_display` the use of this function outside of telescope
might yield to undefined behavior and will not be addressed by us
utilizing the same user configuration

Optionally can concatenate line and column number coordinates to the path
string when they are provided. For all path_display options besides
`filename_first`, the coordinates are appended to the end of the path. For
`filename_first`, the coordinates are appended to the end of the filename,
before the rest of the path. eg. `utils.lua:387:24 lua/telescope`

Note: It is only supported inside `make_entry`/`make_display` the use of
this function outside of telescope might yield to undefined behavior and
will not be addressed by us


Parameters: ~
{opts} (table) The opts the users passed into the picker. Might
contains a path_display key
{path} (string|nil) The path that should be formatted
{opts} (table) The opts the users passed into the picker.
Might contains a path_display key
{path} (string|nil) The path that should be formatted
{coordinates} (string|nil) Line and colunm numbers to be displayed
(eg. ':395:86')

Return: ~
string: path to be displayed
Expand Down
90 changes: 23 additions & 67 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@
--- TODO: Document something we call `entry_index`
---@brief ]]

-- this breaks tree-sitter-lua docgen
-- ---@class telescope.entry
-- ---@field value any
-- ---@field ordinal string
-- ---@field display string|function(entry: telescope.entry): string
-- ---@field filename string|nil
-- ---@field bufnr number|nil
-- ---@field lnum number|nil
-- ---@field col number|nil

local entry_display = require "telescope.pickers.entry_display"
local utils = require "telescope.utils"
local strings = require "plenary.strings"
local Path = require "plenary.path"

local treesitter_type_highlight = {
Expand Down Expand Up @@ -151,24 +160,11 @@ do

local cwd = utils.path_expand(opts.cwd or vim.loop.cwd())

local disable_devicons = opts.disable_devicons

local mt_file_entry = {}

mt_file_entry.cwd = cwd
mt_file_entry.display = function(entry)
local hl_group, icon
local display, path_style = utils.transform_path(opts, entry.value)

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

if hl_group then
local style = { { { 0, #icon + 1 }, hl_group } }
style = utils.merge_styles(style, path_style, #icon + 1)
return display, style
else
return display, path_style
end
return utils.create_path_display(entry, opts)
end

mt_file_entry.__index = function(t, k)
Expand Down Expand Up @@ -272,8 +268,6 @@ do
parse = parse_without_col
end

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

local execute_keys = {
Expand Down Expand Up @@ -309,38 +303,13 @@ do
end
end

local display_string = "%s%s%s"

mt_vimgrep_entry = {
cwd = utils.path_expand(opts.cwd or vim.loop.cwd()),

display = function(entry)
local display_filename, path_style = utils.transform_path(opts, entry.filename)

local coordinates = ":"
if not disable_coordinates then
if entry.lnum then
if entry.col then
coordinates = string.format(":%s:%s:", entry.lnum, entry.col)
else
coordinates = string.format(":%s:", entry.lnum)
end
end
end

local display, hl_group, icon = utils.transform_devicons(
entry.filename,
string.format(display_string, display_filename, coordinates, entry.text),
disable_devicons
)

if hl_group then
local style = { { { 0, #icon }, hl_group } }
style = utils.merge_styles(style, path_style, #icon + 1)
return display, style
else
return display, path_style
end
local display, path_style = utils.create_path_display(entry, opts)
display = string.format("%s:%s", display, entry.text)
return display, path_style
end,

__index = function(t, k)
Expand Down Expand Up @@ -458,8 +427,7 @@ function make_entry.gen_from_quickfix(opts)
local hidden = utils.is_path_hidden(opts)

local make_display = function(entry)
local display_filename, path_style = utils.transform_path(opts, entry.filename)
local display_string = string.format("%s:%d:%d", display_filename, entry.lnum, entry.col)
local display_string, path_style = utils.create_path_display(entry, opts)
if hidden then
display_string = string.format("%4d:%2d", entry.lnum, entry.col)
end
Expand Down Expand Up @@ -540,7 +508,7 @@ function make_entry.gen_from_lsp_symbols(opts)
msg,
}
else
local display_path, path_style = utils.transform_path(opts, entry.filename)
local display_path, path_style = utils.create_path_display(entry, opts)
return displayer {
{
display_path,
Expand Down Expand Up @@ -584,40 +552,27 @@ end
function make_entry.gen_from_buffer(opts)
opts = opts or {}

local disable_devicons = opts.disable_devicons

local icon_width = 0
if not disable_devicons then
local icon, _ = utils.get_devicons("fname", disable_devicons)
icon_width = strings.strdisplaywidth(icon)
end

local displayer = entry_display.create {
separator = " ",
items = {
{ width = opts.bufnr_width },
{ width = 4 },
{ width = icon_width },
{ remaining = true },
},
}

local cwd = utils.path_expand(opts.cwd or vim.loop.cwd())

local make_display = function(entry)
-- bufnr_width + modes + icon + 3 spaces + : + lnum
opts.__prefix = opts.bufnr_width + 4 + icon_width + 3 + 1 + #tostring(entry.lnum)
local display_bufname, path_style = utils.transform_path(opts, entry.filename)
local icon, hl_group = utils.get_devicons(entry.filename, disable_devicons)
local display, style = utils.create_path_display(entry, opts)

return displayer {
{ entry.bufnr, "TelescopeResultsNumber" },
{ entry.indicator, "TelescopeResultsComment" },
{ icon, hl_group },
{
display_bufname .. ":" .. entry.lnum,
display,
function()
return path_style
return style
end,
},
}
Expand Down Expand Up @@ -1049,7 +1004,7 @@ function make_entry.gen_from_ctags(opts)
}

local make_display = function(entry)
local display_path, path_style = utils.transform_path(opts, entry.filename)
local display_path, path_style = utils.create_path_display(entry, opts)

local scode
if opts.show_line then
Expand Down Expand Up @@ -1185,7 +1140,8 @@ function make_entry.gen_from_diagnostics(opts)
}

local make_display = function(entry)
local display_path, path_style = utils.transform_path(opts, entry.filename)
local display_path, path_style =
utils.create_path_display(entry, vim.tbl_extend("force", opts, { disable_coordinates = true }))

-- add styling of entries
local pos = string.format("%4d:%2d", entry.lnum, entry.col)
Expand Down Expand Up @@ -1361,7 +1317,7 @@ function make_entry.gen_from_git_status(opts)
local status_x = git_abbrev[x] or {}
local status_y = git_abbrev[y] or {}

local display_path, path_style = utils.transform_path(opts, entry.path)
local display_path, path_style = utils.create_path_display(entry, opts)

local empty_space = " "
return displayer {
Expand Down
Loading