mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-03-13 02:05:40 +08:00
feat(guide): rewrite leader guide in lua
This commit is contained in:
parent
100aa31e52
commit
7e81ce1019
@ -6,7 +6,6 @@
|
||||
" License: GPLv3
|
||||
"=============================================================================
|
||||
scriptencoding utf-8
|
||||
|
||||
if exists('s:save_cpo')
|
||||
finish
|
||||
endif
|
||||
@ -14,6 +13,50 @@ endif
|
||||
let s:save_cpo = &cpo
|
||||
set cpo&vim
|
||||
|
||||
if has('nvim-0.9.0')
|
||||
|
||||
function! SpaceVim#mapping#guide#parse_mappings() abort " {{{
|
||||
lua require("spacevim.plugin.guide").parse_mappings()
|
||||
endfunction "}}}
|
||||
function! SpaceVim#mapping#guide#start(vis, dict) abort " {{{
|
||||
lua require("spacevim.plugin.guide").start(
|
||||
\ require("spacevim").eval("a:vis"),
|
||||
\ require("spacevim").eval("a:dict")
|
||||
\ )
|
||||
endfunction "}}}
|
||||
function! SpaceVim#mapping#guide#has_configuration() abort "{{{
|
||||
return luaeval('require("spacevim.plugin.guide").has_configuration()')
|
||||
endfunction "}}}
|
||||
function! SpaceVim#mapping#guide#start_by_prefix(vis, key) abort " {{{
|
||||
lua require("spacevim.plugin.guide").start_by_prefix(
|
||||
\ require("spacevim").eval("a:vis"),
|
||||
\ require("spacevim").eval("a:key")
|
||||
\ )
|
||||
endfunction "}}}
|
||||
function! SpaceVim#mapping#guide#register_displayname(lhs, name) abort
|
||||
lua require("spacevim.plugin.guide").register_displayname(
|
||||
\ require("spacevim").eval("a:lhs"),
|
||||
\ require("spacevim").eval("a:name")
|
||||
\ )
|
||||
endfunction "}}}
|
||||
function! SpaceVim#mapping#guide#populate_dictionary(key, dictname) abort " {{{
|
||||
lua require("spacevim.plugin.guide").populate_dictionary(
|
||||
\ require("spacevim").eval("a:key"),
|
||||
\ require("spacevim").eval("a:dictname")
|
||||
\ )
|
||||
endfunction "}}}
|
||||
function! SpaceVim#mapping#guide#register_prefix_descriptions(key, dictname) abort " {{{
|
||||
lua require("spacevim.plugin.guide").register_prefix_descriptions(
|
||||
\ require("spacevim").eval("a:key"),
|
||||
\ require("spacevim").eval("a:dictname")
|
||||
\ )
|
||||
endfunction "}}}
|
||||
function! SpaceVim#mapping#guide#displayfunc() abort
|
||||
lua require("spacevim.plugin.guide").displayfunc()
|
||||
endfunction
|
||||
else
|
||||
|
||||
|
||||
" Load SpaceVim API
|
||||
let s:CMP = SpaceVim#api#import('vim#compatible')
|
||||
let s:STR = SpaceVim#api#import('data#string')
|
||||
@ -26,6 +69,7 @@ else
|
||||
let s:FLOATING = SpaceVim#api#import('vim#floating')
|
||||
endif
|
||||
let s:SL = SpaceVim#api#import('vim#statusline')
|
||||
let s:LOG =SpaceVim#logger#derive('guide')
|
||||
|
||||
" guide specific var
|
||||
let s:winid = -1
|
||||
@ -34,6 +78,7 @@ let s:prefix_key_inp = []
|
||||
let s:lmap = {}
|
||||
" this should be the history of s:lmap and s:guide_group
|
||||
let s:undo_history = []
|
||||
let s:registered_name = {}
|
||||
|
||||
function! SpaceVim#mapping#guide#has_configuration() abort "{{{
|
||||
return exists('s:desc_lookup')
|
||||
@ -46,11 +91,10 @@ function! SpaceVim#mapping#guide#register_prefix_descriptions(key, dictname) abo
|
||||
endif
|
||||
if strlen(key) == 0
|
||||
let s:desc_lookup['top'] = a:dictname
|
||||
return
|
||||
endif
|
||||
if !has_key(s:desc_lookup, key)
|
||||
elseif !has_key(s:desc_lookup, key)
|
||||
let s:desc_lookup[key] = a:dictname
|
||||
endif
|
||||
call s:LOG.debug('desc_lookup is:' . string(s:desc_lookup))
|
||||
endfunction "}}}
|
||||
function! s:create_cache() abort " {{{
|
||||
let s:desc_lookup = {}
|
||||
@ -58,6 +102,7 @@ function! s:create_cache() abort " {{{
|
||||
endfunction " }}}
|
||||
function! s:create_target_dict(key) abort " {{{
|
||||
if has_key(s:desc_lookup, 'top')
|
||||
" use {expr} to eval viml value
|
||||
let toplevel = deepcopy({s:desc_lookup['top']})
|
||||
let tardict = s:toplevel ? toplevel : get(toplevel, a:key, {})
|
||||
let mapdict = s:cached_dicts[a:key]
|
||||
@ -218,7 +263,7 @@ function! s:escape_mappings(mapping) abort " {{{
|
||||
let rstring = 'call feedkeys("'.rstring.'", "'.a:mapping.feedkeyargs.'")'
|
||||
return rstring
|
||||
endfunction " }}}
|
||||
function! s:string_to_keys(input) abort " {{{
|
||||
function! s:string_to_keys(input) abort
|
||||
" Avoid special case: <>
|
||||
let retlist = []
|
||||
if match(a:input, '<.\+>') != -1
|
||||
@ -238,9 +283,9 @@ function! s:string_to_keys(input) abort " {{{
|
||||
let go = 0
|
||||
elseif a:input[si] ==? '>'
|
||||
let go = 1
|
||||
end
|
||||
endif
|
||||
let si += 1
|
||||
endw
|
||||
endwhile
|
||||
else
|
||||
for it in split(a:input, '\zs')
|
||||
if it ==# ' '
|
||||
@ -251,7 +296,7 @@ function! s:string_to_keys(input) abort " {{{
|
||||
endfor
|
||||
endif
|
||||
return retlist
|
||||
endfunction " }}}
|
||||
endfunction
|
||||
function! s:escape_keys(inp) abort " {{{
|
||||
let ret = substitute(a:inp, '<', '<lt>', '')
|
||||
return substitute(ret, '|', '<Bar>', '')
|
||||
@ -265,6 +310,7 @@ function! s:calc_layout() abort " {{{
|
||||
\ 'strdisplaywidth(repeat(" ", 8 - strlen(v:key)) . "[".v:key."]".'.
|
||||
\ '(type(v:val) == type({}) ? v:val["name"] : v:val[1]))'))
|
||||
let maxlength = max(length) + g:leaderGuide_hspace
|
||||
call s:LOG.debug('maxlength is:' . maxlength)
|
||||
if g:leaderGuide_vertical
|
||||
let ret.n_rows = winheight(0) - 2
|
||||
let ret.n_cols = ret.n_items / ret.n_rows + (ret.n_items != ret.n_rows)
|
||||
@ -276,6 +322,7 @@ function! s:calc_layout() abort " {{{
|
||||
let ret.n_rows = ret.n_items / ret.n_cols + (fmod(ret.n_items,ret.n_cols) > 0 ? 1 : 0)
|
||||
let ret.win_dim = ret.n_rows
|
||||
endif
|
||||
call s:LOG.debug('layout is:' . string(ret))
|
||||
return ret
|
||||
endfunction " }}}
|
||||
|
||||
@ -767,6 +814,7 @@ function! SpaceVim#mapping#guide#start_by_prefix(vis, key) abort " {{{
|
||||
let rundict = s:cached_dicts[a:key]
|
||||
endif
|
||||
let s:lmap = rundict
|
||||
call s:LOG.debug('lmap is:' . string(s:lmap))
|
||||
call s:start_buffer()
|
||||
endfunction " }}}
|
||||
function! SpaceVim#mapping#guide#start(vis, dict) abort " {{{
|
||||
@ -774,21 +822,21 @@ function! SpaceVim#mapping#guide#start(vis, dict) abort " {{{
|
||||
let s:lmap = a:dict
|
||||
call s:start_buffer()
|
||||
endfunction " }}}
|
||||
|
||||
if !exists('g:leaderGuide_displayfunc')
|
||||
function! s:leaderGuide_display() abort
|
||||
function! SpaceVim#mapping#guide#register_displayname(lhs, name) abort
|
||||
call extend(s:registered_name, {a:lhs : a:name})
|
||||
endfunction
|
||||
function! SpaceVim#mapping#guide#displayfunc() abort
|
||||
if has_key(s:registered_name, g:leaderGuide#displayname)
|
||||
return s:registered_name[g:leaderGuide#displayname]
|
||||
endif
|
||||
let g:leaderGuide#displayname = substitute(g:leaderGuide#displayname, '\c<cr>$', '', '')
|
||||
endfunction
|
||||
let g:leaderGuide_displayfunc = [function('s:leaderGuide_display')]
|
||||
endif
|
||||
|
||||
let s:registered_name = {}
|
||||
function! SpaceVim#mapping#guide#register_displayname(lhs, name) abort
|
||||
call extend(s:registered_name, {a:lhs : a:name})
|
||||
endfunction
|
||||
if !exists('g:leaderGuide_displayfunc')
|
||||
let g:leaderGuide_displayfunc = [function('SpaceVim#mapping#guide#displayfunc')]
|
||||
endif
|
||||
|
||||
|
||||
if get(g:, 'mapleader', '\') ==# ' '
|
||||
call SpaceVim#mapping#guide#register_prefix_descriptions(' ',
|
||||
|
2
bundle/neodev.nvim/types/nightly/vim.fn.lua
vendored
2
bundle/neodev.nvim/types/nightly/vim.fn.lua
vendored
@ -5655,7 +5655,7 @@ function vim.fn.matchadd(group, pattern, priority, id, dict) end
|
||||
-- ```vim
|
||||
-- GetGroup()->matchaddpos([23, 11])
|
||||
-- ```
|
||||
--- @param pos number
|
||||
--- @param pos number|table
|
||||
--- @param priority? any
|
||||
--- @param id? any
|
||||
--- @param dict? table<string, any>
|
||||
|
@ -23,6 +23,10 @@ function M.getchar(...)
|
||||
end
|
||||
end
|
||||
|
||||
function M.setbufvar(buf, opts)
|
||||
|
||||
end
|
||||
|
||||
function M.getchar2nr(...)
|
||||
local status, ret = pcall(vim.fn.getchar, ...)
|
||||
if not status then
|
||||
|
@ -8,6 +8,10 @@
|
||||
|
||||
local M = {}
|
||||
|
||||
function M.create_buf(listed, scratch)
|
||||
return vim.api.nvim_create_buf(listed, scratch)
|
||||
end
|
||||
|
||||
function M.set_lines(bufnr, startindex, endindex, replacement)
|
||||
if startindex < 0 then
|
||||
startindex = #vim.buffer(bufnr) + 1 + startindex
|
||||
|
@ -1,11 +1,12 @@
|
||||
--!/usr/bin/lua
|
||||
local M = {}
|
||||
|
||||
local specified_keys = {}
|
||||
|
||||
function M.t(str)
|
||||
if vim.api ~= nil and vim.api.nvim_replace_termcodes ~= nil then
|
||||
-- https://github.com/neovim/neovim/issues/17369
|
||||
local ret = vim.api.nvim_replace_termcodes(str, false, true, true):gsub("\128\254X", "\128")
|
||||
local ret = vim.api.nvim_replace_termcodes(str, false, true, true):gsub('\128\254X', '\128')
|
||||
return ret
|
||||
else
|
||||
-- local ret = vim.fn.execute('echon "\\' .. str .. '"')
|
||||
@ -15,4 +16,33 @@ function M.t(str)
|
||||
end
|
||||
end
|
||||
|
||||
function M.char2name(c)
|
||||
if #c == 1 then
|
||||
return M.nr2name(vim.fn.char2nr(c))
|
||||
end
|
||||
return specified_keys[c] or c
|
||||
end
|
||||
|
||||
function M.nr2name(nr)
|
||||
if type(nr) == 'number' then
|
||||
if nr == 32 then
|
||||
return 'SPC'
|
||||
elseif nr == 4 then
|
||||
return '<C-d>'
|
||||
elseif nr == 3 then
|
||||
return '<C-c>'
|
||||
elseif nr == 9 then
|
||||
return '<Tab>'
|
||||
elseif nr == 92 then
|
||||
return '<Leader>'
|
||||
elseif nr == 27 then
|
||||
return '<Esc>'
|
||||
else
|
||||
return vim.fn.nr2char(nr)
|
||||
end
|
||||
else
|
||||
return specified_keys[nr] or ''
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user