From 3e27c752e48c66d0087a1bccf9ca428782f42116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20L=C3=B3pez?= Date: Fri, 23 Jul 2021 10:51:35 -0500 Subject: [PATCH] feat: Add normal mode operator (Luadev-RunOperator) The logic is abstracted into the s:luadev_run_operator function that receives the first parameter "visual" to know it should use the visual range. Otherwise work with motions. In this case motions that expand over one line execute code linewise from the start to the end. This makes motions j,k,%,/,? be more ergonomic I believe. --- plugin/luadev.vim | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/plugin/luadev.vim b/plugin/luadev.vim index 18a210f..27db001 100644 --- a/plugin/luadev.vim +++ b/plugin/luadev.vim @@ -1,23 +1,47 @@ command! Luadev lua require'luadev'.start() noremap (Luadev-RunLine) lua require'luadev'.exec(vim.api.nvim_get_current_line()) -vnoremap (Luadev-Run) :call luaeval("require'luadev'.exec(_A)", get_visual_selection()) +vnoremap (Luadev-Run) :call luadev_run_operator('visual') +noremap (Luadev-RunOperator) :set opfunc=luadev_run_operatorg@ noremap (Luadev-RunWord) :call luaeval("require'luadev'.exec(_A)", get_current_word()) inoremap (Luadev-Complete) lua require'luadev.complete'() + " thanks to @xolox on stackoverflow -function! s:get_visual_selection() - let [lnum1, col1] = getpos("'<")[1:2] - let [lnum2, col2] = getpos("'>")[1:2] +" same function for visual and normal. Except visual passes a parameter. +function! s:luadev_run_operator(type = '') + if a:type == "visual" + let mode = "visual" + else + let mode = "normal" + end + + if mode == 'visual' + let [lnum1, col1] = getpos("'<")[1:2] + let [lnum2, col2] = getpos("'>")[1:2] + elseif mode == 'normal' + let [lnum1, col1] = getpos("'[")[1:2] + let [lnum2, col2] = getpos("']")[1:2] + endif if lnum1 > lnum2 let [lnum1, col1, lnum2, col2] = [lnum2, col2, lnum1, col1] endif + " Normal motions that are more than one line are forced to linewise + if lnum1 != lnum2 && mode =="normal" + let linewise = v:true + else + let linewise = v:false + end + let lines = getline(lnum1, lnum2) - let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)] - let lines[0] = lines[0][col1 - 1:] - return join(lines, "\n")."\n" + if linewise == v:false + let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)] + let lines[0] = lines[0][col1 - 1:] + end + let lines = join(lines, "\n")."\n" + call luaeval("require'luadev'.exec(_A)", lines) endfunction function! s:get_current_word()