1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 13:30:05 +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,13 +4,18 @@
" Author: Wang Shidong < wsdjeg@outlook.com > " Author: Wang Shidong < wsdjeg@outlook.com >
" License: GPLv3 " 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:JOB = SpaceVim#api#import('job')
let s:NOTI = SpaceVim#api#import('notify') let s:NOTI = SpaceVim#api#import('notify')
let s:push_jobid = 0 let s:push_jobid = 0
function! git#push#run(...) abort function! git#push#run(...) abort
if s:push_jobid != 0 if s:push_jobid != 0
call s:NOTI.notify('previous push not finished') call s:NOTI.notify('previous push not finished')
@ -38,9 +43,9 @@ function! git#push#run(...) abort
let s:push_jobid = 0 let s:push_jobid = 0
endif endif
endfunction endfunction
function! s:on_exit(id, data, event) abort function! s:on_exit(id, data, event) abort
if a:data != 0 if a:data != 0
for line in s:std_data.stderr for line in s:std_data.stderr
let s:NOTI.notify_max_width = max([strwidth(line) + 5, s:NOTI.notify_max_width]) let s:NOTI.notify_max_width = max([strwidth(line) + 5, s:NOTI.notify_max_width])
@ -53,34 +58,34 @@ function! s:on_exit(id, data, event) abort
endfor endfor
endif endif
let s:push_jobid = 0 let s:push_jobid = 0
endfunction endfunction
function! s:on_stdout(id, data, event) abort function! s:on_stdout(id, data, event) abort
for line in filter(a:data, '!empty(v:val) && v:val !~# "^remote:"') for line in filter(a:data, '!empty(v:val) && v:val !~# "^remote:"')
let s:NOTI.notify_max_width = max([strwidth(line) + 5, s:NOTI.notify_max_width]) let s:NOTI.notify_max_width = max([strwidth(line) + 5, s:NOTI.notify_max_width])
call s:NOTI.notify(line, 'Normal') call s:NOTI.notify(line, 'Normal')
endfor endfor
endfunction endfunction
" https://stackoverflow.com/questions/57016157/how-to-stop-git-from-writing-non-errors-to-stderr " https://stackoverflow.com/questions/57016157/how-to-stop-git-from-writing-non-errors-to-stderr
" "
" why git push normal info to stderr " why git push normal info to stderr
function! s:on_stderr(id, data, event) abort function! s:on_stderr(id, data, event) abort
call extend(s:std_data.stderr, filter(a:data, '!empty(v:val) && v:val !~# "^remote:"')) call extend(s:std_data.stderr, filter(a:data, '!empty(v:val) && v:val !~# "^remote:"'))
endfunction endfunction
function! s:options() abort function! s:options() abort
return [ return [
\ '-u', \ '-u',
\ '--set-upstream', \ '--set-upstream',
\ '-d', '--delete' \ '-d', '--delete'
\ ] \ ]
endfunction endfunction
function! git#push#complete(ArgLead, CmdLine, CursorPos) abort function! git#push#complete(ArgLead, CmdLine, CursorPos) abort
let str = a:CmdLine[:a:CursorPos-1] let str = a:CmdLine[:a:CursorPos-1]
if str =~# '^Git\s\+push\s\+-$' if str =~# '^Git\s\+push\s\+-$'
return join(s:options(), "\n") return join(s:options(), "\n")
@ -90,13 +95,13 @@ function! git#push#complete(ArgLead, CmdLine, CursorPos) abort
let remote = matchstr(str, '\(Git\s\+push\s\+\)\@<=[^ ]*') let remote = matchstr(str, '\(Git\s\+push\s\+\)\@<=[^ ]*')
return s:remote_branch(remote) return s:remote_branch(remote)
endif endif
endfunction endfunction
function! s:remotes() abort function! s:remotes() abort
return map(systemlist('git remote'), 'trim(v:val)') return map(systemlist('git remote'), 'trim(v:val)')
endfunction endfunction
function! s:remote_branch(remote) abort function! s:remote_branch(remote) abort
let branchs = systemlist('git branch -a') let branchs = systemlist('git branch -a')
if v:shell_error if v:shell_error
return '' return ''
@ -104,4 +109,5 @@ function! s:remote_branch(remote) abort
let branchs = join(map(filter(branchs, 'v:val =~ "\s*remotes/" . a:remote . "/[^ ]*$"'), 'trim(v:val)[len(a:remote) + 9:]'), "\n") let branchs = join(map(filter(branchs, 'v:val =~ "\s*remotes/" . a:remote . "/[^ ]*$"'), 'trim(v:val)[len(a:remote) + 9:]'), "\n")
return branchs return branchs
endif endif
endfunction endfunction
endif

View File

@ -71,6 +71,43 @@ function M.run(argv)
end end
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 return M

View File

@ -70,7 +70,7 @@ if vim.fn.mapcheck('[I', 'n') == '' then
silent = true, silent = true,
nowait = true, nowait = true,
callback = function() callback = function()
require('zettelkasten.browser').open_tag_tree() require('zettelkasten.sidebar').open_tag_tree()
end, end,
}) })
vim.api.nvim_buf_set_keymap(0, 'n', '<Enter>', '', { 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.modifiable = false
vim.opt_local.buflisted = false vim.opt_local.buflisted = false
vim.opt_local.number = false vim.opt_local.number = false
-- vim.opt_local.iskeyword:append(':')
vim.opt_local.iskeyword:append('-')
vim.opt_local.relativenumber = false vim.opt_local.relativenumber = false
vim.opt_local.bufhidden = 'wipe' vim.opt_local.bufhidden = 'wipe'
vim.opt_local.syntax = 'zktagstree' vim.opt_local.syntax = 'zktagstree'
vim.opt_local.buftype = 'nofile' vim.opt_local.buftype = 'nofile'
vim.opt_local.swapfile = false vim.opt_local.swapfile = false
vim.opt_local.winfixwidth = true vim.opt_local.winfixwidth = true
local hi = require('spacevim.api.vim.highlight')
vim.api.nvim_buf_set_keymap(0, 'n', '<F2>', '', { vim.api.nvim_buf_set_keymap(0, 'n', '<F2>', '', {
noremap = true, noremap = true,
silent = true, silent = true,
@ -31,7 +36,7 @@ vim.api.nvim_buf_set_keymap(0, 'n', '<Enter>', '', {
0, 0,
-1, -1,
false, 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 end
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr }) 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, silent = true,
nowait = true, nowait = true,
callback = function() callback = function()
if hi.syntax_at() == 'zktagstreeOrg' then
require('zettelkasten.sidebar').toggle_folded_key()
else
local bufnr = vim.fn.bufnr('zk://browser') local bufnr = vim.fn.bufnr('zk://browser')
if vim.api.nvim_buf_is_valid(bufnr) then if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr }) vim.api.nvim_set_option_value('modifiable', true, { buf = bufnr })
@ -50,9 +58,10 @@ vim.api.nvim_buf_set_keymap(0, 'n', '<LeftRelease>', '', {
0, 0,
-1, -1,
false, 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 end
vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr }) vim.api.nvim_set_option_value('modifiable', false, { buf = bufnr })
end
end, end,
}) })

View File

@ -239,30 +239,4 @@ function M.browse(opt)
vim.opt_local.buflisted = false vim.opt_local.buflisted = false
vim.opt_local.modifiable = false vim.opt_local.modifiable = false
end 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 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"