diff options
| author | caojoshua <33404808+caojoshua@users.noreply.github.com> | 2021-07-08 01:30:44 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-08 10:30:44 +0200 |
| commit | d5a8e48aa632508f5d7d6d428244eaf150adad25 (patch) | |
| tree | 7d0e7ccdfb9f4c01713c08e42bcb4a6993076a0f /lua | |
| parent | 38907ce7d74f26d123bfbb8ecf55bc9616b5ece5 (diff) | |
Consistent filepath display and code cleanup. (#839)
BREAKING CHANGE: see :help telescope.changelog-839 for more information
Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/telescope/builtin/files.lua | 2 | ||||
| -rw-r--r-- | lua/telescope/builtin/init.lua | 10 | ||||
| -rw-r--r-- | lua/telescope/builtin/internal.lua | 2 | ||||
| -rw-r--r-- | lua/telescope/builtin/lsp.lua | 9 | ||||
| -rw-r--r-- | lua/telescope/config.lua | 21 | ||||
| -rw-r--r-- | lua/telescope/deprecated.lua | 14 | ||||
| -rw-r--r-- | lua/telescope/make_entry.lua | 104 | ||||
| -rw-r--r-- | lua/telescope/utils.lua | 40 |
8 files changed, 103 insertions, 99 deletions
diff --git a/lua/telescope/builtin/files.lua b/lua/telescope/builtin/files.lua index 08eb4dd..29c8df5 100644 --- a/lua/telescope/builtin/files.lua +++ b/lua/telescope/builtin/files.lua @@ -492,7 +492,7 @@ files.current_buffer_tags = function(opts) return files.tags(vim.tbl_extend("force", { prompt_title = 'Current Buffer Tags', only_current_file = true, - hide_filename = true, + path_display = 'hidden', }, opts)) end diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 0588997..7cce3d9 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -118,8 +118,6 @@ builtin.current_buffer_fuzzy_find = require('telescope.builtin.files').current_b ---@param opts table: options to pass to the picker ---@field ctags_file string: specify a particular ctags file to use ---@field show_line boolean: if true, shows the content of the line the tag is found on in the picker (default is true) ----@field shorten_path boolean: if true, makes file paths shown in picker use one letter for folders (default is true) ----@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) builtin.tags = require('telescope.builtin.files').tags --- Lists all of the tags for the currently open buffer, with a preview @@ -289,8 +287,6 @@ builtin.spell_suggest = require('telescope.builtin.internal').spell_suggest --- Lists the tag stack for the current window, jumps to tag on `<cr>` ---@param opts table: options to pass to the picker ----@field shorten_path boolean: if true, makes file paths shown in picker use one letter for folders (default is true) ----@field hide_filename boolean: if true, hides the name of the file in the current picker (default is true) builtin.tagstack = require('telescope.builtin.internal').tagstack --- Lists items from Vim's jumplist, jumps to location on `<cr>` @@ -305,7 +301,6 @@ builtin.jumplist = require('telescope.builtin.internal').jumplist --- Lists LSP references for word under the cursor, jumps to reference on `<cr>` ---@param opts table: options to pass to the picker ----@field shorten_path boolean: if true, makes file paths shown in picker use one letter for folders (default is false) builtin.lsp_references = require('telescope.builtin.lsp').references --- Goto the definition of the word under the cursor, if there's only one, otherwise show all options in Telescope @@ -336,9 +331,7 @@ builtin.lsp_document_symbols = require('telescope.builtin.lsp').document_symbols --- - Default keymaps: --- - `<C-l>`: show autocompletion menu to prefilter your query by type of symbol you want to see (i.e. `:variable:`) ---@param opts table: options to pass to the picker ----@field shorten_path boolean: if true, makes file paths shown in picker use one letter for folders (default is false) ---@field ignore_filename string: file(s) to ignore ----@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) ---@field symbols string|table: filter results by symbol kind(s) builtin.lsp_workspace_symbols = require('telescope.builtin.lsp').workspace_symbols @@ -346,7 +339,6 @@ builtin.lsp_workspace_symbols = require('telescope.builtin.lsp').workspace_symbo --- - Default keymaps: --- - `<C-l>`: show autocompletion menu to prefilter your query by type of symbol you want to see (i.e. `:variable:`) ---@param opts table: options to pass to the picker ----@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) builtin.lsp_dynamic_workspace_symbols = require('telescope.builtin.lsp').dynamic_workspace_symbols --- Lists LSP diagnostics for the current buffer @@ -355,7 +347,6 @@ builtin.lsp_dynamic_workspace_symbols = require('telescope.builtin.lsp').dynamic --- - Default keymaps: --- - `<C-l>`: show autocompletion menu to prefilter your query with the diagnostic you want to see (i.e. `:warning:`) ---@param opts table: options to pass to the picker ----@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) ---@field severity string|number: filter diagnostics by severity name (string) or id (number) ---@field severity_limit string|number: keep diagnostics equal or more severe wrt severity name (string) or id (number) ---@field severity_bound string|number: keep diagnostics equal or less severe wrt severity name (string) or id (number) @@ -369,7 +360,6 @@ builtin.lsp_document_diagnostics = require('telescope.builtin.lsp').diagnostics --- - Default keymaps: --- - `<C-l>`: show autocompletion menu to prefilter your query with the diagnostic you want to see (i.e. `:warning:`) ---@param opts table: options to pass to the picker ----@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false) ---@field severity string|number: filter diagnostics by severity name (string) or id (number) ---@field severity_limit string|number: keep diagnostics equal or more severe wrt severity name (string) or id (number) ---@field severity_bound string|number: keep diagnostics equal or less severe wrt severity name (string) or id (number) diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index d53776d..9df2637 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -19,7 +19,7 @@ local internal = {} -- vim.fn.setreg("+", "nnoremap $TODO :lua require('telescope.builtin').<whatever>()<CR>") -- TODO: Can we just do the names instead? internal.builtin = function(opts) - opts.hide_filename = utils.get_default(opts.hide_filename, true) + opts.path_display = utils.get_default(opts.path_display, "hidden") opts.ignore_filename = utils.get_default(opts.ignore_filename, true) opts.include_extensions = utils.get_default(opts.include_extensions, false) diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 8bda217..a238796 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -15,8 +15,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 } @@ -250,8 +248,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 @@ -283,7 +279,6 @@ lsp.workspace_symbols = function(opts) end opts.ignore_filename = utils.get_default(opts.ignore_filename, false) - opts.hide_filename = utils.get_default(opts.hide_filename, false) pickers.new(opts, { prompt_title = 'LSP Workspace Symbols', @@ -337,7 +332,7 @@ lsp.diagnostics = function(opts) return end - opts.hide_filename = utils.get_default(opts.hide_filename, true) + opts.path_display = utils.get_default(opts.path_display, 'hidden') pickers.new(opts, { prompt_title = 'LSP Document Diagnostics', finder = finders.new_table { @@ -354,7 +349,7 @@ end lsp.workspace_diagnostics = function(opts) opts = utils.get_default(opts, {}) - opts.hide_filename = utils.get_default(opts.hide_filename, false) + opts.path_display = utils.get_default(opts.path_display, {}) opts.prompt_title = 'LSP Workspace Diagnostics' opts.get_all = true lsp.diagnostics(opts) diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua index 01d3bc9..dd3d453 100644 --- a/lua/telescope/config.lua +++ b/lua/telescope/config.lua @@ -177,6 +177,21 @@ local telescope_defaults = { Default: true]] }, + path_display = { {}, [[ + Determines how file paths are displayed + + path_display can be set to an array with a combination of: + - "hidden" hide file names + - "tail" only display the file name, and not the path + - "absolute" display absolute paths + - "shorten" only display the first character of each directory in + the path + + path_display can also be set to 'hidden' string to hide file names + + Default: {}]] + }, + borderchars = { { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } }, get_status_text = { @@ -247,13 +262,15 @@ local telescope_defaults = { Into your config. - otherwise, just set the mapping to the function that you want it to be. + otherwise, just set the mapping to the function that you want it to + be. ..., ["<C-i>"] = require('telescope.actions').select_default, ..., - If the function you want is part of `telescope.actions`, then you can simply give a string. + If the function you want is part of `telescope.actions`, then you can + simply give a string. For example, the previous option is equivalent to: ..., diff --git a/lua/telescope/deprecated.lua b/lua/telescope/deprecated.lua index 0d98c1d..ff33a07 100644 --- a/lua/telescope/deprecated.lua +++ b/lua/telescope/deprecated.lua @@ -8,6 +8,20 @@ deprecated.picker_window_options = function(opts) -- Deprecated: PR:922, 2021/06/25 -- Can be removed in a few weeks. + if opts.shorten_path then + table.insert(messages, + "'opts.shorten_path' is no longer valid. Please use 'opts.path_display' instead. " .. + "Please See ':help telescope.changelog-839'" + ) + end + + if opts.hide_filename then + table.insert(messages, + "'opts.hide_filename' is no longer valid. Please use 'opts.path_display' instead. " .. + "Please See ':help telescope.changelog-839'" + ) + end + if opts.width then table.insert(messages, "'opts.width' is no longer valid. Please use 'layout_config.width' instead") end diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index c451805..cfe6509 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -5,8 +5,6 @@ local strings = require('plenary.strings') local Path = require('plenary.path') -local get_default = utils.get_default - local treesitter_type_highlight = { ["associated"] = "TSConstant", ["constant"] = "TSConstant", @@ -68,17 +66,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_path(opts, entry.value) display, hl_group = utils.transform_devicons(entry.value, display, disable_devicons) @@ -136,7 +130,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) @@ -145,7 +138,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 @@ -188,12 +180,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_path(opts, entry.filename) local coordinates = "" if not disable_coordinates then @@ -290,7 +277,6 @@ end function make_entry.gen_from_quickfix(opts) opts = opts or {} - opts.tail_path = get_default(opts.tail_path, true) local displayer = entry_display.create { separator = "▏", @@ -302,15 +288,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_path(opts, entry.filename) local line_info = {table.concat({entry.lnum, entry.col}, ":"), "TelescopeResultsLineNr"} @@ -379,17 +357,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_path(opts, entry.filename) if opts.show_line then -- show inline line info filename = filename .. " [" ..entry.lnum .. ":" .. entry.col .. "]" @@ -463,12 +431,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_path(opts, entry.filename) local icon, hl_group = utils.get_devicons(entry.filename, disable_devicons) @@ -855,12 +818,15 @@ function make_entry.gen_from_ctags(opts) local current_file = path.normalize(vim.fn.expand('%'), cwd) local display_items = { - { width = 30 }, { remaining = true }, } + local hidden = utils.is_path_hidden(opts) + if not hidden then + table.insert(display_items, 1, { width = 30 }) + end if opts.show_line then - table.insert(display_items, 2, { width = 30 }) + table.insert(display_items, 1, { width = 30 }) end local displayer = entry_display.create { @@ -869,25 +835,25 @@ 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_path(opts, entry.filename) local scode if opts.show_line then scode = entry.scode end - return displayer { - filename, - entry.tag, - scode, - } + if hidden then + return displayer { + entry.tag, + scode, + } + else + return displayer { + filename, + entry.tag, + scode, + } + end end return function(line) @@ -932,7 +898,6 @@ end function make_entry.gen_from_lsp_diagnostics(opts) opts = opts or {} - opts.tail_path = utils.get_default(opts.tail_path, true) local lsp_type_diagnostic = vim.lsp.protocol.DiagnosticSeverity local signs @@ -956,22 +921,14 @@ function make_entry.gen_from_lsp_diagnostics(opts) { remaining = true } } local line_width = utils.get_default(opts.line_width, 45) - if not opts.hide_filename then table.insert(layout, 2, {width = line_width}) end + if not utils.is_path_hidden(opts) then table.insert(layout, 2, {width = line_width}) end local displayer = entry_display.create { separator = "▏", items = layout } 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_path(opts, entry.filename) -- add styling of entries local pos = string.format("%4d:%2d", entry.lnum, entry.col) @@ -1156,7 +1113,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 = "▏", @@ -1167,15 +1123,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_path(opts, entry.filename) local line_info = {table.concat({entry.lnum, entry.col}, ":"), "TelescopeResultsLineNr"} diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 3938bae..b8d2b6b 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -275,6 +275,46 @@ utils.path_tail = (function() end end)() +utils.is_path_hidden = function(opts, path_display) + path_display = path_display or utils.get_default(opts.path_display, require('telescope.config').values.path_display) + + return path_display == nil or path_display == "hidden" or + type(path_display) ~= "table" or vim.tbl_contains(path_display, "hidden") +end + +utils.transform_path = function(opts, path) + local path_display = utils.get_default(opts.path_display, require('telescope.config').values.path_display) + + if utils.is_path_hidden(nil, path_display) then + return '' + end + + local transformed_path = path + + if vim.tbl_contains(path_display, "tail") then + transformed_path = utils.path_tail(transformed_path) + else + if not vim.tbl_contains(path_display, "absolute") then + local cwd + if opts.cwd then + cwd = opts.cwd + if not vim.in_fast_event() then + cwd = vim.fn.expand(opts.cwd) + end + else + cwd = vim.loop.cwd(); + end + transformed_path = pathlib.make_relative(transformed_path, cwd) + end + + if vim.tbl_contains(path_display, "shorten") 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) |
