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

Extend config to allow setting custom icon and highlight #18

Merged
merged 4 commits into from
Apr 29, 2024
Merged
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
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ return { "LintaoAmons/bookmarks.nvim",
config = function ()
require("bookmarks").setup( {
json_db_path = vim.fs.normalize(vim.fn.stdpath("config") .. "/bookmarks.db.json"),
signs = {
mark = { icon = "", color = "grey" },
},
})
end
}
Expand All @@ -49,12 +52,12 @@ return { "LintaoAmons/bookmarks.nvim",

There's two concepts in this plugin: `BookmarkList` and `Bookmark`. You can look into the code to find the structure of those two domain objects

| Command | Description |
|-----------------------------------|-------------------------------------------------------------------------------------------------------------|
| `BookmarksMark` | Mark current line into active BookmarkList. |
| `BookmarksGoto` | Go to bookmark at current active BookmarkList |
| `BookmarksCommands` | Find and trigger a bookmark command. |
| `BookmarksGotoRecent` | Go to latest visited/created Bookmark |
| Command | Description |
| --------------------- | --------------------------------------------- |
| `BookmarksMark` | Mark current line into active BookmarkList. |
| `BookmarksGoto` | Go to bookmark at current active BookmarkList |
| `BookmarksCommands` | Find and trigger a bookmark command. |
| `BookmarksGotoRecent` | Go to latest visited/created Bookmark |

<details>
<summary>Commands we have right now</summary>
Expand Down Expand Up @@ -113,7 +116,7 @@ You can contact with me by drop me an email or [telegram](https://t.me/+ssgpiHyY
- [ ] refactor: extract picker module
- [ ] Telescope as default picker and will fallback to vim.ui if don't have telescope dependencies
- [x] telescope enhancement (use specific command instead)
- [ ] Recent files as bookmarks: record all the buffer the user recently opened and sort by the visited_at
- [ ] Recent files as bookmarks: record all the buffer the user recently opened and sort by the visited_at
- [x] A new command to create bookmark and put it into specific bookmark list (instead current active one)
- [ ] goto next/prev bookmark in the current buffer

Expand Down
13 changes: 11 additions & 2 deletions lua/bookmarks/config.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
---@class Bookmarks.Config
---@field signs Signs
local default_config = {
json_db_path = vim.fs.normalize(vim.fn.stdpath("config") .. "/bookmarks.db.json"),
signs = {
mark = { icon = "󰃁", color = "grey" },
-- annotation = { icon = "󰆉", color = "grey" }, -- TODO:
},
}

vim.g.bookmarks_config = default_config

---@param user_config? Bookmarks.Config
local setup = function(user_config)
local previous_config = vim.g.bookmarks_config or default_config
vim.g.bookmarks_config = vim.tbl_deep_extend("force", previous_config, user_config or {}) or default_config
local cfg = vim.tbl_deep_extend("force", vim.g.bookmarks_config or default_config, user_config or {})
or default_config
require("bookmarks.sign").setup(cfg.signs)
vim.g.bookmarks_config = cfg
end

return {
Expand Down
25 changes: 19 additions & 6 deletions lua/bookmarks/sign.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
local repo = require("bookmarks.repo")
local ns_name = "BookmarksNvim"
local sign_name = "BookmarksNvimSign"
local sign_icon = "󰃁"
local hl_name = "BookmarksNvimSign"
local ns = vim.api.nvim_create_namespace(ns_name)

vim.fn.sign_define(sign_name, { text = sign_icon, texthl = sign_name })
vim.api.nvim_set_hl(0, sign_name, { foreground = "grey" }) -- control the color of the sign
---@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, sign_name, 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, sign_name } },
virt_text = { { " " .. desc, hl_name } },
virt_text_pos = "overlay",
hl_mode = "combine",
})
Expand Down Expand Up @@ -57,6 +69,7 @@ local function bookmark_sign_autocmd()
end

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