Skip to content

Commit

Permalink
feat: add heading picker (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 authored Dec 4, 2021
1 parent 2d88555 commit d6089ba
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lua/neorg/modules/core/integrations/telescope/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,30 @@ module.load = function()

telescope.load_extension("neorg")

module.required["core.keybinds"].register_keybinds(module.name, { "find_linkable", "insert_link" })
module.required["core.keybinds"].register_keybinds(module.name, { "find_linkable", "insert_link", "search_headings" })
end

module.public = {
find_linkable = require("telescope._extensions.neorg.find_linkable"),
insert_link = require("telescope._extensions.neorg.insert_link"),
search_headings = require("telescope._extensions.neorg.search_headings"),
}

module.on_event = function(event)
if event.split_type[2] == "core.integrations.telescope.find_linkable" then
module.public.find_linkable()
elseif event.split_type[2] == "core.integrations.telescope.insert_link" then
module.public.insert_link()
elseif event.split_type[2] == "core.integrations.telescope.search_headings" then
module.public.search_headings()
end
end

module.events.subscribed = {
["core.keybinds"] = {
["core.integrations.telescope.find_linkable"] = true,
["core.integrations.telescope.insert_link"] = true,
["core.integrations.telescope.search_headings"] = true,
},
}

Expand Down
1 change: 1 addition & 0 deletions lua/telescope/_extensions/neorg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ return require("telescope").register_extension({
exports = {
find_linkable = require("neorg.modules.core.integrations.telescope.module").public.find_linkable,
insert_link = require("neorg.modules.core.integrations.telescope.module").public.insert_link,
search_headings = require("neorg.modules.core.integrations.telescope.module").public.search_headings,
},
})
120 changes: 120 additions & 0 deletions lua/telescope/_extensions/neorg/search_headings.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
local actions = require("telescope.actions")
local themes = require("telescope.themes")
local action_set = require("telescope.actions.set")
local action_state = require('telescope.actions.state')
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local conf = require("telescope.config").values
local make_entry = require "telescope.make_entry"

local neorg_loaded, _ = pcall(require, "neorg.modules")

assert(neorg_loaded, "Neorg is not loaded - please make sure to load Neorg first")

local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)

local bufnr = vim.api.nvim_get_current_buf()

local filename = vim.fn.expand(vim.api.nvim_buf_get_name(bufnr))
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype")

return function()
local opts = themes.get_dropdown({
border = true,
previewer = false,
shorten_path = false,
prompt_prefix = "",
-- prompt_prefix = "  ",
layout_config = {
prompt_position = "top",
},
})

local lines_with_numbers = {}

for lnum, line in ipairs(lines) do
local match = line:match("^%s*%*+%s+")
if match then
local text = vim.trim(line)
table.insert(lines_with_numbers, {
lnum = lnum,
bufnr = bufnr,
filename = filename,
text = text,
})
end
end

local ts_ok, ts_parsers = pcall(require, "nvim-treesitter.parsers")
if ts_ok then
filetype = ts_parsers.ft_to_lang(filetype)
end

local parser_ok, parser = pcall(vim.treesitter.get_parser, bufnr, filetype)
local query_ok, query = pcall(vim.treesitter.get_query, filetype, "highlights")
if parser_ok and query_ok then
local root = parser:parse()[1]:root()

local highlighter = vim.treesitter.highlighter.new(parser)
local highlighter_query = highlighter:get_query(filetype)

local line_highlights = setmetatable({}, {
__index = function(t, k)
local obj = {}
rawset(t, k, obj)
return obj
end,
})
for id, node in query:iter_captures(root, bufnr, 0, -1) do
local hl = highlighter_query:_get_hl_from_capture(id)
if hl and type(hl) ~= "number" then
local row1, col1, row2, col2 = node:range()
col1 = 0

if row1 == row2 then
local row = row1 + 1

for index = col1, col2 do
line_highlights[row][index] = hl
end
else
local row = row1 + 1
for index = col1, #lines[row] do
line_highlights[row][index] = hl
end

while row < row2 + 1 do
row = row + 1

for index = 0, #lines[row] do
line_highlights[row][index] = hl
end
end
end
end
end

opts.line_highlights = line_highlights
end

pickers.new(opts, {
prompt_title = "Search Headings",
finder = finders.new_table {
results = lines_with_numbers,
entry_maker = opts.entry_maker or make_entry.gen_from_buffer_lines(opts),
},
-- finder = finders.new_table(headings),
sorter = conf.generic_sorter(opts),
previewer = conf.grep_previewer(opts),
attach_mappings = function()
action_set.select:enhance {
post = function()
local selection = action_state.get_selected_entry()
vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 })
end,
}

return true
end,
}):find()
end

0 comments on commit d6089ba

Please sign in to comment.