1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 11:30:06 +08:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Eric Wong
a14bd4f158 feat(zettelkasten): sort tags in sidebar 2024-12-14 16:54:38 +08:00
Eric Wong
6358c2a435 feat(git.vim): complete git push command 2024-12-14 15:11:02 +08:00
7 changed files with 269 additions and 129 deletions

View File

@ -4,6 +4,11 @@
" Author: Wang Shidong < wsdjeg@outlook.com >
" License: GPLv3
"=============================================================================
if has('nvim-0.9.0')
function! git#push#complete(ArgLead, CmdLine, CursorPos) abort
return luaeval('require("git.command.push").complete(vim.api.nvim_eval("a:ArgLead"), vim.api.nvim_eval("a:CmdLine"), vim.api.nvim_eval("a:CursorPos"))')
endfunction
else
let s:JOB = SpaceVim#api#import('job')
let s:NOTI = SpaceVim#api#import('notify')
@ -105,3 +110,4 @@ function! s:remote_branch(remote) abort
return branchs
endif
endfunction
endif

View File

@ -71,6 +71,43 @@ function M.run(argv)
end
end
function M.complete(ArgLead, CmdLine, CursorPos) end
local options = { '-u', '--set-upstream', '-d', '--delete' }
local function remotes()
return vim.tbl_map(function(t)
return vim.fn.trim(t)
end, vim.fn.systemlist('git remote'))
end
local function remote_branch(r)
local branchs = vim.fn.systemlist('git branch -a')
if vim.v.shell_error then
return ''
else
branchs = table.concat(
vim.fn.map(
vim.fn.filter(branchs, [[v:val =~ "\s*remotes/" . a:remote . "/[^ ]*$"]]),
'trim(v:val)[len(a:remote) + 9:]'
),
'\n'
)
return branchs
end
end
function M.complete(ArgLead, CmdLine, CursorPos)
local str = string.sub(CmdLine, 1, CursorPos)
if vim.regex([[^Git\s\+push\s\+-$]]):match_str(str) then
return table.concat(options, '\n')
elseif
vim.regex([[^Git\s\+push\s\+[^ ]*$]]):match_str(str)
or vim.regex([[^Git\s\+push\s\+-u\s\+[^ ]*$]]):match_str(str)
then
return table.concat(remotes(), '\n')
else
local remote = vim.fn.matchstr(str, [[\(Git\s\+push\s\+\)\@<=[^ ]*]])
return remote_branch(remote)
end
end
return M

View File

@ -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', '<Enter>', '', {

View File

@ -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', '<F2>', '', {
noremap = true,
silent = true,
@ -31,7 +36,7 @@ vim.api.nvim_buf_set_keymap(0, 'n', '<Enter>', '', {
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,6 +47,9 @@ vim.api.nvim_buf_set_keymap(0, 'n', '<LeftRelease>', '', {
silent = true,
nowait = true,
callback = function()
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 })
@ -50,9 +58,10 @@ vim.api.nvim_buf_set_keymap(0, 'n', '<LeftRelease>', '', {
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 })
end
end,
})

View File

@ -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

View File

@ -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

View File

@ -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"