diff --git a/README.md b/README.md index 6a70b863..fa8e7ae9 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ currently only marks are supported in telescope ## ⇁ Configuration if configuring harpoon is desired it must be done through harpoons setup function ```lua -require("harpoon").setup({ ... }) +require("harpoon").setup({ global_settings = { ... }, projects = { ... }}) ``` ### Global Settings @@ -152,8 +152,12 @@ global_settings = { -- enable tabline with harpoon marks tabline = false, + tabline_icons = false, -- requires nvim-web-devicons tabline_prefix = " ", tabline_suffix = " ", + tabline_previous_buffer_text = ":b#", + tabline_show_previous_buffer = true, + tabline_show_current_buffer_not_added = true, } ``` diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index b928178c..db994ebe 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -10,10 +10,8 @@ local cache_config = string.format("%s/harpoon.json", data_path) local M = {} -local the_primeagen_harpoon = vim.api.nvim_create_augroup( - "THE_PRIMEAGEN_HARPOON", - { clear = true } -) +local the_primeagen_harpoon = + vim.api.nvim_create_augroup("THE_PRIMEAGEN_HARPOON", { clear = true }) vim.api.nvim_create_autocmd({ "BufLeave, VimLeave" }, { callback = function() @@ -211,6 +209,10 @@ function M.setup(config) ["excluded_filetypes"] = { "harpoon" }, ["mark_branch"] = false, ["tabline"] = false, + ["tabline_icons"] = false, + ["tabline_previous_buffer_text"] = "#", + ["tabline_show_previous_buffer"] = true, + ["tabline_show_current_buffer_not_added"] = true, ["tabline_suffix"] = " ", ["tabline_prefix"] = " ", }, @@ -220,7 +222,7 @@ function M.setup(config) -- an object for vim.loop.cwd() ensure_correct_config(complete_config) - if complete_config.tabline then + if complete_config.global_settings.tabline then require("harpoon.tabline").setup(complete_config) end diff --git a/lua/harpoon/tabline.lua b/lua/harpoon/tabline.lua index cc6f874a..8ba67b62 100644 --- a/lua/harpoon/tabline.lua +++ b/lua/harpoon/tabline.lua @@ -7,7 +7,6 @@ local function get_color(group, attr) return vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(group)), attr) end - local function shorten_filenames(filenames) local shortened = {} @@ -21,7 +20,10 @@ local function shorten_filenames(filenames) local name = vim.fn.fnamemodify(file.filename, ":t") if counts[name] == 1 then - table.insert(shortened, { filename = vim.fn.fnamemodify(name, ":t") }) + table.insert( + shortened, + { filename = vim.fn.fnamemodify(name, ":t") } + ) else table.insert(shortened, { filename = file.filename }) end @@ -32,10 +34,13 @@ end function M.setup(opts) function _G.tabline() - local tabs = shorten_filenames(require('harpoon').get_mark_config().marks) - local tabline = '' + local tabs = + shorten_filenames(require("harpoon").get_mark_config().marks) + local tabline = "" - local index = require('harpoon.mark').get_index_of(vim.fn.bufname()) + local index = require("harpoon.mark").get_index_of(vim.fn.bufname()) + local cfg = opts.global_settings + local has_icons = cfg.tabline_icons for i, tab in ipairs(tabs) do local is_current = i == index @@ -49,40 +54,176 @@ function M.setup(opts) label = tab.filename end - - if is_current then - tabline = tabline .. - '%#HarpoonNumberActive#' .. (opts.tabline_prefix or ' ') .. i .. ' %*' .. '%#HarpoonActive#' + if has_icons then + local extension = tab.filename:match("^.+(%..+)$") or "" + local mime, color = require("nvim-web-devicons").get_icon( + tab.filename, + extension:sub(2, #extension), + { default = true } + ) + + if is_current then + tabline = tabline + .. "%#HarpoonNumberActive#" + .. (cfg.tabline_prefix or " ") + .. i + .. " %*%#" + .. color + .. "#" + .. mime + .. "%*%#HarpoonActive# " + else + tabline = tabline + .. "%#HarpoonNumberInactive#" + .. (cfg.tabline_prefix or " ") + .. i + .. " %*%#" + .. color + .. "#" + .. mime + .. "%*" + .. "%#HarpoonInactive# " + end else - tabline = tabline .. - '%#HarpoonNumberInactive#' .. (opts.tabline_prefix or ' ') .. i .. ' %*' .. '%#HarpoonInactive#' + if is_current then + tabline = tabline + .. "%#HarpoonNumberActive#" + .. (cfg.tabline_prefix or " ") + .. i + .. " %*" + .. "%#HarpoonActive#" + else + tabline = tabline + .. "%#HarpoonNumberInactive#" + .. (opts.tabline_prefix or " ") + .. i + .. " %*" + .. "%#HarpoonInactive#" + end end - tabline = tabline .. label .. (opts.tabline_suffix or ' ') .. '%*' + tabline = tabline .. label .. (cfg.tabline_suffix or " ") .. "%*" if i < #tabs then - tabline = tabline .. '%T' + tabline = tabline .. "%T" + end + end + + if + vim.fn.bufexists("#") + and not (require("harpoon.mark").get_index_of( + vim.fn.bufname("#") or "" + )) + and vim.fn.bufname("#") ~= "" + then + if cfg.tabline_show_previous_buffer then + local previous_buffer_filename = + vim.fn.fnamemodify(vim.fn.bufname("#"), ":t") + if cfg.tabline_icons then + local extension = previous_buffer_filename:match( + "^.+(%..+)$" + ) or "" + -- local extension = tab.filename:match("^.+(%..+)$") or "" + local mime, color = require("nvim-web-devicons").get_icon( + previous_buffer_filename, + extension:sub(2, #extension), + { default = true } + ) + + tabline = tabline + .. "%T" + .. "%#HarpoonNumberInactive#" + .. (cfg.tabline_prefix or " ") + .. (cfg.tabline_previous_buffer_text or ":b#") + .. " %*%#" + .. color + .. "#" + .. mime + .. "%*%#HarpoonInactive# " + .. previous_buffer_filename + .. (cfg.tabline_suffix or " ") + else + tabline = tabline + .. "%#HarpoonNumberInactive#" + .. (cfg.tabline_prefix or " ") + .. (cfg.tabline_previous_buffer_text or ":b#") + .. " %*" + .. "%#HarpoonInactive#" + .. previous_buffer_filename + .. (cfg.tabline_suffix or " ") + end end end + if (not index) and vim.fn.bufname() ~= "" then + if cfg.tabline_show_current_buffer_not_added then + local current_buffer_filename = + vim.fn.fnamemodify(vim.fn.bufname(), ":t") + + if has_icons then + local extension = current_buffer_filename:match( + "^.+(%..+)$" + ) or "" + -- local extension = tab.filename:match("^.+(%..+)$") or "" + local mime, color = require("nvim-web-devicons").get_icon( + current_buffer_filename, + extension:sub(2, #extension), + { default = true } + ) + + tabline = tabline + .. "%T" + .. "%#HarpoonNumberActive#" + .. (cfg.tabline_prefix or " ") + .. "?" + .. " %*%#" + .. color + .. "#" + .. mime + .. "%*%#HarpoonActive# " + .. current_buffer_filename + .. (cfg.tabline_suffix or " ") + else + tabline = tabline + .. "%#HarpoonNumberActive#" + .. (cfg.tabline_prefix or " ") + .. "?" + .. " %*" + .. "%#HarpoonActive#" + .. current_buffer_filename + .. (cfg.tabline_suffix or " ") + end + end + end + + tabline = tabline .. "%*" + return tabline end vim.opt.showtabline = 2 - vim.o.tabline = '%!v:lua.tabline()' + vim.o.tabline = "%!v:lua.tabline()" vim.api.nvim_create_autocmd("ColorScheme", { group = vim.api.nvim_create_augroup("harpoon", { clear = true }), pattern = { "*" }, callback = function() - local color = get_color('HarpoonActive', 'bg#') + local color = get_color("HarpoonActive", "bg#") - if (color == "" or color == nil) then + if color == "" or color == nil then vim.api.nvim_set_hl(0, "HarpoonInactive", { link = "Tabline" }) vim.api.nvim_set_hl(0, "HarpoonActive", { link = "TablineSel" }) - vim.api.nvim_set_hl(0, "HarpoonNumberActive", { link = "TablineSel" }) - vim.api.nvim_set_hl(0, "HarpoonNumberInactive", { link = "Tabline" }) + vim.api.nvim_set_hl( + 0, + "HarpoonNumberActive", + { link = "TablineSel" } + ) + vim.api.nvim_set_hl( + 0, + "HarpoonNumberInactive", + { link = "Tabline" } + ) end end, })