1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-02 22:50:06 +08:00

fix(core): fix major mode cache support

This commit is contained in:
Shidong Wang 2023-04-18 19:40:42 +08:00 committed by GitHub
parent f61fc738f0
commit f97e3e9f94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 30 deletions

View File

@ -16,6 +16,9 @@
" name = 'core#statusline' " name = 'core#statusline'
" enable = false " enable = false
" < " <
" @subsection Layer options
"
" - `major_mode_cache`: Enable/disable major mode cache, enabled by default.
scriptencoding utf-8 scriptencoding utf-8
@ -109,6 +112,10 @@ let s:modes = {
\ }, \ },
\ } \ }
" the major_mode will be cached by default.
let s:major_mode_cache = 1
if SpaceVim#layers#isLoaded('checkers') if SpaceVim#layers#isLoaded('checkers')
call add(s:loaded_modes, 'syntax-checking') call add(s:loaded_modes, 'syntax-checking')
endif endif
@ -733,9 +740,9 @@ endfunction
" \ 'desc' : 'centered-cursor mode', " \ 'desc' : 'centered-cursor mode',
" \ }, " \ },
function! SpaceVim#layers#core#statusline#register_mode(mode) abort function! SpaceVim#layers#core#statusline#register_mode(mode) abort
if has_key(s:modes, a:mode.key) if has_key(s:modes, a:mode.key) && has_key(a:mode, 'func')
let s:modes[a:mode.key]['func'] = a:mode.func let s:modes[a:mode.key]['func'] = a:mode.func
call SpaceVim#logger#info('the func has been added to mode:' . a:mode.key) call SpaceVim#logger#debug('register major mode function:' . a:mode.key)
else else
let s:modes[a:mode.key] = a:mode let s:modes[a:mode.key] = a:mode
endif endif
@ -744,15 +751,17 @@ endfunction
" This func is used to toggle major mode in statusline " This func is used to toggle major mode in statusline
function! SpaceVim#layers#core#statusline#toggle_mode(name) abort function! SpaceVim#layers#core#statusline#toggle_mode(name) abort
call SpaceVim#logger#debug('toggle major mode: ' . a:name)
let mode = get(s:modes, a:name, {}) let mode = get(s:modes, a:name, {})
call SpaceVim#logger#info('try to call func of mode:' . a:name) if empty(mode)
call SpaceVim#logger#debug('can not find major mode: ' . a:name)
return
endif
if has_key(mode, 'func') if has_key(mode, 'func')
let done = call(mode.func, []) let done = call(mode.func, [])
else else
let done = 1 let done = 1
call SpaceVim#logger#info('no func found for mode:' . a:name)
endif endif
if index(s:loaded_modes, a:name) != -1 if index(s:loaded_modes, a:name) != -1
call remove(s:loaded_modes, index(s:loaded_modes, a:name)) call remove(s:loaded_modes, index(s:loaded_modes, a:name))
@ -762,7 +771,9 @@ function! SpaceVim#layers#core#statusline#toggle_mode(name) abort
endif endif
endif endif
let &l:statusline = SpaceVim#layers#core#statusline#get(1) let &l:statusline = SpaceVim#layers#core#statusline#get(1)
call s:update_conf() if s:major_mode_cache
call s:update_conf()
endif
endfunction endfunction
let s:section_old_pos = { let s:section_old_pos = {
@ -797,6 +808,10 @@ function! SpaceVim#layers#core#statusline#rsep() abort
return get(s:separators, g:spacevim_statusline_separator, s:separators['arrow']) return get(s:separators, g:spacevim_statusline_separator, s:separators['arrow'])
endfunction endfunction
function! SpaceVim#layers#core#statusline#set_variable(var) abort
let s:major_mode_cache = get(a:var, 'major_mode_cache', s:major_mode_cache)
endfunction
function! SpaceVim#layers#core#statusline#config() abort function! SpaceVim#layers#core#statusline#config() abort
let [s:lsep , s:rsep] = get(s:separators, g:spacevim_statusline_separator, s:separators['arrow']) let [s:lsep , s:rsep] = get(s:separators, g:spacevim_statusline_separator, s:separators['arrow'])
let [s:ilsep , s:irsep] = get(s:i_separators, g:spacevim_statusline_iseparator, s:i_separators['arrow']) let [s:ilsep , s:irsep] = get(s:i_separators, g:spacevim_statusline_iseparator, s:i_separators['arrow'])
@ -828,11 +843,14 @@ function! SpaceVim#layers#core#statusline#config() abort
\ 'main': 'SpaceVim#layers#core#statusline#ctrlp', \ 'main': 'SpaceVim#layers#core#statusline#ctrlp',
\ 'prog': 'SpaceVim#layers#core#statusline#ctrlp_status', \ 'prog': 'SpaceVim#layers#core#statusline#ctrlp_status',
\ } \ }
if filereadable(expand(g:spacevim_data_dir . 'SpaceVim/major_mode.json')) if filereadable(expand(g:spacevim_data_dir . 'SpaceVim/major_mode.json')) && s:major_mode_cache
" the major mode is cashed in: ~\.cache\SpaceVim\major_mode.json
call SpaceVim#logger#debug('load cache from major_mode.json')
let conf = s:JSON.json_decode(join(readfile(expand(g:spacevim_data_dir . 'SpaceVim/major_mode.json'), ''), '')) let conf = s:JSON.json_decode(join(readfile(expand(g:spacevim_data_dir . 'SpaceVim/major_mode.json'), ''), ''))
for key in keys(conf) for key in keys(conf)
if conf[key] if conf[key]
" this function should be called silent. " this function should be called silent.
call SpaceVim#logger#debug('cached major mode: ' . key)
silent! call SpaceVim#layers#core#statusline#toggle_mode(key) silent! call SpaceVim#layers#core#statusline#toggle_mode(key)
endif endif
endfor endfor
@ -840,11 +858,16 @@ function! SpaceVim#layers#core#statusline#config() abort
endfunction endfunction
function! s:update_conf() abort function! s:update_conf() abort
call SpaceVim#logger#debug('write major mode to major_mode.json')
let conf = {} let conf = {}
for key in keys(s:modes) for key in keys(s:modes)
call extend(conf, {key : (index(s:loaded_modes, key) > -1 ? 1 : 0)}) call extend(conf, {key : (index(s:loaded_modes, key) > -1 ? 1 : 0)})
endfor endfor
call writefile([s:JSON.json_encode(conf)], expand(g:spacevim_data_dir . 'SpaceVim/major_mode.json')) if writefile([s:JSON.json_encode(conf)], expand(g:spacevim_data_dir . 'SpaceVim/major_mode.json')) == 0
call SpaceVim#logger#debug('update major_mode.json done')
else
call SpaceVim#logger#debug('failed to update major_mode.json')
endif
endfunction endfunction
" Arguments: " Arguments:

View File

@ -117,6 +117,8 @@ function! SpaceVim#layers#ui#plugins() abort
endfunction endfunction
let s:file = expand('<sfile>:~')
let s:funcbeginline = expand('<slnum>') + 1
function! SpaceVim#layers#ui#config() abort function! SpaceVim#layers#ui#config() abort
if g:spacevim_colorscheme_bg ==# 'dark' if g:spacevim_colorscheme_bg ==# 'dark'
let g:indentLine_color_term = get(g:, 'indentLine_color_term', 239) let g:indentLine_color_term = get(g:, 'indentLine_color_term', 239)
@ -195,8 +197,8 @@ function! SpaceVim#layers#ui#config() abort
\ 'call SpaceVim#mapping#SmartClose()') \ 'call SpaceVim#mapping#SmartClose()')
endif endif
" Ui toggles " Ui toggles
call SpaceVim#mapping#space#def('nnoremap', ['t', '8'], 'call call(' call SpaceVim#mapping#space#def('nnoremap', ['t', '8'],
\ . string(s:_function('s:toggle_fill_column')) . ', [])', \ 'call SpaceVim#layers#core#statusline#toggle_mode("hi-characters-for-long-lines")',
\ 'highlight-long-lines', 1) \ 'highlight-long-lines', 1)
if g:spacevim_autocomplete_method ==# 'deoplete' if g:spacevim_autocomplete_method ==# 'deoplete'
call SpaceVim#mapping#space#def('nnoremap', ['t', 'a'], 'call SpaceVim#layers#autocomplete#toggle_deoplete()', call SpaceVim#mapping#space#def('nnoremap', ['t', 'a'], 'call SpaceVim#layers#autocomplete#toggle_deoplete()',
@ -213,8 +215,7 @@ function! SpaceVim#layers#ui#config() abort
\ 'toggle conceallevel', 1) \ 'toggle conceallevel', 1)
call SpaceVim#mapping#space#def('nnoremap', ['t', 't'], 'call SpaceVim#plugins#tabmanager#open()', call SpaceVim#mapping#space#def('nnoremap', ['t', 't'], 'call SpaceVim#plugins#tabmanager#open()',
\ 'open-tabs-manager', 1) \ 'open-tabs-manager', 1)
call SpaceVim#mapping#space#def('nnoremap', ['t', 'f'], 'call call(' call SpaceVim#mapping#space#def('nnoremap', ['t', 'f'], 'call SpaceVim#layers#core#statusline#toggle_mode("fill-column-indicator")',
\ . string(s:_function('s:toggle_colorcolumn')) . ', [])',
\ 'fill-column-indicator', 1) \ 'fill-column-indicator', 1)
call SpaceVim#mapping#space#def('nnoremap', ['t', 'h', 'h'], 'call call(' call SpaceVim#mapping#space#def('nnoremap', ['t', 'h', 'h'], 'call call('
\ . string(s:_function('s:toggle_cursorline')) . ', [])', \ . string(s:_function('s:toggle_cursorline')) . ', [])',
@ -257,6 +258,35 @@ function! SpaceVim#layers#ui#config() abort
\ 'func' : s:_function('s:toggle_spell_check'), \ 'func' : s:_function('s:toggle_spell_check'),
\ } \ }
\ ) \ )
call SpaceVim#layers#core#statusline#register_mode(
\ {
\ 'key' : 'hi-characters-for-long-lines',
\ 'func' : s:_function('s:toggle_fill_column'),
\ }
\ )
call SpaceVim#layers#core#statusline#register_mode(
\ {
\ 'key' : 'fill-column-indicator',
\ 'func' : s:_function('s:toggle_colorcolumn'),
\ }
\ )
call SpaceVim#layers#core#statusline#register_mode(
\ {
\ 'key' : 'whitespace',
\ 'func' : s:_function('s:toggle_whitespace'),
\ }
\ )
let s:lnum = expand('<slnum>') + s:funcbeginline
call SpaceVim#mapping#space#def('nnoremap', ['t', 'w'],
\ 'call SpaceVim#layers#core#statusline#toggle_mode("whitespace")',
\ ['toggle-highlight-tail-spaces',
\ [
\ '[SPC t w] will toggle white space highlighting',
\ '',
\ 'Definition: ' . s:file . ':' . s:lnum,
\ ]
\ ]
\ , 1)
call SpaceVim#mapping#space#def('nnoremap', ['t', 'S'], call SpaceVim#mapping#space#def('nnoremap', ['t', 'S'],
\ 'call SpaceVim#layers#core#statusline#toggle_mode("spell-checking")', \ 'call SpaceVim#layers#core#statusline#toggle_mode("spell-checking")',
\ 'toggle-spell-checker', 1) \ 'toggle-spell-checker', 1)
@ -275,12 +305,15 @@ function! SpaceVim#layers#ui#config() abort
call SpaceVim#mapping#space#def('nnoremap', ['t', 'l'], 'setlocal list!', call SpaceVim#mapping#space#def('nnoremap', ['t', 'l'], 'setlocal list!',
\ 'toggle-hidden-listchars', 1) \ 'toggle-hidden-listchars', 1)
call SpaceVim#mapping#space#def('nnoremap', ['t', 'W'], 'call call(' call SpaceVim#layers#core#statusline#register_mode(
\ . string(s:_function('s:toggle_wrap_line')) . ', [])', \ {
\ 'key' : 'wrapline',
\ 'func' : s:_function('s:toggle_wrap_line'),
\ }
\ )
call SpaceVim#mapping#space#def('nnoremap', ['t', 'W'],
\ 'call SpaceVim#layers#core#statusline#toggle_mode("wrapline")',
\ 'toggle-wrap-line', 1) \ 'toggle-wrap-line', 1)
call SpaceVim#mapping#space#def('nnoremap', ['t', 'w'], 'call call('
\ . string(s:_function('s:toggle_whitespace')) . ', [])',
\ 'toggle-highlight-tail-spaces', 1)
nnoremap <silent> <F11> :call <SID>toggle_full_screen()<Cr> nnoremap <silent> <F11> :call <SID>toggle_full_screen()<Cr>
let g:_spacevim_mappings_space.z = get(g:_spacevim_mappings_space, 'z', {'name' : '+Fonts'}) let g:_spacevim_mappings_space.z = get(g:_spacevim_mappings_space, 'z', {'name' : '+Fonts'})
@ -355,7 +388,7 @@ function! s:toggle_colorcolumn() abort
set cc= set cc=
let s:ccflag = 0 let s:ccflag = 0
endif endif
call SpaceVim#layers#core#statusline#toggle_mode('fill-column-indicator') return 1
endfunction endfunction
let s:fcflag = 0 let s:fcflag = 0
@ -372,7 +405,7 @@ function! s:toggle_fill_column() abort
set cc= set cc=
let s:fcflag = 0 let s:fcflag = 0
endif endif
call SpaceVim#layers#core#statusline#toggle_mode('hi-characters-for-long-lines') return 1
endfunction endfunction
function! s:toggle_indentline() abort function! s:toggle_indentline() abort
@ -478,12 +511,12 @@ function! s:toggle_whitespace() abort
let s:whitespace_enable = 1 let s:whitespace_enable = 1
endif endif
call SpaceVim#layers#core#statusline#toggle_section('whitespace') call SpaceVim#layers#core#statusline#toggle_section('whitespace')
call SpaceVim#layers#core#statusline#toggle_mode('whitespace') return 1
endfunction endfunction
function! s:toggle_wrap_line() abort function! s:toggle_wrap_line() abort
setlocal wrap! set wrap!
call SpaceVim#layers#core#statusline#toggle_mode('wrapline') return 1
endfunction endfunction
function! s:toggle_conceallevel() abort function! s:toggle_conceallevel() abort

View File

@ -1918,6 +1918,9 @@ airline's statusline, just disable this layer
name = 'core#statusline' name = 'core#statusline'
enable = false enable = false
< <
LAYER OPTIONS
`major_mode_cache`: Enable/disable major mode cache, enabled by default.
============================================================================== ==============================================================================
CORE#TABLINE *SpaceVim-layers-core-tabline* CORE#TABLINE *SpaceVim-layers-core-tabline*

View File

@ -639,7 +639,7 @@ The `core#statusline` layer provides a heavily customized powerline with the fol
- show the index of search results - show the index of search results
- toggle syntax checking info - toggle syntax checking info
- toggle battery info - toggle battery info
- toggle minor mode lighters - toggle major mode lighters
- show VCS information (branch, hunk summary) (requires `git` and `VersionControl` layers) - show VCS information (branch, hunk summary) (requires `git` and `VersionControl` layers)
| Key Bindings | Descriptions | | Key Bindings | Descriptions |
@ -664,8 +664,8 @@ Some elements can be dynamically toggled:
| `SPC t m b` | toggle the battery status (need to install acpi) | | `SPC t m b` | toggle the battery status (need to install acpi) |
| `SPC t m c` | toggle the org task clock (available in org layer)(TODO) | | `SPC t m c` | toggle the org task clock (available in org layer)(TODO) |
| `SPC t m i` | toggle the input method | | `SPC t m i` | toggle the input method |
| `SPC t m m` | toggle the minor mode lighters | | `SPC t m m` | toggle the major mode lighters |
| `SPC t m M` | toggle the major mode | | `SPC t m M` | toggle the filetype section |
| `SPC t m n` | toggle the cat! (If colors layer is declared in your dotfile)(TODO) | | `SPC t m n` | toggle the cat! (If colors layer is declared in your dotfile)(TODO) |
| `SPC t m p` | toggle the cursor position | | `SPC t m p` | toggle the cursor position |
| `SPC t m t` | toggle the time | | `SPC t m t` | toggle the time |
@ -679,7 +679,7 @@ By default SpaceVim uses nerd-fonts, which can be downloaded from their [website
**syntax checking integration:** **syntax checking integration:**
When syntax checking minor mode is enabled, a new element appears showing the number of errors and warnings. When syntax checking major mode is enabled, a new element appears showing the number of errors and warnings.
**Search index integration:** **Search index integration:**
@ -729,9 +729,9 @@ Here is an exhaustive set of screenshots for all the available separators:
| `nil` | ![separator-nil](https://cloud.githubusercontent.com/assets/13142418/26249776/645a5a96-3cda-11e7-9655-0aa1f76714f4.png) | | `nil` | ![separator-nil](https://cloud.githubusercontent.com/assets/13142418/26249776/645a5a96-3cda-11e7-9655-0aa1f76714f4.png) |
| `fire` | ![separator-fire](https://cloud.githubusercontent.com/assets/13142418/26274142/434cdd10-3d75-11e7-811b-e44cebfdca58.png) | | `fire` | ![separator-fire](https://cloud.githubusercontent.com/assets/13142418/26274142/434cdd10-3d75-11e7-811b-e44cebfdca58.png) |
**Minor Modes:** **major modes:**
The minor mode area can be toggled on and off with `SPC t m m`. The major mode area can be toggled on and off with `SPC t m m`.
Unicode symbols are displayed by default. Add `statusline_unicode = false` to your custom configuration file to use ASCII characters instead (may be useful in the terminal if you cannot set an appropriate font). Unicode symbols are displayed by default. Add `statusline_unicode = false` to your custom configuration file to use ASCII characters instead (may be useful in the terminal if you cannot set an appropriate font).
@ -746,6 +746,15 @@ The letters displayed in the statusline correspond to the key bindings used to t
| `SPC t w` | ⓦ | w | whitespace mode (highlight trailing whitespace) | | `SPC t w` | ⓦ | w | whitespace mode (highlight trailing whitespace) |
| `SPC t W` | Ⓦ | W | wrap line mode | | `SPC t W` | Ⓦ | W | wrap line mode |
The status of major mode will be cached, the cache will be loaded when spacevim startup.
If you want to disable major mode cache, you need to charge the layer option of `core#statusline` layer.
```toml
[[layers]]
name = 'core#statusline'
major_mode_cache = false
```
**colorscheme of statusline:** **colorscheme of statusline:**
By default SpaceVim only supports colorschemes included in [colorscheme layer](../layers/colorscheme/). By default SpaceVim only supports colorschemes included in [colorscheme layer](../layers/colorscheme/).
@ -2552,5 +2561,3 @@ export PATH=$PATH:$HOME/.SpaceVim/bin
Use `svc` to open a file in the existing Vim server, or use `nsvc` to open a file in the existing Neovim server. Use `svc` to open a file in the existing Vim server, or use `nsvc` to open a file in the existing Neovim server.
![server-and-client](https://user-images.githubusercontent.com/13142418/32554968-7164fe9c-c4d6-11e7-95f7-f6a6ea75e05b.gif) ![server-and-client](https://user-images.githubusercontent.com/13142418/32554968-7164fe9c-c4d6-11e7-95f7-f6a6ea75e05b.gif)
<!-- vim:set nowrap: -->