feat: Support Line Column in file pickers

Original issue: When selecting files in telescope iti is impossible to
enter paths from compiler that includes cursor locations, you need to clear it from the path.

This commit fixes the problem by stripping out the location from path
(important: only for file pickes) and using this location to set the
cursor when openning the file and highlight the line or poisition in the
previewer.

This feature requires a new option for a picker, whichi for now is basically
enabling location stripping but it is helpful for any file picker
builtin or external one.

By default equals `false` becuase most of pickers like live grep,
current buffer fuzy are messed up with locations stripping. It is only
useful for file searches.

Added test suite that covers alghoritm of stripping the location to the
`utils_spec.lua`
This commit is contained in:
Dmitriy Kovalenko
2023-11-18 18:46:16 +01:00
parent 18774ec792
commit 0dfe9e1edf
6 changed files with 142 additions and 2 deletions
+27 -2
View File
@@ -4,6 +4,7 @@ local utils = require "telescope.utils"
local putils = require "telescope.previewers.utils"
local Previewer = require "telescope.previewers.previewer"
local conf = require("telescope.config").values
local global_state = require "telescope.state"
local pscan = require "plenary.scandir"
@@ -345,8 +346,6 @@ previewers.new_buffer_previewer = function(opts)
local old_bufs = {}
local bufname_table = {}
local global_state = require "telescope.state"
local preview_window_id
local function get_bufnr(self)
@@ -489,6 +488,29 @@ end
previewers.cat = defaulter(function(opts)
opts = opts or {}
local cwd = opts.cwd or vim.loop.cwd()
local function jump_to_line(bufnr, winid)
pcall(vim.api.nvim_buf_clear_namespace, bufnr, ns_previewer, 0, -1)
local location = global_state.get_global_key "prompt_location"
if location and location.row > 0 then
local highlight_range = location.col and location.col > 0 and { location.col - 1, location.col } or { 0, -1 }
pcall(
vim.api.nvim_buf_add_highlight,
bufnr,
ns_previewer,
"TelescopePreviewLine",
location.row - 1,
highlight_range[1],
highlight_range[2]
)
pcall(vim.api.nvim_win_set_cursor, winid, { location.row, location.col })
vim.api.nvim_buf_call(bufnr, function()
vim.cmd "norm! zz"
end)
end
end
return previewers.new_buffer_previewer {
title = "File Preview",
dyn_title = function(_, entry)
@@ -509,6 +531,9 @@ previewers.cat = defaulter(function(opts)
winid = self.state.winid,
preview = opts.preview,
file_encoding = opts.file_encoding,
callback = function(bufnr)
jump_to_line(bufnr, self.state.winid)
end,
})
end,
}