Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Icon Support and fix #300

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
```

Expand Down
12 changes: 7 additions & 5 deletions lua/harpoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"] = " ",
},
Expand All @@ -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

Expand Down
177 changes: 159 additions & 18 deletions lua/harpoon/tabline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
})
Expand Down
Loading