Skip to content

Commit

Permalink
update sign config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Apr 27, 2024
1 parent d1f4f59 commit 851a7e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
11 changes: 5 additions & 6 deletions lua/bookmarks/config.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---@class Bookmarks.Config
---@field sign { icon: string, highlight: string }
---@field signs Signs
local default_config = {
json_db_path = vim.fs.normalize(vim.fn.stdpath("config") .. "/bookmarks.db.json"),
sign = {
icon = "󰃁",
highlight = "BookmarksNvimSign",
signs = {
mark = { icon = "󰃁", color = "grey" },
-- annotation = { icon = "󰆉", color = "grey" },
},
}

Expand All @@ -14,8 +14,7 @@ vim.g.bookmarks_config = default_config
local setup = function(user_config)
local cfg = vim.tbl_deep_extend("force", vim.g.bookmarks_config or default_config, user_config or {})
or default_config
vim.fn.sign_define(cfg.sign.highlight, { text = cfg.sign.icon, texthl = cfg.sign.highlight })
vim.api.nvim_set_hl(0, cfg.sign.highlight, { foreground = "grey" }) -- control the color of the sign
require("bookmarks.sign").setup(cfg.signs)
vim.g.bookmarks_config = cfg
end

Expand Down
22 changes: 20 additions & 2 deletions lua/bookmarks/sign.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
local repo = require("bookmarks.repo")
local ns_name = "BookmarksNvim"
local hl_name = "BookmarksNvimSign"
local ns = vim.api.nvim_create_namespace(ns_name)

---@class Signs
---@field mark Sign
-- ---@field annotation Sign -- TODO:

---@class Sign
---@field icon string
---@field color ?string

---@param signs Signs
local function setup(signs)
for k, _ in pairs(signs) do
vim.fn.sign_define(hl_name, { text = signs[k].icon, texthl = hl_name })
vim.api.nvim_set_hl(0, hl_name, { foreground = signs[k].color })
end
end

---@param line number
local function place_sign(line, buf_number, desc)
vim.fn.sign_place(line, ns_name, vim.g.bookmarks_config.sign.highlight, buf_number, { lnum = line })
vim.fn.sign_place(line, ns_name, hl_name, buf_number, { lnum = line })
local at_end = -1
vim.api.nvim_buf_set_extmark(buf_number, ns, line - 1, at_end, {
virt_text = { { " " .. desc, vim.g.bookmarks_config.sign.highlight } },
virt_text = { { " " .. desc, hl_name } },
virt_text_pos = "overlay",
hl_mode = "combine",
})
Expand Down Expand Up @@ -52,6 +69,7 @@ local function bookmark_sign_autocmd()
end

return {
setup = setup,
bookmark_sign_autocmd = bookmark_sign_autocmd,
refresh_signs = safe_refresh_signs,
}

0 comments on commit 851a7e7

Please sign in to comment.