From 32b8309ce5ac0390c7b21c43c80c979465c30068 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Sat, 24 Jun 2023 15:08:18 +0800 Subject: [PATCH] perf(iedit): use notify api --- lua/spacevim/plugin/iedit.lua | 127 +++++++++++----------------------- 1 file changed, 41 insertions(+), 86 deletions(-) diff --git a/lua/spacevim/plugin/iedit.lua b/lua/spacevim/plugin/iedit.lua index cd6b44bfd..c209fc3bb 100644 --- a/lua/spacevim/plugin/iedit.lua +++ b/lua/spacevim/plugin/iedit.lua @@ -74,7 +74,8 @@ local getline = vim.fn.getline local timer_start = vim.fn.timer_start local function echo(msg) -- {{{ - vim.api.nvim_echo({ { msg } }, false, {}) + vim.g._spacevim_temp_msg = msg + vim.api.nvim_eval('SpaceVim#api#notify#get().notify(g:_spacevim_temp_msg, "None")') end -- }}} @@ -99,11 +100,8 @@ local function replace_symbol() -- {{{ for i = 1, #cursor_stack, 1 do if cursor_stack[i].lnum ~= line then if not empty(idxs) then - _end = string.sub( - vim.fn.getline(line), - cursor_stack[i - 1].col + cursor_stack[i - 1].len, - -1 - ) + _end = + string.sub(vim.fn.getline(line), cursor_stack[i - 1].col + cursor_stack[i - 1].len, -1) pre = pre .. _end end fixstack(idxs) @@ -149,9 +147,7 @@ local function replace_symbol() -- {{{ table.insert(idxs, { i, vim.fn.len( - cursor_stack[i].cursor_begin - .. cursor_stack[i].cursor_char - .. cursor_stack[i].cursor_end + cursor_stack[i].cursor_begin .. cursor_stack[i].cursor_char .. cursor_stack[i].cursor_end ), }) end @@ -242,15 +238,11 @@ local function handle_normal(char) -- handle normal key bindings {{{ if cursor_stack[i].active then local old_cursor_char = cursor_stack[i].cursor_char cursor_stack[i].cursor_char = vim.fn.matchstr( - cursor_stack[i].cursor_begin - .. cursor_stack[i].cursor_char - .. cursor_stack[i].cursor_end, + cursor_stack[i].cursor_begin .. cursor_stack[i].cursor_char .. cursor_stack[i].cursor_end, '^.' ) cursor_stack[i].cursor_end = vim.fn.substitute( - cursor_stack[i].cursor_begin - .. old_cursor_char - .. cursor_stack[i].cursor_end, + cursor_stack[i].cursor_begin .. old_cursor_char .. cursor_stack[i].cursor_end, '^.', '', 'g' @@ -269,12 +261,9 @@ local function handle_normal(char) -- handle normal key bindings {{{ vim.w.spacevim_statusline_mode = 'ii' for _, i in ipairs(vim.fn.range(1, #cursor_stack)) do if cursor_stack[i].active then - cursor_stack[i].cursor_begin = cursor_stack[i].cursor_begin - .. cursor_stack[i].cursor_char - cursor_stack[i].cursor_char = - vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') - cursor_stack[i].cursor_end = - vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') + cursor_stack[i].cursor_begin = cursor_stack[i].cursor_begin .. cursor_stack[i].cursor_char + cursor_stack[i].cursor_char = vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') + cursor_stack[i].cursor_end = vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') end end vim.cmd('redrawstatus!') @@ -309,8 +298,7 @@ local function handle_normal(char) -- handle normal key bindings {{{ elseif char == '~' then -- toggle the case of cursor char {{{ for _, i in ipairs(vim.fn.range(1, #cursor_stack)) do if cursor_stack[i].active then - cursor_stack[i].cursor_char = - str.toggle_case(cursor_stack[i].cursor_char) + cursor_stack[i].cursor_char = str.toggle_case(cursor_stack[i].cursor_char) end end replace_symbol() @@ -325,10 +313,8 @@ local function handle_normal(char) -- handle normal key bindings {{{ vim.w.spacevim_statusline_mode = 'ii' for _, i in ipairs(vim.fn.range(1, #cursor_stack)) do if cursor_stack[i].active then - cursor_stack[i].cursor_char = - vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') - cursor_stack[i].cursor_end = - vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') + cursor_stack[i].cursor_char = vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') + cursor_stack[i].cursor_end = vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') end end replace_symbol() @@ -336,10 +322,8 @@ local function handle_normal(char) -- handle normal key bindings {{{ elseif char == 'x' then -- {{{ for _, i in ipairs(vim.fn.range(1, #cursor_stack)) do if cursor_stack[i].active then - cursor_stack[i].cursor_char = - vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') - cursor_stack[i].cursor_end = - vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') + cursor_stack[i].cursor_char = vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') + cursor_stack[i].cursor_end = vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') end end replace_symbol() @@ -356,10 +340,8 @@ local function handle_normal(char) -- handle normal key bindings {{{ elseif char == k.t('') or char == 'h' then -- {{{ for _, i in ipairs(vim.fn.range(1, #cursor_stack)) do if cursor_stack[i].active then - cursor_stack[i].cursor_end = cursor_stack[i].cursor_char - .. cursor_stack[i].cursor_end - cursor_stack[i].cursor_char = - vim.fn.matchstr(cursor_stack[i].cursor_begin, '.$') + cursor_stack[i].cursor_end = cursor_stack[i].cursor_char .. cursor_stack[i].cursor_end + cursor_stack[i].cursor_char = vim.fn.matchstr(cursor_stack[i].cursor_begin, '.$') cursor_stack[i].cursor_begin = vim.fn.substitute(cursor_stack[i].cursor_begin, '.$', '', 'g') end @@ -368,12 +350,9 @@ local function handle_normal(char) -- handle normal key bindings {{{ elseif char == k.t('') or char == 'l' then for _, i in ipairs(vim.fn.range(1, #cursor_stack)) do if cursor_stack[i].active then - cursor_stack[i].cursor_begin = cursor_stack[i].cursor_begin - .. cursor_stack[i].cursor_char - cursor_stack[i].cursor_char = - vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') - cursor_stack[i].cursor_end = - vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') + cursor_stack[i].cursor_begin = cursor_stack[i].cursor_begin .. cursor_stack[i].cursor_char + cursor_stack[i].cursor_char = vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') + cursor_stack[i].cursor_end = vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') end end elseif char == 'e' then @@ -383,8 +362,7 @@ local function handle_normal(char) -- handle normal key bindings {{{ cursor_stack[i].cursor_begin = cursor_stack[i].cursor_begin .. cursor_stack[i].cursor_char .. word - cursor_stack[i].cursor_char = - vim.fn.matchstr(cursor_stack[i].cursor_begin, '.$') + cursor_stack[i].cursor_char = vim.fn.matchstr(cursor_stack[i].cursor_begin, '.$') cursor_stack[i].cursor_end = vim.fn.substitute(cursor_stack[i].cursor_end, [[^\s*\S*]], '', 'g') end @@ -398,10 +376,8 @@ local function handle_normal(char) -- handle normal key bindings {{{ .. cursor_stack[i].cursor_end cursor_stack[i].cursor_begin = vim.fn.substitute(cursor_stack[i].cursor_begin, [[\S*\s*$]], '', 'g') - cursor_stack[i].cursor_char = - vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') - cursor_stack[i].cursor_end = - vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') + cursor_stack[i].cursor_char = vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') + cursor_stack[i].cursor_end = vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') end end elseif char == 'w' then @@ -413,10 +389,8 @@ local function handle_normal(char) -- handle normal key bindings {{{ .. word cursor_stack[i].cursor_end = vim.fn.substitute(cursor_stack[i].cursor_end, [[^\S*\s*]], '', 'g') - cursor_stack[i].cursor_char = - vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') - cursor_stack[i].cursor_end = - vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') + cursor_stack[i].cursor_char = vim.fn.matchstr(cursor_stack[i].cursor_end, '^.') + cursor_stack[i].cursor_end = vim.fn.substitute(cursor_stack[i].cursor_end, '^.', '', 'g') end end elseif char == '0' or char == '' then @@ -490,9 +464,7 @@ local function handle_normal(char) -- handle normal key bindings {{{ elseif char == 'N' then end highlight_cursor() - return cursor_stack[1].cursor_begin - .. cursor_stack[1].cursor_char - .. cursor_stack[1].cursor_end + return cursor_stack[1].cursor_begin .. cursor_stack[1].cursor_char .. cursor_stack[1].cursor_end end -- }}} @@ -506,9 +478,7 @@ local function handle_insert(char) -- {{{ highlight_cursor() vim.cmd('redraw!') vim.cmd('redrawstatus!') - return cursor_stack[1].cursor_begin - .. cursor_stack[1].cursor_char - .. cursor_stack[1].cursor_end + return cursor_stack[1].cursor_begin .. cursor_stack[1].cursor_char .. cursor_stack[1].cursor_end elseif char == k.t('') then for i = 1, #cursor_stack, 1 do if cursor_stack[i].active then @@ -532,17 +502,14 @@ local function handle_insert(char) -- {{{ elseif char == k.t('') or char == k.t('') then for i = 1, #cursor_stack, 1 do if cursor_stack[i].active then - cursor_stack[i].cursor_begin = - substitute(cursor_stack[i].cursor_begin, '.$', '', 'g') + cursor_stack[i].cursor_begin = substitute(cursor_stack[i].cursor_begin, '.$', '', 'g') end end elseif char == k.t('') or char == k.t('') then for i = 1, #cursor_stack, 1 do if cursor_stack[i].active then - cursor_stack[i].cursor_begin = - matchstr(cursor_stack[i].cursor_end, '^.') - cursor_stack[i].cursor_end = - substitute(cursor_stack[i].cursor_end, '^.', '', 'g') + cursor_stack[i].cursor_begin = matchstr(cursor_stack[i].cursor_end, '^.') + cursor_stack[i].cursor_end = substitute(cursor_stack[i].cursor_end, '^.', '', 'g') end end elseif char == k.t('') or char == k.t('') then @@ -550,12 +517,9 @@ local function handle_insert(char) -- {{{ for i = 1, #cursor_stack, 1 do if cursor_stack[i].active then if not empty(cursor_stack[i].cursor_begin) then - cursor_stack[i].cursor_end = cursor_stack[i].cursor_char - .. cursor_stack[i].cursor_end - cursor_stack[i].cursor_char = - matchstr(cursor_stack[i].cursor_begin, '.$') - cursor_stack[i].cursor_begin = - substitute(cursor_stack[i].cursor_begin, '.$', '', 'g') + cursor_stack[i].cursor_end = cursor_stack[i].cursor_char .. cursor_stack[i].cursor_end + cursor_stack[i].cursor_char = matchstr(cursor_stack[i].cursor_begin, '.$') + cursor_stack[i].cursor_begin = substitute(cursor_stack[i].cursor_begin, '.$', '', 'g') end end end @@ -563,11 +527,9 @@ local function handle_insert(char) -- {{{ is_movement = true for i = 1, #cursor_stack, 1 do if cursor_stack[i].active then - cursor_stack[i].cursor_begin = cursor_stack[i].cursor_begin - .. cursor_stack[i].cursor_char + cursor_stack[i].cursor_begin = cursor_stack[i].cursor_begin .. cursor_stack[i].cursor_char cursor_stack[i].cursor_char = matchstr(cursor_stack[i].cursor_end, '^.') - cursor_stack[i].cursor_end = - substitute(cursor_stack[i].cursor_end, '^.', '', 'g') + cursor_stack[i].cursor_end = substitute(cursor_stack[i].cursor_end, '^.', '', 'g') end end elseif char == k.t('') then @@ -584,9 +546,7 @@ local function handle_insert(char) -- {{{ replace_symbol() end highlight_cursor() - return cursor_stack[1].cursor_begin - .. cursor_stack[1].cursor_char - .. cursor_stack[1].cursor_end + return cursor_stack[1].cursor_begin .. cursor_stack[1].cursor_char .. cursor_stack[1].cursor_end end --- }}} @@ -628,10 +588,8 @@ local function handle_f_char(char) -- {{{ if char >= 32 and char <= 126 then Operator = '' for _, i in ipairs(vim.fn.range(1, #cursor_stack)) do - local matchedstr = vim.fn.matchstr( - cursor_stack[i].cursor_end, - vim.fn.printf('[^%s]', vim.fn.nr2char(char)) - ) + local matchedstr = + vim.fn.matchstr(cursor_stack[i].cursor_end, vim.fn.printf('[^%s]', vim.fn.nr2char(char))) cursor_stack[i].cursor_begin = cursor_stack[i].cursor_begin .. cursor_stack[i].cursor_char .. matchedstr @@ -643,9 +601,7 @@ local function handle_f_char(char) -- {{{ end end highlight_cursor() - return cursor_stack[1].cursor_begin - .. cursor_stack[1].cursor_char - .. cursor_stack[1].cursor_end + return cursor_stack[1].cursor_begin .. cursor_stack[1].cursor_char .. cursor_stack[1].cursor_end end -- }}} @@ -662,9 +618,7 @@ local function handle_register(char) -- {{{ replace_symbol() highlight_cursor() end - return cursor_stack[1].cursor_begin - .. cursor_stack[1].cursor_char - .. cursor_stack[1].cursor_end + return cursor_stack[1].cursor_begin .. cursor_stack[1].cursor_char .. cursor_stack[1].cursor_end end -- }}} @@ -694,6 +648,7 @@ function M.start(...) -- {{{ ) then echo('no pattern found under cursor') + return end local save_tve = vim.o.t_ve local save_cl = vim.wo.cursorline