generated from eblume/project-template
feat(nvim): live Telescope filter for the [[ link picker (§8.4)
Some checks failed
Build / validate (pull_request) Failing after 10s
Some checks failed
Build / validate (pull_request) Failing after 10s
Replace the "guess a query, then filter a frozen result set" flow with a live fuzzy filter over every node when Telescope is present: type to narrow `node.list`, <CR> inserts the highlighted node, <C-x> creates a doc named the current prompt (a miss flows straight into making it). The search-then-`vim.ui.select` two-step stays as the no-Telescope fallback. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
b112b0d7c1
commit
d178a657e0
3 changed files with 84 additions and 16 deletions
|
|
@ -68,11 +68,74 @@ function M.follow()
|
|||
require("heph.node").open(node.id)
|
||||
end
|
||||
|
||||
--- Pick a node (by full-text search) and insert a canonical `[[NODEID]]` link at
|
||||
--- the cursor — the authoring path for wiki-links-by-id (§8.4); a node id is the
|
||||
--- only thing that ever enters a stored link, so there's no name ambiguity. A
|
||||
--- "Create" entry mints a new doc named after the query. No-op if cancelled.
|
||||
function M.insert()
|
||||
-- Insert the labelled form `[[id|Name]]` at the cursor (readable + conceal-ready;
|
||||
-- it collapses to the canonical bare `[[id]]` on save, §8.4).
|
||||
local function put_link(id, title)
|
||||
vim.api.nvim_put({ "[[" .. id .. "|" .. title .. "]]" }, "c", true, true)
|
||||
end
|
||||
|
||||
-- Mint a doc named `title` and insert a link to it.
|
||||
local function create_and_put(title)
|
||||
if title and #title > 0 then
|
||||
local node = rpc.call("node.create", { kind = "doc", title = title })
|
||||
put_link(node.id, node.title)
|
||||
end
|
||||
end
|
||||
|
||||
-- Telescope is available and not explicitly disabled (tests force ui.select).
|
||||
local function use_telescope()
|
||||
return not vim.g.heph_force_ui_select and pcall(require, "telescope")
|
||||
end
|
||||
|
||||
-- A live, fuzzy-filtered Telescope picker over every node: type to narrow,
|
||||
-- <CR> inserts the highlighted node, <C-x> creates a doc named the current
|
||||
-- prompt text (so a miss flows straight into "make it"). Telescope only.
|
||||
local function telescope_insert()
|
||||
local pickers = require("telescope.pickers")
|
||||
local finders = require("telescope.finders")
|
||||
local conf = require("telescope.config").values
|
||||
local actions = require("telescope.actions")
|
||||
local action_state = require("telescope.actions.state")
|
||||
|
||||
local nodes = rpc.call("node.list", vim.empty_dict()) or {}
|
||||
pickers
|
||||
.new({}, {
|
||||
prompt_title = "Link to node (<C-x> = create from prompt)",
|
||||
finder = finders.new_table({
|
||||
results = nodes,
|
||||
entry_maker = function(n)
|
||||
return {
|
||||
value = n,
|
||||
display = n.title .. " [" .. (n.kind or "node") .. "]",
|
||||
ordinal = n.title,
|
||||
}
|
||||
end,
|
||||
}),
|
||||
sorter = conf.generic_sorter({}),
|
||||
attach_mappings = function(bufnr, map)
|
||||
actions.select_default:replace(function()
|
||||
local entry = action_state.get_selected_entry()
|
||||
actions.close(bufnr)
|
||||
if entry then
|
||||
put_link(entry.value.id, entry.value.title)
|
||||
end
|
||||
end)
|
||||
local create = function()
|
||||
local title = action_state.get_current_line()
|
||||
actions.close(bufnr)
|
||||
create_and_put(title)
|
||||
end
|
||||
map("i", "<C-x>", create)
|
||||
map("n", "<C-x>", create)
|
||||
return true
|
||||
end,
|
||||
})
|
||||
:find()
|
||||
end
|
||||
|
||||
-- Fallback when Telescope isn't available: prompt a query, search, then pick
|
||||
-- from the results (or a "+ Create" entry). `vim.ui.select` can't filter live.
|
||||
local function uiselect_insert()
|
||||
vim.ui.input({ prompt = "Link to: " }, function(query)
|
||||
if not query or query == "" then
|
||||
return
|
||||
|
|
@ -93,21 +156,26 @@ function M.insert()
|
|||
}, function(choice)
|
||||
if not choice then
|
||||
return
|
||||
end
|
||||
local id, title
|
||||
if choice.__create then
|
||||
local node = rpc.call("node.create", { kind = "doc", title = choice.title })
|
||||
id, title = node.id, node.title
|
||||
elseif choice.__create then
|
||||
create_and_put(choice.title)
|
||||
else
|
||||
id, title = choice.id, choice.title
|
||||
put_link(choice.id, choice.title)
|
||||
end
|
||||
-- Insert the labelled form `[[id|Name]]` (readable + conceal-ready); it
|
||||
-- collapses to the canonical bare `[[id]]` on save (§8.4).
|
||||
vim.api.nvim_put({ "[[" .. id .. "|" .. title .. "]]" }, "c", true, true)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
--- Insert a canonical `[[NODEID|Name]]` link at the cursor (§8.4) by picking a
|
||||
--- node — live-filtered via Telescope when available, else a search-then-select
|
||||
--- prompt. A node id is the only thing that ever enters a stored link.
|
||||
function M.insert()
|
||||
if use_telescope() then
|
||||
telescope_insert()
|
||||
else
|
||||
uiselect_insert()
|
||||
end
|
||||
end
|
||||
|
||||
--- Attach the buffer-local follow/insert keymaps and inline-`#hashtag`
|
||||
--- highlighting (only on heph:// buffers).
|
||||
function M.attach(buf)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue