diff .chezmoitemplates/init.lua @ 783:335b1f5c242d

feat: add winbar with navic (LSP based location breadcrumbs)
author zegervdv <zegervdv@me.com>
date Fri, 28 Apr 2023 08:18:06 +0200
parents 31605398117a
children add486ac2046
line wrap: on
line diff
--- a/.chezmoitemplates/init.lua	Fri Apr 28 07:12:49 2023 +0200
+++ b/.chezmoitemplates/init.lua	Fri Apr 28 08:18:06 2023 +0200
@@ -92,7 +92,7 @@
           fold_open = '-',
           done = '✓',
         },
-        hg_cmd = { 'chg' },
+        hg_cmd = { 'rhg' },
         view = {
           merge_tool = {
             layout = 'diff4_mixed',
@@ -426,6 +426,11 @@
   },
 
   {
+    'SmiteshP/nvim-navic',
+    dependencies = { 'neovim/nvim-lspconfig' },
+  },
+
+  {
     'ThePrimeagen/refactoring.nvim',
     dependencies = {
       'nvim-lua/plenary.nvim',
@@ -666,7 +671,61 @@
         statusline_default,
       }
 
-      require('heirline').setup { statusline = statusline }
+      local navic = require 'nvim-navic'
+      local winbar = {
+        fallthrough = false,
+        condition = function() return navic.is_available() end,
+        static = {
+          -- bit operation dark magic, see below...
+          enc = function(line, col, winnr) return bit.bor(bit.lshift(line, 16), bit.lshift(col, 6), winnr) end,
+          -- line: 16 bit (65535); col: 10 bit (1023); winnr: 6 bit (63)
+          dec = function(c)
+            local line = bit.rshift(c, 16)
+            local col = bit.band(bit.rshift(c, 6), 1023)
+            local winnr = bit.band(c, 63)
+            return line, col, winnr
+          end,
+        },
+        init = function(self)
+          local data = navic.get_data() or {}
+          local children = {}
+          for i, loc in ipairs(data) do
+            local pos = self.enc(loc.scope.start.line, loc.scope.start.character, self.winnr)
+            local child = {
+              {
+                provider = loc.name,
+                on_click = {
+                  minwid = pos,
+                  callback = function(_, minwid)
+                    local line, col, winnr = self.dec(minwid)
+                    vim.api.nvim_win_set_cursor(vim.fn.win_getid(winnr), { line, col })
+                  end,
+                  name = 'heirline_navic',
+                },
+              },
+            }
+
+            if #data > 1 and i < #data then table.insert(child, { provider = ' > ' }) end
+            table.insert(children, child)
+          end
+
+          self.child = self:new(children, 1)
+        end,
+        provider = function(self) return self.child:eval() end,
+        update = 'CursorMoved',
+      }
+
+      require('heirline').setup {
+        statusline = statusline,
+        winbar = winbar,
+        opts = {
+          disable_winbar_cb = function(args)
+            return not conditions.buffer_matches { filetype = { 'python', 'verilog', 'systemverilog', 'cpp', 'c' } }
+            -- TODO: If LSP is not attached when opening the window, this does not work
+            -- return not navic.is_available(args.bufnr)
+          end,
+        },
+      }
     end,
   },
 
@@ -1191,6 +1250,11 @@
     client.server_capabilities.documentRangeFormattingProvider = false
   end
 
+  if client.server_capabilities.documentSymbolProvider then
+    local navic = require 'nvim-navic'
+    navic.attach(client, bufnr)
+  end
+
   if client.supports_method 'textDocument/rangeFormatting' then
     local root = vim.fs.find({ '.git', '.hg' }, { path = client.config.root_dir })
     local vcs = 'git'