From a14bd4f1586e2117c6c697c781cba5c9f3d2ca3b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 14 Dec 2024 15:31:01 +0800 Subject: [PATCH] feat(zettelkasten): sort tags in sidebar --- .../vim-zettelkasten/ftplugin/zkbrowser.lua | 2 +- .../vim-zettelkasten/ftplugin/zktagstree.lua | 33 ++++--- .../lua/zettelkasten/browser.lua | 26 ----- .../lua/zettelkasten/sidebar.lua | 98 +++++++++++++++++++ bundle/vim-zettelkasten/syntax/zktagstree.vim | 16 +++ 5 files changed, 136 insertions(+), 39 deletions(-) create mode 100644 bundle/vim-zettelkasten/lua/zettelkasten/sidebar.lua create mode 100644 bundle/vim-zettelkasten/syntax/zktagstree.vim diff --git a/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua b/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua index 632c8dc18..504dae9c3 100644 --- a/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua +++ b/bundle/vim-zettelkasten/ftplugin/zkbrowser.lua @@ -70,7 +70,7 @@ if vim.fn.mapcheck('[I', 'n') == '' then silent = true, nowait = true, callback = function() - require('zettelkasten.browser').open_tag_tree() + require('zettelkasten.sidebar').open_tag_tree() end, }) vim.api.nvim_buf_set_keymap(0, 'n', '', '', { diff --git a/bundle/vim-zettelkasten/ftplugin/zktagstree.lua b/bundle/vim-zettelkasten/ftplugin/zktagstree.lua index 6269c97b5..3569ba269 100644 --- a/bundle/vim-zettelkasten/ftplugin/zktagstree.lua +++ b/bundle/vim-zettelkasten/ftplugin/zktagstree.lua @@ -6,12 +6,17 @@ vim.opt_local.cursorline = true vim.opt_local.modifiable = false vim.opt_local.buflisted = false vim.opt_local.number = false +-- vim.opt_local.iskeyword:append(':') +vim.opt_local.iskeyword:append('-') vim.opt_local.relativenumber = false vim.opt_local.bufhidden = 'wipe' vim.opt_local.syntax = 'zktagstree' vim.opt_local.buftype = 'nofile' vim.opt_local.swapfile = false vim.opt_local.winfixwidth = true + +local hi = require('spacevim.api.vim.highlight') + vim.api.nvim_buf_set_keymap(0, 'n', '', '', { noremap = true, silent = true, @@ -31,7 +36,7 @@ vim.api.nvim_buf_set_keymap(0, 'n', '', '', { 0, -1, false, - require('zettelkasten').get_note_browser_content({ tags = { vim.fn.getline('.') } }) + require('zettelkasten').get_note_browser_content({ tags = { vim.fn.trim(vim.fn.getline('.')) } }) ) end vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr }) @@ -42,17 +47,21 @@ vim.api.nvim_buf_set_keymap(0, 'n', '', '', { silent = true, nowait = true, callback = function() - local bufnr = vim.fn.bufnr('zk://browser') - if vim.api.nvim_buf_is_valid(bufnr) then - vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr }) - vim.api.nvim_buf_set_lines( - bufnr, - 0, - -1, - false, - require('zettelkasten').get_note_browser_content({ tags = { vim.fn.getline('.') } }) - ) + if hi.syntax_at() == 'zktagstreeOrg' then + require('zettelkasten.sidebar').toggle_folded_key() + else + local bufnr = vim.fn.bufnr('zk://browser') + if vim.api.nvim_buf_is_valid(bufnr) then + vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr }) + vim.api.nvim_buf_set_lines( + bufnr, + 0, + -1, + false, + require('zettelkasten').get_note_browser_content({ tags = { vim.fn.trim(vim.fn.getline('.')) } }) + ) + end + vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr }) end - vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr }) end, }) diff --git a/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua b/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua index 1ffd6b4e2..c918cd2b0 100644 --- a/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua +++ b/bundle/vim-zettelkasten/lua/zettelkasten/browser.lua @@ -239,30 +239,4 @@ function M.browse(opt) vim.opt_local.buflisted = false vim.opt_local.modifiable = false end -local function unique_string_table(t) - local temp = {} - for _, k in ipairs(t) do - temp[k] = true - end - local rst = {} - for m, _ in pairs(temp) do - table.insert(rst, m) - end - return rst -end - -function M.open_tag_tree() - vim.cmd('30vsplit zk://tags_tree') - vim.opt_local.filetype = 'zktagstree' - vim.opt_local.modifiable = true - local lines = {} - local result = M.get_tags() - - for _, tag in ipairs(result) do - table.insert(lines, tag.name) - end - vim.api.nvim_buf_set_lines(0, 0, -1, false, unique_string_table(lines)) - vim.opt_local.buflisted = false - vim.opt_local.modifiable = false -end return M diff --git a/bundle/vim-zettelkasten/lua/zettelkasten/sidebar.lua b/bundle/vim-zettelkasten/lua/zettelkasten/sidebar.lua new file mode 100644 index 000000000..b9c910fbb --- /dev/null +++ b/bundle/vim-zettelkasten/lua/zettelkasten/sidebar.lua @@ -0,0 +1,98 @@ +--============================================================================= +-- sidebar.lua --- sidebar for zettelkasten plugin +-- Copyright (c) 2019-2024 Wang Shidong & Contributors +-- Author: Wang Shidong < wsdjeg@outlook.com > +-- URL: https://spacevim.org +-- License: GPLv3 +--============================================================================= + +local browser = require('zettelkasten.browser') + +local M = {} + +local folded_keys = {} + +local function unique_string_table(t) + local temp = {} + for _, k in ipairs(t) do + temp[k] = true + end + local rst = {} + for m, _ in pairs(temp) do + table.insert(rst, m) + end + return rst +end + +-- 按照首字母归类 + +local function get_sorted_keys(t) + local keys = {} + + for k, _ in pairs(t) do + table.insert(keys, k) + end + return vim.fn.sort(keys) +end + +local function sort_tags(tags) + local atags = {} + + for _, tag in ipairs(vim.fn.sort(tags)) do + local k = string.upper(string.sub(tag, 2, 2)) + if atags[k] then + table.insert(atags[k], tag) + else + atags[k] = { tag } + end + end + + local lines = {} + + -- ▼ functions + -- ▶ functions + for _, k in ipairs(get_sorted_keys(atags)) do + if #atags[k] > 0 then + if not folded_keys[k] then + table.insert(lines, '▼ ' .. k) + for _, t in ipairs(atags[k]) do + table.insert(lines, ' ' .. t) + end + else + table.insert(lines, '▶ ' .. k) + end + end + end + return lines +end + +local function update_sidebar_context() + vim.opt_local.modifiable = true + local lines = {} + local result = browser.get_tags() + for _, tag in ipairs(result) do + table.insert(lines, tag.name) + end + vim.api.nvim_buf_set_lines(0, 0, -1, false, sort_tags(unique_string_table(lines))) + vim.opt_local.buflisted = false + vim.opt_local.modifiable = false +end + +function M.open_tag_tree() + vim.cmd('30vsplit zk://tags_tree') + vim.opt_local.filetype = 'zktagstree' + folded_keys = {} + update_sidebar_context() +end + +function M.toggle_folded_key() + local k = string.sub(vim.fn.getline('.'), 5, 5) + if folded_keys[k] then + folded_keys[k] = false + else + folded_keys[k] = true + end + update_sidebar_context() +end + +return M diff --git a/bundle/vim-zettelkasten/syntax/zktagstree.vim b/bundle/vim-zettelkasten/syntax/zktagstree.vim new file mode 100644 index 000000000..9a0fa4a82 --- /dev/null +++ b/bundle/vim-zettelkasten/syntax/zktagstree.vim @@ -0,0 +1,16 @@ +if "zktagstree" !=# get(b:, "current_syntax", "zktagstree") + finish +endif + +" syntax match ZettelKastenID '[0-9]\+-[0-9]\+-[0-9]\+-[0-9]\+-[0-9]\+-[0-9]\+\.md' +" syntax match ZettelKastenDash '\s-\s' +syntax match zktagstreeOrg '[▼▶]\+ .*' +syntax match zktagstreeTags '#\<\k\+\>' + +" highlight default link ZettelKastenID String +" highlight default link ZettelKastenDash Comment +highlight default link zktagstreeOrg Number +highlight default link zktagstreeTags Tag + +let b:current_syntax = "zktagstree" +