]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/ui.lua
nvim: show pretty_path in inactive sections too
[dotfiles.git] / nvim / lua / plugins / ui.lua
1 local M = {}
2
3 ---@param component any -- actually a lualine.component
4 ---@param text string
5 ---@param hl_group? string
6 ---@return string
7 function M.format(component, text, hl_group)
8 text = text:gsub("%%", "%%%%")
9 if not hl_group or hl_group == "" then
10 return text
11 end
12 --- hl_cache is added here, not modified
13 ---@type table<string, string>
14 component.hl_cache = component.hl_cache or {}
15 local lualine_hl_group = component.hl_cache[hl_group]
16 if not lualine_hl_group then
17 local utils = require("lualine.utils.utils")
18 ---@type string[]
19 local gui = vim.tbl_filter(function(x) return x end, {
20 utils.extract_highlight_colors(hl_group, "bold") and "bold",
21 utils.extract_highlight_colors(hl_group, "italic") and "italic",
22 })
23
24 lualine_hl_group = component:create_hl({
25 fg = utils.extract_highlight_colors(hl_group, "fg"),
26 gui = #gui > 0 and table.concat(gui, ",") or nil,
27 }, "LV_" .. hl_group) --[[@as string]]
28 component.hl_cache[hl_group] = lualine_hl_group
29 end
30 return component:format_hl(lualine_hl_group) .. text .. component:get_default_hl()
31 end
32
33 function M.pretty_path()
34 return function(self)
35 local opts = {
36 directory_hl = "",
37 filename_hl = "Bold",
38 length = 3,
39 modified_hl = "MatchParen", -- hl group when file is modified
40 modified_sign = "",
41 readonly_icon = " 󰌾 ",
42 }
43
44 local path = vim.fn.expand("%:p") --[[@as string]]
45
46 if path == "" then
47 return ""
48 end
49
50 path = require("lazy.core.util").norm(path)
51 local cwd = vim.uv.cwd()
52
53 if path:find(cwd, 1, true) == 1 then
54 path = path:sub(#cwd + 2)
55 end
56
57 local sep = package.config:sub(1, 1)
58 local parts = vim.split(path, "[\\/]")
59
60 if opts.length == 0 then
61 parts = parts
62 elseif #parts > opts.length then
63 parts = { parts[1], "…", unpack(parts, #parts - opts.length + 2, #parts) }
64 end
65
66 if vim.bo.modified then
67 parts[#parts] = parts[#parts] .. opts.modified_sign
68 parts[#parts] = M.format(self, parts[#parts], opts.modified_hl)
69 else
70 parts[#parts] = M.format(self, parts[#parts], opts.filename_hl)
71 end
72
73 local dir = ""
74 if #parts > 1 then
75 dir = table.concat({ unpack(parts, 1, #parts - 1) }, sep)
76 dir = M.format(self, dir .. sep, opts.directory_hl)
77 end
78
79 local readonly = ""
80 if vim.bo.readonly then
81 readonly = M.format(self, opts.readonly_icon, opts.modified_hl)
82 end
83 return dir .. parts[#parts] .. readonly
84 end
85 end
86
87 ---@type LazySpec
88 return {
89 { 'echasnovski/mini.clue',
90 -- NOTE: this caused some issue with unterminated mappings, i.e. <L>gc when there
91 -- is also <L>gcc. It would simply terminate early and not allow for next mapping.
92 version = '*',
93 opts = {
94 triggers = {
95 { mode = 'n', keys = '<Leader>' },
96 { mode = 'x', keys = '<Leader>' },
97 -- `g` key
98 { mode = 'n', keys = 'g' },
99 { mode = 'x', keys = 'g' },
100 -- Marks
101 { mode = 'n', keys = "'" },
102 { mode = 'n', keys = '`' },
103 { mode = 'x', keys = "'" },
104 { mode = 'x', keys = '`' },
105 -- Window commands
106 { mode = 'n', keys = '<C-w>' },
107 -- `z` key
108 { mode = 'n', keys = 'z' },
109 { mode = 'x', keys = 'z' },
110 },
111 clues = {
112 { mode = 'n', keys = '<Leader>b', desc = '+Buffers' },
113 { mode = 'n', keys = '<Leader>c', desc = '+LSP' },
114 },
115 },
116 },
117 { "akinsho/bufferline.nvim",
118 event = "VeryLazy",
119 keys = {
120 { "<leader>bp", "<Cmd>BufferLineTogglePin<CR>", desc = "Toggle Pin" },
121 { "<leader>bP", "<Cmd>BufferLineGroupClose ungrouped<CR>", desc = "Delete Non-Pinned Buffers" },
122 { "<leader>br", "<Cmd>BufferLineCloseRight<CR>", desc = "Delete Buffers to the Right" },
123 { "<leader>bl", "<Cmd>BufferLineCloseLeft<CR>", desc = "Delete Buffers to the Left" },
124 { "<S-h>", "<cmd>BufferLineCyclePrev<cr>", desc = "Prev Buffer" },
125 { "<S-l>", "<cmd>BufferLineCycleNext<cr>", desc = "Next Buffer" },
126 { "[b", "<cmd>BufferLineCyclePrev<cr>", desc = "Prev Buffer" },
127 { "]b", "<cmd>BufferLineCycleNext<cr>", desc = "Next Buffer" },
128 { "[B", "<cmd>BufferLineMovePrev<cr>", desc = "Move buffer prev" },
129 { "]B", "<cmd>BufferLineMoveNext<cr>", desc = "Move buffer next" },
130 },
131 ---@type BufferlineConfig
132 opts = {
133 options = {
134 close_command = function(n) Snacks.bufdelete(n) end,
135 right_mouse_command = function(n) Snacks.bufdelete(n) end,
136 diagnostics = "nvim_lsp",
137 always_show_bufferline = false,
138 diagnostics_indicator = function(_, _, diag)
139 local icons = LazyVim.config.icons.diagnostics
140 local ret = (diag.error and icons.Error .. diag.error .. " " or "")
141 .. (diag.warning and icons.Warn .. diag.warning or "")
142 return vim.trim(ret)
143 end,
144 offsets = {
145 {
146 filetype = "man",
147 text = "Man page",
148 highlight = "Directory",
149 text_align = "left",
150 },
151 },
152 ---@param opts bufferline.IconFetcherOpts
153 get_element_icon = function(opts)
154 return LazyVim.config.icons.ft[opts.filetype]
155 end,
156 },
157 },
158 },
159 { "nvim-lualine/lualine.nvim",
160 -- TODO: Things that were in vim but are missing
161 -- - git line add/mod/del ar next to branch name rather on right
162 -- - one status line per splits
163 -- - maybe a single one is OK too?
164 -- - I think I want a line stating wihch file is in the split though
165 -- - unix/dos eof markers
166 -- - really I only want to know if it's not unix
167 -- - filetype in text form. It's quite important to glance this quickly
168 event = "VeryLazy",
169 init = function()
170 vim.g.lualine_laststatus = vim.o.laststatus
171 if vim.fn.argc(-1) > 0 then
172 -- set an empty statusline till lualine loads
173 vim.o.statusline = " "
174 else
175 -- hide the statusline on the starter page
176 vim.o.laststatus = 0
177 end
178 end,
179 opts = function()
180 -- PERF: we don't need this lualine require madness 🤷
181 local lualine_require = require("lualine_require")
182 lualine_require.require = require
183
184 local icons = LazyVim.config.icons
185
186 vim.o.laststatus = vim.g.lualine_laststatus
187
188 local opts = {
189 options = {
190 theme = "auto",
191 globalstatus = vim.o.laststatus == 3,
192 disabled_filetypes = { statusline = { "dashboard", "alpha", "ministarter", "snacks_dashboard" } },
193 },
194 sections = {
195 lualine_a = { "mode" },
196 lualine_b = { "branch" },
197
198 lualine_c = {
199 {
200 "diagnostics",
201 symbols = {
202 error = icons.diagnostics.Error,
203 warn = icons.diagnostics.Warn,
204 info = icons.diagnostics.Info,
205 hint = icons.diagnostics.Hint,
206 },
207 },
208 { "filetype", icon_only = true, separator = "", padding = { left = 1, right = 0 } },
209 { M.pretty_path() },
210 },
211 lualine_x = {
212 Snacks.profiler.status(),
213 -- stylua: ignore
214 {
215 function() return " " .. require("dap").status() end,
216 cond = function() return package.loaded["dap"] and require("dap").status() ~= "" end,
217 color = function() return { fg = Snacks.util.color("Debug") } end,
218 },
219 -- stylua: ignore
220 {
221 require("lazy.status").updates,
222 cond = require("lazy.status").has_updates,
223 color = function() return { fg = Snacks.util.color("Special") } end,
224 },
225 {
226 "diff",
227 symbols = {
228 added = icons.git.added,
229 modified = icons.git.modified,
230 removed = icons.git.removed,
231 },
232 source = function()
233 local gitsigns = vim.b.gitsigns_status_dict
234 if gitsigns then
235 return {
236 added = gitsigns.added,
237 modified = gitsigns.changed,
238 removed = gitsigns.removed,
239 }
240 end
241 end,
242 },
243 },
244 lualine_y = {
245 { "progress", separator = " ", padding = { left = 1, right = 0 } },
246 { "location", padding = { left = 0, right = 1 } },
247 },
248 lualine_z = {
249 function()
250 return " " .. os.date("%R")
251 end,
252 },
253 },
254 inactive_sections = {
255 lualine_c = {
256 { "filetype", icon_only = true, separator = "", padding = { left = 1, right = 0 } },
257 { M.pretty_path() },
258 },
259 },
260 extensions = { "neo-tree", "lazy", "fzf" },
261 }
262
263 local trouble = require("trouble")
264 local symbols = trouble.statusline({
265 mode = "symbols",
266 groups = {},
267 title = false,
268 filter = { range = true },
269 format = "{kind_icon}{symbol.name:Normal}",
270 hl_group = "lualine_c_normal",
271 })
272 table.insert(opts.sections.lualine_c, {
273 symbols and symbols.get,
274 cond = function()
275 return vim.b.trouble_lualine ~= false and symbols.has()
276 end,
277 })
278
279 return opts
280 end,
281 },
282 {
283 "echasnovski/mini.icons",
284 lazy = true,
285 opts = {
286 file = {
287 [".keep"] = { glyph = "󰊢", hl = "MiniIconsGrey" },
288 ["devcontainer.json"] = { glyph = "", hl = "MiniIconsAzure" },
289 },
290 filetype = {
291 dotenv = { glyph = "", hl = "MiniIconsYellow" },
292 },
293 },
294 init = function()
295 -- TODO: try without this
296 -- see :h MiniIcons.mock_nvim_web_devicons()
297 package.preload["nvim-web-devicons"] = function()
298 require("mini.icons").mock_nvim_web_devicons()
299 return package.loaded["nvim-web-devicons"]
300 end
301 end,
302 },
303 { "MunifTanjim/nui.nvim", lazy = true },
304 { "stevearc/dressing.nvim",
305 enabled = false, -- replaced by snacks.input
306 lazy = true,
307 opts = {
308 input = {
309 start_in_insert = false,
310 insert_only = false,
311 },
312 },
313 },
314 { "indent-blankline.nvim",
315 enabled = false, -- replaced by snacks.indent
316 },
317 { "echasnovski/mini.indentscope",
318 enabled = false, -- replaced by snacks.indent
319 version = false, -- wait till new 0.7.0 release to put it back on semver
320 event = "LazyFile",
321 opts = function (_, opts)
322 return {
323 draw = {
324 animation = require("mini.indentscope").gen_animation.linear({ duration = 10 })
325 }
326 }
327 end,
328 },
329 { "SmiteshP/nvim-navic",
330 lazy = true,
331 init = function()
332 vim.g.navic_silence = true
333 LazyVim.lsp.on_attach(function(client, buffer)
334 if client.supports_method("textDocument/documentSymbol") then
335 require("nvim-navic").attach(client, buffer)
336 end
337 end)
338 end,
339 opts = function()
340 return {
341 separator = " ",
342 highlight = true,
343 depth_limit = 5,
344 icons = LazyVim.config.icons.kinds,
345 lazy_update_context = true,
346 }
347 end,
348 },
349 { "nvim-lualine/lualine.nvim",
350 optional = true,
351 opts = function(_, opts)
352 if not vim.g.trouble_lualine then
353 table.insert(opts.sections.lualine_c, { "navic", color_correction = "dynamic" })
354 end
355 end,
356 },
357 }