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

Zk Grep #171

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 10 additions & 4 deletions lua/zk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,16 @@ end
---@see zk.ui.pick_notes
function M.pick_notes(options, picker_options, cb)
options = vim.tbl_extend("force", { select = ui.get_pick_notes_list_api_selection(picker_options) }, options or {})
api.list(options.notebook_path, options, function(err, notes)
assert(not err, tostring(err))
ui.pick_notes(notes, picker_options, cb)
end)

if options.grep then
picker_options.grep = options.grep
ui.pick_notes({}, picker_options, cb)
else
api.list(options.notebook_path, options, function(err, notes)
assert(not err, tostring(err))
ui.pick_notes(notes, picker_options, cb)
end)
end
end

---Opens a tags picker, and calls the callback with the selection
Expand Down
6 changes: 6 additions & 0 deletions lua/zk/commands/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ commands.add("ZkNotes", function(options)
zk.edit(options, { title = "Zk Notes" })
end)

commands.add("ZkGrep", function(options)
options = options or {}
options.grep = true
zk.edit(options, { title = "Zk Grep" })
end)

commands.add("ZkBacklinks", function(options)
options = vim.tbl_extend("force", { linkTo = { vim.api.nvim_buf_get_name(0) } }, options or {})
zk.edit(options, { title = "Zk Backlinks" })
Expand Down
53 changes: 53 additions & 0 deletions lua/zk/pickers/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ local action_state = require("telescope.actions.state")
local action_utils = require("telescope.actions.utils")
local entry_display = require("telescope.pickers.entry_display")
local previewers = require("telescope.previewers")
local sorters = require("telescope.sorters")
local util = require("zk.util")

local M = {}

Expand Down Expand Up @@ -70,6 +72,7 @@ function M.show_note_picker(notes, options, cb)
sorter = conf.file_sorter(options),
previewer = M.make_note_previewer(),
attach_mappings = function(prompt_bufnr)
-- TODO: add an action here to grep the currently displayed notes with ZkGrep.
actions.select_default:replace(function()
if options.multi_select then
local selection = {}
Expand All @@ -92,6 +95,56 @@ function M.show_note_picker(notes, options, cb)
:find()
end

-- Picker definition
function M.show_note_grep_picker(opts)
-- This is how Telescope's `live_grep` does it
-- local args = { "rg", "--color=never", "--no-heading", "--with-filename", "--line-number", "--column", "--smart-case" }

opts = opts or {}
-- Prepare the prompt
-- TODO: this should be a call to the lua api
local args = { "zk", "list", "--format=oneline", "-m" }
local file_abs_path = util.resolve_notebook_path(vim.api.nvim_get_current_buf())
local notebook_root = util.notebook_root(file_abs_path)

-- Prepare the asynchronous job
local live_zk = finders.new_job(
function(prompt)
-- Empty prompt would give bad results
if not prompt or prompt == "" then
return nil
end
return vim.tbl_flatten({ args, prompt })
end,
-- Format entries
function(entry)
local _, _, title, filename, date_modified = string.find(entry, [[(..-) (%S+) %((.+)%)]])
return {
value = entry, -- entire entry is searchable
display = title, -- only the title is displayed
ordinal = entry, -- TODO: what is this for?
-- TODO: remove hardcoded path below
path = notebook_root .. "/" .. filename, -- store the path so we can open the file
}
end,
opts.max_results,
opts.cwd
)

pickers
.new(opts, {
prompt_title = "Live Zk",
finder = live_zk,
previewer = conf.grep_previewer(opts),
sorter = sorters.highlighter_only(opts),
attach_mappings = function(_, map)
map("i", "<c-space>", actions.to_fuzzy_refine)
return true
end,
})
:find()
end

function M.show_tag_picker(tags, options, cb)
options = options or {}
local telescope_options = vim.tbl_extend("force", { prompt_title = options.title }, options.telescope or {})
Expand Down
11 changes: 10 additions & 1 deletion lua/zk/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ local M = {}
function M.pick_notes(notes, options, cb)
options =
vim.tbl_extend("force", { title = "Zk Notes", picker = config.options.picker, multi_select = true }, options or {})
require("zk.pickers." .. options.picker).show_note_picker(notes, options, cb)

if options.grep ~= nil then
if options.picker ~= "telescope" then
print(":ZkGrep is only usable with Telescope for now. Maybe time for a PR? 😘")
return
end
require("zk.pickers." .. options.picker).show_note_grep_picker(options, cb)
else
require("zk.pickers." .. options.picker).show_note_picker(notes, options, cb)
end
end

---Opens a tags picker
Expand Down