From 49fff62e21b1c171fb3105fe7ade3c9bc8b71e23 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 14 Mar 2024 11:08:00 +0800 Subject: [PATCH] feat(history)!: use history plugin instead of shada can not fix https://github.com/neovim/neovim/issues/6875 always get error when open two nvim instances --- autoload/SpaceVim/autocmds.vim | 14 ++--- autoload/SpaceVim/layers/core.vim | 19 ++++++- autoload/SpaceVim/layers/ui.vim | 1 + autoload/SpaceVim/plugins/history.vim | 62 +++++++++++++++++++++++ bundle/vim-startify/autoload/startify.vim | 17 +++---- bundle/vim-startify/syntax/startify.vim | 2 +- docs/bundle-plugins.md | 8 +-- lua/spacevim/default.lua | 2 + 8 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 autoload/SpaceVim/plugins/history.vim diff --git a/autoload/SpaceVim/autocmds.vim b/autoload/SpaceVim/autocmds.vim index cc0c4f190..9531c760f 100644 --- a/autoload/SpaceVim/autocmds.vim +++ b/autoload/SpaceVim/autocmds.vim @@ -34,10 +34,7 @@ function! SpaceVim#autocmds#init() abort autocmd BufEnter,WinEnter,InsertLeave * setl cursorcolumn autocmd BufLeave,WinLeave,InsertEnter * setl nocursorcolumn endif - autocmd BufReadPost * - \ if line("'\"") > 0 && line("'\"") <= line("$") | - \ exe "normal! g`\"" | - \ endif + autocmd BufLeave * call SpaceVim#plugins#history#savepos() autocmd BufNewFile,BufEnter * set cpoptions+=d " NOTE: ctags find the tags file from the current path instead of the path of currect file autocmd BufWinLeave * let b:_winview = winsaveview() autocmd BufWinEnter * if(exists('b:_winview')) | call winrestview(b:_winview) | endif @@ -66,7 +63,12 @@ function! SpaceVim#autocmds#init() abort autocmd SessionLoadPost * let g:_spacevim_session_loaded = 1 autocmd VimLeavePre * call SpaceVim#plugins#manager#terminal() if has('nvim') - autocmd CursorHold,FocusGained,FocusLost * rshada|wshada + autocmd VimEnter,FocusGained * call SpaceVim#plugins#history#readcache() + autocmd FocusLost,VimLeave * call SpaceVim#plugins#history#writecache() + autocmd BufReadPost * + \ if line("'\"") > 0 && line("'\"") <= line("$") | + \ call SpaceVim#plugins#history#jumppos() | + \ endif endif augroup END endfunction @@ -89,7 +91,7 @@ function! s:reload_touchpad_status() abort if s:touchpadoff call s:disable_touchpad() endif -endf +endfunction function! s:disable_touchpad() abort let s:touchpadoff = 1 call system('synclient touchpadoff=1') diff --git a/autoload/SpaceVim/layers/core.vim b/autoload/SpaceVim/layers/core.vim index 9dfa2f1f9..c17116759 100644 --- a/autoload/SpaceVim/layers/core.vim +++ b/autoload/SpaceVim/layers/core.vim @@ -319,7 +319,9 @@ function! SpaceVim#layers#core#config() abort " TODO: fix all these command call SpaceVim#mapping#space#def('nnoremap', ['f', 't'], 'Defx', 'toggle-file-tree', 1) call SpaceVim#mapping#space#def('nnoremap', ['f', 'T'], 'Defx -no-toggle', 'show-file-tree', 1) - call SpaceVim#mapping#space#def('nnoremap', ['f', 'o'], "Defx -no-toggle -search=`expand('%:p')` `stridx(expand('%:p'), getcwd()) < 0? expand('%:p:h'): getcwd()`", 'open-file-tree', 1) + call SpaceVim#mapping#space#def('nnoremap', ['f', 'o'], 'call call(' + \ . string(s:_function('s:defx_find_current_file')) . ', [])', + \ 'open-file-tree', 1) call SpaceVim#mapping#space#def('nnoremap', ['b', 't'], 'exe "Defx -no-toggle " . fnameescape(expand("%:p:h"))', 'show-file-tree-at-buffer-dir', 1) elseif g:spacevim_filemanager ==# 'nvim-tree' call SpaceVim#mapping#space#def('nnoremap', ['f', 't'], 'NvimTreeToggle', 'toggle-file-tree', 1) @@ -1067,6 +1069,21 @@ function! SpaceVim#layers#core#set_variable(var) abort \ 0) endfunction +function! s:defx_find_current_file() abort + let current_file = s:FILE.unify_path(expand('%'), ':p') + let current_dir = s:FILE.unify_path(getcwd()) + + let command = "Defx -no-toggle -search=`expand('%:p')` " + if stridx(current_file, current_dir) < 0 + let command .= expand('%:p:h') + else + let command .= getcwd() + endif + + call execute(command) + +endfunction + function! SpaceVim#layers#core#get_options() abort return [ diff --git a/autoload/SpaceVim/layers/ui.vim b/autoload/SpaceVim/layers/ui.vim index 8f8d05a09..d617765c1 100644 --- a/autoload/SpaceVim/layers/ui.vim +++ b/autoload/SpaceVim/layers/ui.vim @@ -97,6 +97,7 @@ function! SpaceVim#layers#ui#plugins() abort \ 'EnableWhitespace' \ ]}], \ ] + call add(plugins, [g:_spacevim_root_dir . 'bundle/neomru.vim', {'merged' : 0}]) if (has('nvim-0.5.0') && s:NVIM_VERSION.is_release_version()) \ || has('nvim-0.6.0') call add(plugins, [g:_spacevim_root_dir . 'bundle/indent-blankline.nvim', diff --git a/autoload/SpaceVim/plugins/history.vim b/autoload/SpaceVim/plugins/history.vim new file mode 100644 index 000000000..672088b14 --- /dev/null +++ b/autoload/SpaceVim/plugins/history.vim @@ -0,0 +1,62 @@ +"============================================================================= +" history.vim --- history manager +" Copyright (c) 2016-2019 Wang Shidong & Contributors +" Author: Wang Shidong < wsdjeg@outlook.com > +" URL: https://spacevim.org +" License: GPLv3 +"============================================================================= + +let s:FILE = SpaceVim#api#import('file') +let s:JSON = SpaceVim#api#import('data#json') +let s:LOG = SpaceVim#logger#derive('history') +let s:history_cache_path = s:FILE.unify_path(g:spacevim_data_dir, ':p') . 'SpaceVim/nvim_history.json' +let s:filepos = {} + +function! SpaceVim#plugins#history#readcache() abort + call s:read_cache() +endfunction +function! SpaceVim#plugins#history#writecache() abort + call s:write_cache() +endfunction + +function! SpaceVim#plugins#history#jumppos() abort + let [l, c] = get(s:filepos, expand('%:p'), [0, 0]) + if l != 0 && c != 0 + call cursor(l, c) + endif +endfunction + +function! SpaceVim#plugins#history#savepos() abort + call s:LOG.debug('save pos for:' . bufname()) + let [_, l, c, _] = getpos('.') + call s:LOG.debug(printf('line %d, col %d', l, c)) + if l != 0 && c != 0 && filereadable(bufname()) + let s:filepos[expand('%:p')] = [l, c] + endif +endfunction + + + +function! s:read_cache() abort + if filereadable(s:history_cache_path) + let his = s:JSON.json_decode(join(readfile(s:history_cache_path, ''), '')) + if type(his) ==# type({}) + call map(deepcopy(his.commands), 'histadd("cmd", v:val)') + let s:filepos = get(his, 'filepos', {}) + endif + endif +endfunction + +function! s:write_cache() abort + let his = { 'commands' : [], 'filepos' : s:filepos} + for i in range(1, 100) + let cmd = histget('cmd', 0 - i) + if empty(cmd) + break + endif + call add(his.commands, histget('cmd', i)) + endfor + call writefile([s:JSON.json_encode(his)], s:history_cache_path) +endfunction + + diff --git a/bundle/vim-startify/autoload/startify.vim b/bundle/vim-startify/autoload/startify.vim index 3b9867a9b..0888ae2d9 100644 --- a/bundle/vim-startify/autoload/startify.vim +++ b/bundle/vim-startify/autoload/startify.vim @@ -7,6 +7,9 @@ if exists('g:autoloaded_startify') || &compatible finish endif + +let s:FILE = SpaceVim#api#import('file') + let g:autoloaded_startify = 1 " Function: #get_lastline {{{1 @@ -113,10 +116,6 @@ function! startify#insane_in_the_membrane(on_vimenter) abort let l:show_session = 1 endif - if empty(v:oldfiles) - call s:warn("Can't read viminfo file. Read :help startify-faq-02") - endif - let b:startify.section_header_lines = [] let lists = s:get_lists() @@ -588,7 +587,7 @@ function! s:filter_oldfiles(path_prefix, path_format, use_env) abort let entries = {} let oldfiles = [] - for fname in v:oldfiles + for fname in neomru#_gather_file_candidates() if counter <= 0 break endif @@ -599,7 +598,7 @@ function! s:filter_oldfiles(path_prefix, path_format, use_env) abort endif try - let absolute_path = fnamemodify(resolve(fname), ":p") + let absolute_path = s:FILE.unify_path(resolve(fname), ":p") catch /E655/ " Too many symbolic links (cycle?) call s:warn('Symlink loop detected! Skipping: '. fname) continue @@ -617,7 +616,7 @@ function! s:filter_oldfiles(path_prefix, path_format, use_env) abort let entry_path = s:transform(absolute_path) endif if empty(entry_path) - let entry_path = fnamemodify(absolute_path, a:path_format) + let entry_path = s:FILE.unify_path(absolute_path, a:path_format) endif let entries[absolute_path] = 1 @@ -649,7 +648,7 @@ function! s:filter_oldfiles_unsafe(path_prefix, path_format, use_env) abort let oldfiles = [] let is_dir = escape(s:sep, '\') . '$' - for fname in v:oldfiles + for fname in neomru#_gather_file_candidates() if counter <= 0 break endif @@ -679,7 +678,7 @@ endfunction " Function: s:show_dir {{{1 function! s:show_dir() abort - return s:display_by_path(getcwd() . s:sep, ':.', 0) + return s:display_by_path(s:FILE.unify_path(getcwd()), ':.', 0) endfunction " Function: s:show_files {{{1 diff --git a/bundle/vim-startify/syntax/startify.vim b/bundle/vim-startify/syntax/startify.vim index aa0e11887..ed329c8f1 100644 --- a/bundle/vim-startify/syntax/startify.vim +++ b/bundle/vim-startify/syntax/startify.vim @@ -8,7 +8,7 @@ if exists("b:current_syntax") finish endif -let s:sep = startify#get_separator() +let s:sep = '/' let s:padding_left = repeat(' ', get(g:, 'startify_padding_left', 3)) syntax sync fromstart diff --git a/docs/bundle-plugins.md b/docs/bundle-plugins.md index 7a352976e..b7fe877a4 100644 --- a/docs/bundle-plugins.md +++ b/docs/bundle-plugins.md @@ -12,6 +12,7 @@ In `bundle/` directory, there are two kinds of plugins: forked plugins without c - [Changed plugin:](#changed-plugin) - [`core` layer](#core-layer) - [`edit` layer](#edit-layer) + - [`ui` layer](#ui-layer) - [No changed plugins](#no-changed-plugins) - [`core` layer](#core-layer-1) - [`lsp` layer](#lsp-layer) @@ -20,7 +21,6 @@ In `bundle/` directory, there are two kinds of plugins: forked plugins without c - [`lang#liquid` layer](#langliquid-layer) - [`lang#go` layer](#langgo-layer) - [`tmux` layer](#tmux-layer) - - [`ui` layer](#ui-layer) - [`incsearch` layer](#incsearch-layer) - [`lang#java` layer](#langjava-layer) - [`lang#plantuml` layer](#langplantuml-layer) @@ -52,6 +52,9 @@ These plugins are changed based on a specific version of origin plugin. - `vim-grammarous`: based on [`rhysd/vim-grammarous@db46357`](https://github.com/rhysd/vim-grammarous/tree/db46357465ce587d5325e816235b5e92415f8c05) +#### `ui` layer + +- `vim-startify`: based on [`mhinz/vim-startify@4e089dffd`](https://github.com/mhinz/vim-startify/tree/4e089dffdad46f3f5593f34362d530e8fe823dcf) ### No changed plugins @@ -106,9 +109,6 @@ These plugins are changed based on a specific version of origin plugin. - [christoomey/vim-tmux-navigator@9ca5bfe5b](https://github.com/christoomey/vim-tmux-navigator/tree/9ca5bfe5bd274051b5dd796cc150348afc993b80) -#### `ui` layer - -- [`mhinz/vim-startify@81e36c35`](https://github.com/mhinz/vim-startify/tree/81e36c352a8deea54df5ec1e2f4348685569bed2) #### `incsearch` layer diff --git a/lua/spacevim/default.lua b/lua/spacevim/default.lua index 43836c9aa..3924ec037 100644 --- a/lua/spacevim/default.lua +++ b/lua/spacevim/default.lua @@ -28,6 +28,8 @@ function M.options() vim.o.showcmd = false + vim.o.shada = '' + vim.o.autoindent = true vim.o.linebreak = true