Skip to content

Commit

Permalink
feat(cookbook, navic): improve navic component: add on_click; add hig…
Browse files Browse the repository at this point in the history
…hlights to basic version
  • Loading branch information
rebelot committed Oct 5, 2022
1 parent 9179b71 commit a35f1c2
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,14 @@ new children.
-- The easy way.
local Navic = {
condition = require("nvim-navic").is_available,
provider = require("nvim-navic").get_location,
provider = function()
require("nvim-navic").get_location({highlight=true}),
end
}

-- Full nerd (with icon colors)!
-- Be careful, this implementation will work only if this component
-- is displayied in a single window, and it is the current window.
local Navic = {
condition = require("nvim-navic").is_available,
static = {
Expand Down Expand Up @@ -810,21 +814,41 @@ local Navic = {
Operator = "Operator",
TypeParameter = "Type",
},
-- bit operation dark magic, see below...
enc = function(a, b)
return bit.bor(bit.lshift(a, 16), b)
end,
dec = function(c)
return { bit.rshift(c, 16), bit.band(c, 65535) }
end
},
init = function(self)
local data = require("nvim-navic").get_data() or {}
local children = {}
-- create a child for each level
for i, d in ipairs(data) do
-- encode line and column numbers into a single integer
local pos = self.enc(d.scope.start.line, d.scope.start.character)
local child = {
{
provider = d.icon,
hl = self.type_hl[d.type],
},
{
provider = d.name,
-- escape `%`s (elixir) and buggy default separators
provider = d.name:gsub("%%", "%%%%"):gsub("%s*->%s*", ''),
-- highlight icon only or location name as well
-- hl = self.type_hl[d.type],

on_click = {
-- pass the encoded position through minwid
minwid = pos,
callback = function(self, minwid)
-- decode
vim.api.nvim_win_set_cursor(0, self.dec(minwid))
end,
name = "heirline_navic",
},
},
}
-- add a separator only if needed
Expand Down

0 comments on commit a35f1c2

Please sign in to comment.