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

Jumplist picker and jump to row/col on existing buffers. #813

Merged
merged 5 commits into from
May 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 5 additions & 6 deletions lua/telescope/actions/set.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,18 @@ action_set.edit = function(prompt_bufnr, command)
if entry_bufnr then
edit_buffer(command, entry_bufnr)
else

-- check if we didn't pick a different buffer
-- prevents restarting lsp server
if vim.api.nvim_buf_get_name(0) ~= filename or command ~= "edit" then
filename = path.normalize(vim.fn.fnameescape(filename), vim.loop.cwd())
vim.cmd(string.format("%s %s", command, filename))
end
end

if row and col then
local ok, err_msg = pcall(a.nvim_win_set_cursor, 0, {row, col})
if not ok then
log.debug("Failed to move to cursor:", err_msg, row, col)
end
if row and col then
local ok, err_msg = pcall(a.nvim_win_set_cursor, 0, {row, col})
if not ok then
log.debug("Failed to move to cursor:", err_msg, row, col)
elianiva marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ builtin.highlights = require('telescope.builtin.internal').highlights
builtin.autocommands = require('telescope.builtin.internal').autocommands
builtin.spell_suggest = require('telescope.builtin.internal').spell_suggest
builtin.tagstack = require('telescope.builtin.internal').tagstack
builtin.jumplist = require('telescope.builtin.internal').jumplist

builtin.lsp_references = require('telescope.builtin.lsp').references
builtin.lsp_definitions = require('telescope.builtin.lsp').definitions
Expand Down
24 changes: 24 additions & 0 deletions lua/telescope/builtin/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,30 @@ internal.tagstack = function(opts)
}):find()
end

internal.jumplist = function(opts)
opts = opts or {}
local jumplist = vim.api.nvim_eval('getjumplist()')[1]
smithbm2316 marked this conversation as resolved.
Show resolved Hide resolved

for _, value in ipairs(jumplist) do
value.text = ''
end

-- reverse the list
local sorted_jumplist = {}
for i = #jumplist, 1, -1 do
sorted_jumplist[#sorted_jumplist+1] = jumplist[i]
smithbm2316 marked this conversation as resolved.
Show resolved Hide resolved
end

pickers.new(opts, {
prompt_title = 'Jumplist',
elianiva marked this conversation as resolved.
Show resolved Hide resolved
finder = finders.new_table {
results = sorted_jumplist,
entry_maker = make_entry.gen_from_jumplist(opts),
},
previewer = previewers.vim_buffer_qflist.new(opts),
elianiva marked this conversation as resolved.
Show resolved Hide resolved
sorter = conf.generic_sorter(opts),
}):find()
end

local function apply_checks(mod)
for k, v in pairs(mod) do
Expand Down
52 changes: 52 additions & 0 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1130,5 +1130,57 @@ function make_entry.gen_from_git_status(opts)
end
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 = "▏",
items = {
{ width = 10 },
{ remaining = true },
}
}

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 line_info = {table.concat({entry.lnum, entry.col}, ":"), "TelescopeResultsLineNr"}

return displayer {
line_info,
filename,
}
end

return function(entry)
local filename = entry.filename or vim.api.nvim_buf_get_name(entry.bufnr)
Copy link
Member

Choose a reason for hiding this comment

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

Just tested it. I am getting: Invalid buffer id: 1 here if i had startify open before.
So: nvim -> :Telescope find_files -> open file -> :Telescope jumplist (error)
If i do: nvim file -> :Telescope jumplist works great

We should do

if not vim.api.nvim_buf_is_valid(entry.bufnr) then
  return
end

before this line. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Otherwise works great :) thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this to the commit.

I personally don't use startify, and haven't been able to reproduce a scenario where this makes/breaks anything. I think its OK, unless there is some other sort of verification we can do.


return {
valid = true,

value = entry,
ordinal = (
not opts.ignore_filename and filename
or ''
) .. ' ' .. entry.text,
display = make_display,

bufnr = entry.bufnr,
filename = filename,
lnum = entry.lnum,
col = entry.col,
}
end
end


return make_entry