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

add option to insert link from telescope #95

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions lua/zk/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ M.defaults = {
filetypes = { "markdown" },
},
},
mappings = {
telescope_link_note = { {"i", "n"}, "<C-Y>" }
}
}

M.options = M.defaults -- not necessary, but better code completion
Expand Down
43 changes: 42 additions & 1 deletion lua/zk/pickers/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local action_utils = require("telescope.actions.utils")
local putils = require("telescope.previewers.utils")
local entry_display = require("telescope.pickers.entry_display")
local previewers = require("telescope.previewers")
local config = require("zk.config")

local M = {}

Expand Down Expand Up @@ -59,18 +60,37 @@ function M.make_note_previewer()
})
end

function M.capture_cursor_state()
local pos = vim.api.nvim_win_get_cursor(0)
return {
bufnr = vim.api.nvim_get_current_buf(),
row = pos[1] - 1,
col = pos[2],
}
end

function M.insert_links_at_cursor(cursor, entries)
for i, entry in pairs(entries) do
entries[i] = string.format("[%s](%s)", entry.title, entry.path)
end
vim.api.nvim_buf_set_text(cursor.bufnr, cursor.row, cursor.col, cursor.row, cursor.col, entries)
end

function M.show_note_picker(notes, options, cb)
options = options or {}
local telescope_options = vim.tbl_extend("force", { prompt_title = options.title }, options.telescope or {})

-- before telescope buffer takes over
local cursor = M.capture_cursor_state()

pickers.new(telescope_options, {
finder = finders.new_table({
results = notes,
entry_maker = M.create_note_entry_maker(options),
}),
sorter = conf.file_sorter(options),
previewer = M.make_note_previewer(),
attach_mappings = function(prompt_bufnr)
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
if options.multi_select then
local selection = {}
Expand All @@ -87,6 +107,27 @@ function M.show_note_picker(notes, options, cb)
cb(action_state.get_selected_entry().value)
end
end)


-- add the option to insert the note as link in current buffer
local mode = config.options.mappings.telescope_link_note[1]
local key = config.options.mappings.telescope_link_note[2]
map(mode, key, function()
local selection = {}
if options.multi_select then
action_utils.map_selections(prompt_bufnr, function(entry, _)
table.insert(selection, entry.value)
end)
else
selection = { action_state.get_selected_entry().value }
end
if vim.tbl_isempty(selection) then
selection = { action_state.get_selected_entry().value }
end
M.insert_links_at_cursor(cursor, selection)
actions.close(prompt_bufnr)
end)

return true
end,
}):find()
Expand Down