Skip to content

Commit

Permalink
vim: Tab in Present hops between links
Browse files Browse the repository at this point in the history
Due to nvim-treesitter/nvim-treesitter-textobjects#479,
I can't use nvim-treesitter-textobjects, but this is actually quite
simple… :-)
  • Loading branch information
liskin committed Sep 18, 2024
1 parent 7231465 commit 8460d25
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
-- Origin: https://github.com/jghauser/follow-md-links.nvim/blob/caa85cad973c5e612662d04b55e07eefc88f1d6b/lua/follow-md-links.lua
-- SPDX-License-Identifier: GPL-3.0-only

local ts = require("vim.treesitter")
local ts_utils = require("nvim-treesitter.ts_utils")
local ts = vim.treesitter
local ts_utils = require'nvim-treesitter.ts_utils'
local ts_lib = require'init.lib.treesitter'

local M = {}

local function get_reference_link_destination(link_label)
local syntax_trees = vim.treesitter.get_parser():parse(true)
local root = syntax_trees[1]:root()
local _, root = ts_lib.parse()

local link_defs = {}
local query_link_defs = vim.treesitter.query.parse("markdown", [[
Expand Down
50 changes: 50 additions & 0 deletions .config/nvim/lua/init/lib/hop_treesitter_objects.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
local ts_lib = require'init.lib.treesitter'

local M = {}
M.opts = {}

local function text_objects(captures)
local objects = ts_lib.text_objects()
local jump_targets = {}

for _, object in ipairs(objects or {}) do
if not captures or vim.tbl_contains(captures, object.capture) then
for _, node in ipairs(object.nodes) do
local row, col = node:range()
local jump_target = {
window = 0,
buffer = 0,
cursor = {
row = row + 1,
col = col,
},
length = 0,
}
table.insert(jump_targets, jump_target)
-- FIXME: filter out duplicates and invisible targets
end
end
end

return {
jump_targets = jump_targets,
}
end

function M.text_objects(captures, opts)
local hop = require'hop'

opts = setmetatable(opts or {}, { __index = M.opts })
hop.hint_with(function(_opts)
return text_objects(captures)
end, opts)
end

function M.register(opts)
M.opts = opts
vim.api.nvim_create_user_command('HopTextObjects', function()
M.text_objects()
end, {})
end

return M
40 changes: 40 additions & 0 deletions .config/nvim/lua/init/lib/treesitter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local ts = vim.treesitter

local M = {}

function M.parse()
local ok, parser = pcall(ts.get_parser)
if ok then
local syntax_trees = parser:parse(true)
return parser, syntax_trees[1]:root()
end
end

local function text_objects(root_parser)
local objects = {}

root_parser:for_each_tree(function(tree, parser)
local query = ts.query.get(parser:lang(), 'textobjects')
for _, match, _metadata in query:iter_matches(tree:root(), 0, nil, nil, { all = true }) do
for id, nodes in pairs(match) do
table.insert(objects, {
capture = query.captures[id],
nodes = nodes,
})
end
end
end)

return objects
end

function M.text_objects()
local parser = M.parse()
if parser then
return text_objects(parser)
else
return {}
end
end

return M
5 changes: 5 additions & 0 deletions .config/nvim/lua/init/plug_hop.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
require'hop'.setup {
extensions = {
'hop-yank',
'hop-treesitter',
'init.lib.hop_treesitter_objects'
},
}
6 changes: 5 additions & 1 deletion .config/nvim/lua/init/plug_presenting.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local presenting = require'presenting'
local lspconfig = require'lspconfig'
local follow_md_links = require'init.lib.follow-md-links'
local follow_md_links = require'init.lib.follow_md_links'
local plug_hop_ts = require'init.lib.hop_treesitter_objects'

local function present_in_argv()
return vim.tbl_contains(vim.v.argv, '-c') and vim.tbl_contains(vim.v.argv, 'Present')
Expand Down Expand Up @@ -89,6 +90,9 @@ presenting.setup {
['<PageUp>'] = presenting.prev,
['<PageDown>'] = presenting.next,
['<CR>'] = follow_md_links.follow_link,
['<Tab>'] = function()
plug_hop_ts.text_objects({'markup.link.label'})
end,
['q'] = quit,
},
configure_slide_buffer = lspconfig.util.add_hook_after(presenting.config.configure_slide_buffer,
Expand Down
1 change: 0 additions & 1 deletion .vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,6 @@ if has('nvim')
noremap <silent> Hw <Cmd>:HopWordMW<CR>
noremap <silent> Hl <Cmd>:HopLineStartMW<CR>
noremap <silent> HL <Cmd>:HopLineMW<CR>
noremap <silent> Ht <Cmd>:HopNodesMW<CR>
noremap <silent> H/ <Cmd>:HopPatternMW<CR>
noremap <silent> HH <Cmd>:HopAnywhereMW<CR>
endif
Expand Down

0 comments on commit 8460d25

Please sign in to comment.