Skip to content

Commit

Permalink
feat: implement mapping to insert link to selected document
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasMuehlmann committed Sep 27, 2021
1 parent 418bcf9 commit 8f5ab02
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ Add the following line somewhere after `require('telescope').setup()`.
require('telescope').load_extension('bntp')
```

## Available commands
# Available commands

```vim
:Telescope bntp documents
```

# Additional Mappings

`<c-l>` in insert or normal mode to insert a `markdown` link to the selected document.
34 changes: 29 additions & 5 deletions lua/telescope/_extensions/bntp.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
-- https://github.com/nvim-telescope/telescope.nvim/blob/master/developers.md#guide-to-your-first-picker

local documents = require('telescope._extensions.bntp.documents')
local bntp_documents = require('telescope._extensions.bntp.documents')

local pickers = require "telescope.pickers"
local finders = require "telescope.finders"
local actions = require "telescope.actions"
local action_state = require('telescope.actions.state')
local conf = require("telescope.config").values

local items = {}
Expand All @@ -12,21 +14,43 @@ local items = {}
-- Reacting to db changes?
-- Checking for new tags every few seconds?
local update_documents = function(self)
local documents = documents .get_documents()
local documents_ = bntp_documents.get_documents()

for _, document in ipairs(documents) do
table.insert(items, tag)
for _, document in ipairs(documents_) do
table.insert(items, document)
end
end

local insert_link = function(prompt_bufnr)
local linkDestionation = action_state.get_selected_entry().value
local insertedText = "[]".."("..linkDestionation..")"

actions._close(prompt_bufnr, true)

local cursor = vim.api.nvim_win_get_cursor(0)
local line = vim.api.nvim_get_current_line()
local nline = line:sub(0, cursor[2]) .. insertedText .. line:sub( cursor[2]+ 1)
vim.api.nvim_set_current_line(nline)

vim.schedule(function()
vim.api.nvim_win_set_cursor(0, { cursor[1], cursor[2] + 1})
end)
end

local documents = function(opts)
pickers.new(opts, {
prompt_title = "bntp documents",
finder = finders.new_table {
results = documents.get_tags()
results = bntp_documents.get_tags()
},
sorter = conf.generic_sorter(opts),
previewer = conf.file_previewer(opts),
attach_mappings = function(prompt_bufnr, map)
map('i', '<c-l>', insert_link)
map('n', '<c-l>', insert_link)

return true
end,
}):find()
end

Expand Down

0 comments on commit 8f5ab02

Please sign in to comment.