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

To resolve the conflict #28

Merged
merged 32 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
29d023c
add book mark tree, and now support create folder
shanlihou Jul 25, 2024
6ffffe4
add cut and paste
shanlihou Jul 25, 2024
f74ef15
could collapse and delete
shanlihou Jul 26, 2024
1bed5a9
could open bookmark
shanlihou Jul 26, 2024
1cd2c81
fix some bug
shanlihou Jul 26, 2024
49a7a98
fix bug: could not add tree node if there is not existed
shanlihou Jul 29, 2024
b03c19c
remove tree prop,and extend the bookmarks contains bookmark and bookm…
shanlihou Aug 1, 2024
c5ddb05
fix hls of cut
shanlihou Aug 1, 2024
87b0e00
modify bookmark icon and add some documentation
shanlihou Aug 1, 2024
8934175
some code refactor
shanlihou Aug 1, 2024
a958349
migrate
shanlihou Aug 2, 2024
0824dd1
use s to active the current book mark list
shanlihou Aug 2, 2024
4b213b7
fix bug that rename bookmark_scope back
shanlihou Aug 2, 2024
617eed8
add copy operation
shanlihou Aug 2, 2024
b3988c0
fix create folder but active
shanlihou Aug 2, 2024
a6b3f8e
update sign highlight after goto-bookmark && more config
LintaoAmons Aug 2, 2024
c392a3c
bookmark tree in right window && open bookmark in previous window
LintaoAmons Aug 2, 2024
ec9226c
update readme for treeview next step
LintaoAmons Aug 2, 2024
dad700d
prevent unexpected operation key
LintaoAmons Aug 2, 2024
93693d4
fix stylua
LintaoAmons Aug 2, 2024
71f602c
refactor goto by gpt
LintaoAmons Aug 2, 2024
bfa1962
fix add bookmark exception when the frist level list has list
shanlihou Aug 2, 2024
e4179ae
allow lower nvim version
LintaoAmons Aug 3, 2024
cb00c81
refactor: move render module under tree module
LintaoAmons Aug 3, 2024
25b0bdc
refactor goto and stylua
LintaoAmons Aug 3, 2024
5a6c666
add refresh operator
LintaoAmons Aug 3, 2024
b09aa91
update config and remove unused function
LintaoAmons Aug 3, 2024
736c916
refactor: extract node model
LintaoAmons Aug 4, 2024
2dd9eec
fix sign
LintaoAmons Aug 4, 2024
121bc55
fix picker
LintaoAmons Aug 4, 2024
54e5f30
update readme
LintaoAmons Aug 4, 2024
0ad3f7e
fix stylua
LintaoAmons Aug 4, 2024
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
68 changes: 59 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Bookmarks.nvim

> v1.1.0: line-highlight-and-db-backup
>
> Function Preview: https://lintao-index.pages.dev/docs/Vim/Neovim/my-plugins-docs/bookmarks.nvim/release-log
Expand Down Expand Up @@ -36,16 +37,42 @@ return {
Right now we have only one config options

```lua
return { "LintaoAmons/bookmarks.nvim",
config = function ()
require("bookmarks").setup( {
return {
"LintaoAmons/bookmarks.nvim",
-- tag = "v0.5.4", -- optional, pin the plugin at specific version for stability
dependencies = {
{ "nvim-telescope/telescope.nvim" },
{ "stevearc/dressing.nvim" }, -- optional: to have the same UI shown in the GIF
},
config = function()
local opts = {
-- where you want to put your bookmarks db file (a simple readable json file, which you can edit manually as well, dont forget run `BookmarksReload` command to clean the cache)
json_db_path = vim.fs.normalize(vim.fn.stdpath("config") .. "/bookmarks.db.json"),
-- This is how the sign looks.
signs = {
mark = { icon = "󰃁", color = "red", line_bg= "#572626" },
mark = { icon = "󰃁", color = "red", line_bg = "#572626" },
},
-- optional, backup the json db file when a new neovim session started and you try to mark a place
-- you can find the file under the same folder
enable_backup = true,
-- treeview options
treeview = {
bookmark_format = function(bookmark)
return bookmark.name .. " [" .. bookmark.location.project_name .. "] " .. bookmark.location.relative_path .. " : " .. bookmark.content
end,
keymap = {
quit = { "q", "<ESC>" },
refresh = "R",
create_folder = "a",
tree_cut = "x",
tree_paste = "p",
collapse = "o",
delete = "d",
active = "s",
copy = "c",
},
},
-- do whatever you like by hooks
hooks = {
{
---a sample hook that change the working directory when goto bookmark
Expand All @@ -64,19 +91,22 @@ return { "LintaoAmons/bookmarks.nvim",
end,
},
},
})
end
}
require("bookmarks").setup(opts)
end,
}
```

</details>

## Commands and Keybindings
## Usage

There's two concepts in this plugin: `BookmarkList` and `Bookmark`.
There's two key concepts in this plugin: `BookmarkList` and `Bookmark`.

You can look into the code to find the structure of those two domain objects

### Commands

| Command | Description |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `BookmarksMark` | Mark current line into active BookmarkList. Rename existing bookmark under cursor. Toggle it off if the new name is an empty string |
Expand All @@ -85,6 +115,7 @@ You can look into the code to find the structure of those two domain objects
| `BookmarksGotoRecent` | Go to latest visited/created Bookmark |
| `BookmarksReload` | Clean the cache and resync the bookmarks jsonfile |
| `BookmarksEditJsonFile` | An shortcut to edit bookmark jsonfile, remember BookmarksReload to clean the cache after you finish editing |
| `BookmarksTree` | Display all bookmarks with tree-view, and use "cut", "paste", "create folder" to edit the tree. |

<details>
<summary>BookmarksCommands(subcommands) we have right now</summary>
Expand Down Expand Up @@ -125,6 +156,22 @@ vim.keymap.set("n", "<leader>ll", call_bookmark_command)

</details>

### Bookmark Treeview

<details>
<summary>BookmarksDisplay operations</summary>
a: add new folder
x: cut folder or bookmark
c: copy folder or bookmark
p: paste folder or bookmark
d: delete folder or bookmark
o: collapse or expand folder
s: active the current bookmark_list
q: quit
</details>

### Keymap

This plugin don't provide any default keybinding. I recommend you to have those three keybindings.

```lua
Expand Down Expand Up @@ -172,7 +219,10 @@ By [telegram](https://t.me/+ssgpiHyY9580ZWFl) or [微信: CateFat](https://linta
- [ ] Sequance diagram out of bookmarks: Pattern `[actor] -->actor sequance_number :: desc`
- [ ] buffer renderer
- [ ] filetree-like BookmarkList and Bookmark browsing.
- use `nui` or a `custom buffer`
- [x] MVP
- [ ] floating preview window
- [ ] better default bookmark render format(string format, then better UI)
- [ ] `u` undo. Expecially for unexpected `d` delete
- custom buffer (can render more things, and can nav/copy/paste with ease)
- local Keybindings
- popup window
Expand Down
8 changes: 1 addition & 7 deletions lua/bookmarks/adapter/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ local utils = require("bookmarks.utils")
---@param bookmark Bookmarks.Bookmark
---@param bookmarks Bookmarks.Bookmark[]
local function format(bookmark, bookmarks)
local max_len_listname = 0
local max_len_name = 0
local max_len_path = 0

for _, b in ipairs(bookmarks) do
if b.listname and #b.listname > max_len_listname then
max_len_listname = #b.listname
end

if #b.name > max_len_name then
max_len_name = #b.name
end
Expand All @@ -23,8 +18,7 @@ local function format(bookmark, bookmarks)
end

return string.format(
"%-" .. max_len_listname .. "s %-" .. max_len_name .. "s %-" .. max_len_path .. "s",
bookmark.listname or "",
"%-" .. max_len_name .. "s %-" .. max_len_path .. "s",
bookmark.name,
bookmark.location.project_name .. "/" .. utils.shorten_file_path(bookmark.location.relative_path)
)
Expand Down
6 changes: 4 additions & 2 deletions lua/bookmarks/adapter/picker.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local repo = require("bookmarks.repo")
local _mark_repo = require("bookmarks.repo.bookmark")
local _bookmark_list = require("bookmarks.domain").bookmark_list
local common = require("bookmarks.adapter.common")

-- TODO: check dependencies firstly
Expand Down Expand Up @@ -63,12 +65,12 @@ local function pick_bookmark(callback, opts)
local bookmarks
local bookmark_list_name
if opts.all then
bookmarks = repo.mark.read.find_all()
bookmarks = _mark_repo.read.find_all()
bookmark_list_name = "All"
else
local bookmark_list = opts.bookmark_list or repo.bookmark_list.write.find_or_set_active()
bookmark_list_name = bookmark_list.name
bookmarks = bookmark_list.bookmarks
bookmarks = _bookmark_list.get_all_marks(bookmark_list)
end

table.sort(bookmarks, function(a, b)
Expand Down
13 changes: 6 additions & 7 deletions lua/bookmarks/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ local function add_list(param)
end, bookmark_lists)

---@type Bookmarks.BookmarkList
local new_list = domain.bookmark_list.new(param.name, repo.generate_datetime_id())
local new_list = domain.bookmark_list.new(param.name, utils.generate_datetime_id())

table.insert(new_lists, new_list)
repo.bookmark_list.write.save_all(new_lists)
Expand Down Expand Up @@ -93,6 +93,8 @@ local function goto_bookmark(bookmark, opts)
for _, hook in ipairs(hooks) do
hook(bookmark, projects)
end

sign.refresh_signs()
end

local function goto_last_visited_bookmark()
Expand All @@ -108,6 +110,8 @@ local function goto_last_visited_bookmark()
if last_bookmark then
goto_bookmark(last_bookmark)
end

sign.refresh_signs()
end

-- TODO: trigger by `BufferEnter` Event
Expand Down Expand Up @@ -171,11 +175,6 @@ local function open_bookmarks_jsonfile()
vim.cmd("e " .. vim.g.bookmarks_config.json_db_path)
end

local function buffer_display()
local lists = repo.bookmark_list.read.find_all()
require("bookmarks.render.main").render(lists)
end

return {
mark = mark,
rename_bookmark = rename_bookmark,
Expand All @@ -194,5 +193,5 @@ return {
open_bookmarks_jsonfile = open_bookmarks_jsonfile,
},

buffer_display = buffer_display,
tree = require("bookmarks.tree.api"),
}
52 changes: 47 additions & 5 deletions lua/bookmarks/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,59 @@
---@field signs Signs
---@field hooks? Bookmarks.Hook[]
local default_config = {
-- where you want to put your bookmarks db file (a simple readable json file, which you can edit manually as well, dont forget run `BookmarksReload` command to clean the cache)
json_db_path = vim.fs.normalize(vim.fn.stdpath("config") .. "/bookmarks.db.json"),
-- This is how the sign looks.
signs = {
mark = { icon = "󰃁", color = "grey", line_bg = "#572626" },
-- annotation = { icon = "󰆉", color = "grey" }, -- TODO:
mark = { icon = "󰃁", color = "red", line_bg = "#572626" },
},
-- optional, backup the json db file when a new neovim session started and you try to mark a place
-- you can find the file under the same folder
enable_backup = true,
-- treeview options
treeview = {
bookmark_format = function(bookmark)
return bookmark.name
.. " ["
.. bookmark.location.project_name
.. "] "
.. bookmark.location.relative_path
.. " : "
.. bookmark.content
end,
keymap = {
quit = { "q", "<ESC>" },
refresh = "R",
create_folder = "a",
tree_cut = "x",
tree_paste = "p",
collapse = "o",
delete = "d",
active = "s",
copy = "c",
},
},
-- do whatever you like by hooks
hooks = {},
hooks = {
{
---a sample hook that change the working directory when goto bookmark
---@param bookmark Bookmarks.Bookmark
---@param projects Bookmarks.Project[]
callback = function(bookmark, projects)
local project_path
for _, p in ipairs(projects) do
if p.name == bookmark.location.project_name then
project_path = p.path
end
end
if project_path then
vim.cmd("cd " .. project_path)
end
end,
},
},
}

vim.g.bookmarks_config = default_config

---@param user_config? Bookmarks.Config
local setup = function(user_config)
local cfg = vim.tbl_deep_extend("force", vim.g.bookmarks_config or default_config, user_config or {})
Expand All @@ -26,4 +67,5 @@ end

return {
setup = setup,
default_config = default_config,
}
2 changes: 1 addition & 1 deletion lua/bookmarks/domain/bookmark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local location_scope = require("bookmarks.domain.location")
---@field location Bookmarks.Location
---@field content string
---@field githash string
---@field listname? string
---@field listname? string -- TODO: remove this field, only used in repo when trying to find all marks, which is not reasonable
---@field created_at number
---@field visited_at number

Expand Down
56 changes: 52 additions & 4 deletions lua/bookmarks/domain/bookmark_list.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
local bookmark_scope = require("bookmarks.domain.bookmark")
local utils = require("bookmarks.utils")
local _type = require("bookmarks.domain.type").type
local _get_value_type = require("bookmarks.domain.type").get_value_type

---@class Bookmarks.BookmarkList
---@field id string
---@field name string
---@field is_active boolean
---@field project_path_name_map {string: string}
---@field bookmarks Bookmarks.Bookmark[]
---@field project_path_name_map {string: string} -- bookmark specific path_name map, used to allow overwrite the project path to share with others
---@field bookmarks Bookmarks.Node[]
---@field collapse boolean treeview runtime status, may need refactor this field later

local M = {}

Expand All @@ -32,8 +36,12 @@ end
function M.find_bookmark_by_location(self, location)
-- TODO: self location path
for _, b in ipairs(self.bookmarks) do
if b.location.path == location.path and b.location.line == location.line then
return b
local b_type = _get_value_type(b)
if b_type == _type.BOOKMARK then
---@cast b Bookmarks.Bookmark
if b.location.path == location.path and b.location.line == location.line then
return b
end
end
end
return nil
Expand Down Expand Up @@ -88,4 +96,44 @@ function M.contains_bookmark(self, bookmark, projects)
return false
end

---@param self Bookmarks.BookmarkList
---@param id string | number
---@return Bookmarks.Bookmark?
function M.find_bookmark_by_id(self, id)
for _, b in ipairs(self.bookmarks) do
if b.id == id then
---@type Bookmarks.Bookmark
return b
end

if _get_value_type(b) == _type.BOOKMARK_LIST then
---@cast b Bookmarks.BookmarkList
M.find_bookmark_by_id(b, id)
end
end
return nil
end

---get all bookmarks in one dimension array
---@param self Bookmarks.BookmarkList
---@return Bookmarks.Bookmark[]
function M.get_all_marks(self)
local r = {}

local function __get_all_marks(list, result)
for _, b in ipairs(list.bookmarks) do
if _get_value_type(b) == _type.BOOKMARK then
---@cast b Bookmarks.Bookmark
table.insert(result, b)
else
__get_all_marks(b, result)
end
end
end

__get_all_marks(self, r)

return r
end

return M
Loading