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

Register finder #275

Merged
merged 11 commits into from
Nov 23, 2020
31 changes: 31 additions & 0 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,37 @@ actions.set_command_line = function(prompt_bufnr)
vim.cmd(entry.value)
end

actions.edit_register = function(prompt_bufnr)
local entry = actions.get_selected_entry(prompt_bufnr)

local picker = actions.get_current_picker(prompt_bufnr)
print(vim.inspect(getmetatable(picker)))
-- actions.close(prompt_bufnr)

vim.fn.inputsave()
local updated_value = vim.fn.input("Edit" .. entry.value .. "> ", entry.content)
vim.fn.inputrestore()
if updated_value ~= entry.content then
vim.fn.setreg(entry.value, updated_value)
entry.content = updated_value
end
end

actions.paste_register = function(prompt_bufnr)
local entry = actions.get_selected_entry(prompt_bufnr)
local picker = actions.get_current_picker(prompt_bufnr)

actions.close(prompt_bufnr)

local reg_type = vim.fn.getregtype(entry.value)
if reg_type:byte(1, 1) == 0x16 then
reg_type = "b" .. reg_type:sub(2, -1)
end

vim.api.nvim_put({entry.content}, reg_type, true, true)
print (reg_type)
end

actions.run_builtin = function(prompt_bufnr)
local entry = actions.get_selected_entry(prompt_bufnr)

Expand Down
56 changes: 55 additions & 1 deletion lua/telescope/builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ builtin.man_pages = function(opts)
end

pickers.new(opts, {
prompt_tile = 'Man',
prompt_title = 'Man',
sunjon marked this conversation as resolved.
Show resolved Hide resolved
finder = finders.new_table {
results = lines,
entry_maker = make_entry.gen_from_apropos(opts),
Expand Down Expand Up @@ -859,6 +859,60 @@ builtin.marks = function(opts)
}):find()
end

builtin.registers = function(opts)
opts = opts or {}

local registers_table = {}
table.insert(registers_table, {kind = "Unnamed", key = "\""})
sunjon marked this conversation as resolved.
Show resolved Hide resolved
table.insert(registers_table, {kind = "Small Delete", key = "_"})
table.insert(registers_table, {kind = "Alternate", key = "#"})
table.insert(registers_table, {kind = "Expression", key = "="})
table.insert(registers_table, {kind = "Black Hole", key = "_"})
table.insert(registers_table, {kind = "Last Search", key = "/"})

-- clipboaord
local selection = {"*", "+"}
for i in pairs(selection) do
table.insert(registers_table, {kind = "Selection", key = selection[i]})
end
sunjon marked this conversation as resolved.
Show resolved Hide resolved

-- named
for i = 0, 9 do
table.insert(registers_table, {kind = "Numbered", key = tostring(i)})
end

-- alpha
for i = 65, 90 do
table.insert(registers_table, {kind = "Named", key = string.char(i)})
end

-- readonly
local readonly = {":", ".", "%"}
for i in pairs(readonly) do
table.insert(registers_table, {kind = "Read-Only", key = readonly[i]})
end

pickers.new(opts,{
prompt_title = 'Registers',
finder = finders.new_table {
results = registers_table,
entry_maker = make_entry.gen_from_registers(opts),
},
previewer = previewers.vimgrep.new(opts),
sunjon marked this conversation as resolved.
Show resolved Hide resolved
-- use levenshtein as n-gram doesn't support <2 char matches
sorter = sorters.get_levenshtein_sorter(),
attach_mappings = function(_, map)
-- TODO: Find a way to insert the text... it seems hard.
-- map('i', '<C-i>', actions.insert_value, { expr = true })
map('i', '<CR>', actions.insert_value)
Copy link
Member

Choose a reason for hiding this comment

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

You should use actions.goto_file_selection_edit:replace(actions.insert_value). That is a cool new api done by tj. In that way we respect user mappings. Take a look at what other builtins do with replace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll look into this and update tomrrow.

Copy link
Contributor Author

@sunjon sunjon Nov 23, 2020

Choose a reason for hiding this comment

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

To be honest, that mapping wasn't used and I'm not sure how :replace fits in with the other mappings. If it's still applicable, let me know.

Copy link
Member

Choose a reason for hiding this comment

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

The thing about actions.goto_file_selection_edit:replace is that we basically replacing the users main action.
So if a user doesn't like <CR> as mapping and likes to use idk <space> he will hit <space> and we would call actions.goto_.... With that replace we basically just follow what the user expects for all individual pickers, even though they do something differently like checking out a branch or stuff. Does that makes sense.

map('i', '<C-e>', actions.edit_register)
map('i', '<C-p>', actions.paste_register)

return true
end,
}):find()
end

-- find normal mode mappings
builtin.keymaps = function(opts)
opts = opts or {}
Expand Down
29 changes: 28 additions & 1 deletion lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,34 @@ function make_entry.gen_from_marks(_)
end
end

function make_entry.gen_from_registers(_)
local displayer = entry_display.create {
separator = ":",
items = {
{ width = 4 },
{ remaining = true },
},
}

local make_display = function(register, content)
return displayer {
string.format("[%s]", register.key),
content,
}
end

return function(entry)
local reg_content = vim.fn.getreg(entry.key)
return {
value = entry.key,
ordinal = entry.key,
content = reg_content,
regtype = vim.fn.getregtype(entry.key),
display = make_display(entry, reg_content),
sunjon marked this conversation as resolved.
Show resolved Hide resolved
}
end
end

function make_entry.gen_from_vimoptions(opts)
-- TODO: Can we just remove this from `options.lua`?
function N_(s)
Expand Down Expand Up @@ -520,7 +548,6 @@ function make_entry.gen_from_vimoptions(opts)
end
end

-- TODO: don't call this 'line'
local displayer = entry_display.create {
separator = "│",
items = {
Expand Down