Skip to content

Commit

Permalink
feat: separate quickfix and telescope pickers
Browse files Browse the repository at this point in the history
We are not piggy-backing on quickfix for telescope picker anymore.
  • Loading branch information
bloznelis committed Mar 12, 2024
1 parent 119e003 commit ec15a7f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 16 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ Track edit locations and jump back to them, like [changelist](https://neovim.io/
-- Jump to next entry in the edit history
vim.keymap.set('n', '<C-l>', before.jump_to_next_edit, {})

-- Move edit history to quickfix (or telescope)
vim.keymap.set('n', '<leader>oe', before.show_edits, {})
-- Show and look for previous edits in telescope
vim.keymap.set('n', '<leader>oe', before.show_edits_in_telescope, {})

-- Show and look for previous edits in quickfix list
vim.keymap.set('n', '<leader>oq', before.show_edits_in_quickfix, {})
end
}
```
Expand All @@ -34,14 +37,12 @@ require('before').setup({
history_size = 42
-- Wrap around the ends of the edit history (default: false)
history_wrap_enabled = true
-- Use telescope quickfix picker for `show_edits` command (default: false)
telescope_for_preview = true
})
```
#### Telescope picker
```lua
-- Provide custom opts to telescope picker as show_edits argument:
-- You can provide telescope opts to the picker via show_edits_in_telescope arguments:
vim.keymap.set('n', '<leader>oe', function()
before.show_edits(require('telescope.themes').get_dropdown())
before.show_edits_in_telescope(require('telescope.themes').get_dropdown())
end, {})
```
75 changes: 65 additions & 10 deletions lua/before.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ local function find_backwards_jump(currentLocation)
local location = M.edit_locations[local_cursor]

if location and not bufvalid(location.bufnr) then
vim.cmd('e ' .. location.file)
vim.cmd.edit(location.file)
local new_bufnr = vim.api.nvim_get_current_buf()
location['bufnr'] = new_bufnr
M.edit_locations[local_cursor] = location
Expand Down Expand Up @@ -90,7 +90,7 @@ local function find_forward_jump(currentLocation)
local location = M.edit_locations[local_cursor]

if location and not bufvalid(location.bufnr) then
vim.cmd('e ' .. location.file)
vim.cmd.edit(location.file)
local new_bufnr = vim.api.nvim_get_current_buf()
location['bufnr'] = new_bufnr
M.edit_locations[local_cursor] = location
Expand Down Expand Up @@ -119,11 +119,15 @@ local function find_forward_jump(currentLocation)
end
end

local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end

local function load_file_line(file, linenum)
local cnt = 1
for line in io.lines(file) do
if cnt == linenum then
return line
return trim(line)
end
cnt = cnt + 1
end
Expand All @@ -132,27 +136,77 @@ local function load_file_line(file, linenum)
end

local function load_buf_line(bufnr, linenum)
return vim.api.nvim_buf_get_lines(bufnr, linenum - 1, linenum, false)[1]
return trim(vim.api.nvim_buf_get_lines(bufnr, linenum - 1, linenum, false)[1])
end

function M.show_edits(picker_opts)
local function get_line_content(location)
local line_content = nil

if bufvalid(location.bufnr) then
line_content = load_buf_line(location.bufnr, location.line)
else
line_content = load_file_line(location.file, location.line)
end

if line_content == '' then
line_content = "[EMPTY-LINE]"
end
return line_content
end

local pickers = require('telescope.pickers')
local finders = require('telescope.finders')
local conf = require('telescope.config').values

function M.show_edits_in_telescope(opts)
local default_opts = {
prompt_title = "Edit Locations",
finder = finders.new_table({
results = M.edit_locations,
entry_maker = function(entry)
local line_content = get_line_content(entry)
return {
value = entry.file .. entry.line,
display = entry.line .. ':' .. entry.bufnr .. '| ' .. line_content,
ordinal = entry.line .. ':' .. entry.bufnr .. '| ' .. line_content,
filename = entry.file,
bufnr = entry.bufnr,
lnum = entry.line,
col = entry.col,
}
end,
}),
sorter = conf.generic_sorter({}),
previewer = require('telescope.config').values.grep_previewer({}),
}

opts = vim.tbl_deep_extend("force", default_opts, opts or {})

pickers.new({ preview_title = "Preview" }, opts):find()
end

function M.show_edits_in_quickfix()
local qf_entries = {}
for _, location in pairs(M.edit_locations) do
local line_content = get_line_content(location)
if bufvalid(location.bufnr) then
local line_content = load_buf_line(location.bufnr, location.line)
table.insert(qf_entries, { bufnr = location.bufnr, lnum = location.line, col = location.col, text = line_content })
else
local line_content = load_file_line(location.file, location.line)
table.insert(qf_entries,
{ filename = location.file, lnum = location.line, col = location.col, text = line_content })
end
end

vim.fn.setqflist(qf_entries, 'r')
vim.fn.setqflist({}, 'r', { title = 'Edit Locations', items = qf_entries })
vim.cmd([[copen]])
end

-- DEPRECATED, but don't want to brake the users by removing.
function M.show_edits(picker_opts)
if M.telescope_for_preview then
require('telescope.builtin').quickfix(picker_opts or {})
M.show_edits_in_telescope(picker_opts)
else
vim.cmd('copen')
M.show_edits_in_quickfix()
end
end

Expand Down Expand Up @@ -209,6 +263,7 @@ end
M.defaults = {
history_size = 10,
history_wrap_enabled = false,
-- DEPRECATED, but don't want to brake the users by removing.
telescope_for_preview = false
}

Expand Down

0 comments on commit ec15a7f

Please sign in to comment.