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 1/5] 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() From 349a599e984468eb51e62f9170430ca4ed131c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20L=C3=B3pez?= Date: Fri, 23 Jul 2021 10:52:12 -0500 Subject: [PATCH 2/5] update the readme to document the new mapping --- README.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d878c30..b1e1dea 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,13 @@ The `:Luadev` command will open an scratch window which will show output from ex Use the folllowing mappings to execute lua code: -Binding | Action -------------------------- | ------ -`(Luadev-RunLine)` | Execute the current line -`(Luadev-Run)` | in visual mode: execute visual selection -`(Luadev-RunWord)` | Eval identifier under cursor, including `table.attr` -`(Luadev-Complete)` | in insert mode: complete (nested) global table fields +Binding | Action +------------------------- | ------ +`(Luadev-RunLine)` | Execute the current line +`(Luadev-Run)` | in visual mode: execute visual selection +`(Luadev-RunOperator)` | in normal mode: execute the motion selection, if linewise the whole lines +`(Luadev-RunWord)` | Eval identifier under cursor, including `table.attr` +`(Luadev-Complete)` | in insert mode: complete (nested) global table fields If the code is a expression, it will be evaluated, and the result shown with `inspect.lua`. Otherwise it will be executed as a block of code. A top-level @@ -26,7 +27,7 @@ Planned features: - [x] autodetect expression vs statements - [x] Fix `inspect.lua` to use `tostring()` on userdata (done on a local copy) - [x] completion of global names and table attributes (WIP: basic implementation done) - - [ ] make `(Luadev-Run)` a proper operator + - [x] make `(Luadev-RunOperator)` a proper operator - [ ] solution for step-wise execution of code with `local` assignments (such as a flag to copy local values to an env) - [x] tracebacks From e47f40c95cf2410db206a3f5f24f6dba64e38832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20L=C3=B3pez?= Date: Fri, 23 Jul 2021 12:15:54 -0500 Subject: [PATCH 3/5] make things less verbose. --- plugin/luadev.vim | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/plugin/luadev.vim b/plugin/luadev.vim index 27db001..4941658 100644 --- a/plugin/luadev.vim +++ b/plugin/luadev.vim @@ -1,42 +1,22 @@ command! Luadev lua require'luadev'.start() noremap (Luadev-RunLine) lua require'luadev'.exec(vim.api.nvim_get_current_line()) -vnoremap (Luadev-Run) :call luadev_run_operator('visual') +vnoremap (Luadev-Run) :call luadev_run_operator(v:true) 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 -" 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 +function! s:luadev_run_operator(is_op) + let [lnum1, col1] = getpos(a:is_op ? "'<" : "'[")[1:2] + let [lnum2, col2] = getpos(a:is_op ? "'>" : "']")[1:2] 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) - if linewise == v:false + if a:is_op == v:true || lnum1 == lnum2 let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)] let lines[0] = lines[0][col1 - 1:] end From e1cbe253e3810a772e48285469c1b3551920bc54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20L=C3=B3pez?= Date: Tue, 27 Jul 2021 12:31:52 -0500 Subject: [PATCH 4/5] Use nnoremap to reuse Luadev-Run --- plugin/luadev.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/luadev.vim b/plugin/luadev.vim index 4941658..ad77223 100644 --- a/plugin/luadev.vim +++ b/plugin/luadev.vim @@ -2,7 +2,7 @@ command! Luadev lua require'luadev'.start() noremap (Luadev-RunLine) lua require'luadev'.exec(vim.api.nvim_get_current_line()) vnoremap (Luadev-Run) :call luadev_run_operator(v:true) -noremap (Luadev-RunOperator) :set opfunc=luadev_run_operatorg@ +nnoremap (Luadev-Run) :set opfunc=luadev_run_operatorg@ noremap (Luadev-RunWord) :call luaeval("require'luadev'.exec(_A)", get_current_word()) inoremap (Luadev-Complete) lua require'luadev.complete'() From 33d054adfcc8c567f03003f2ee85a9aeebd7790a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20L=C3=B3pez?= Date: Tue, 27 Jul 2021 17:24:27 -0500 Subject: [PATCH 5/5] Fix the readme and lua function call. --- README.md | 5 ++--- plugin/luadev.vim | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b1e1dea..c24daa0 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,7 @@ Use the folllowing mappings to execute lua code: Binding | Action ------------------------- | ------ `(Luadev-RunLine)` | Execute the current line -`(Luadev-Run)` | in visual mode: execute visual selection -`(Luadev-RunOperator)` | in normal mode: execute the motion selection, if linewise the whole lines +`(Luadev-Run)` | Operator to execute lua code over a movement or text object. `(Luadev-RunWord)` | Eval identifier under cursor, including `table.attr` `(Luadev-Complete)` | in insert mode: complete (nested) global table fields @@ -27,7 +26,7 @@ Planned features: - [x] autodetect expression vs statements - [x] Fix `inspect.lua` to use `tostring()` on userdata (done on a local copy) - [x] completion of global names and table attributes (WIP: basic implementation done) - - [x] make `(Luadev-RunOperator)` a proper operator + - [x] make `(Luadev-Run)` a proper operator - [ ] solution for step-wise execution of code with `local` assignments (such as a flag to copy local values to an env) - [x] tracebacks diff --git a/plugin/luadev.vim b/plugin/luadev.vim index ad77223..be93d92 100644 --- a/plugin/luadev.vim +++ b/plugin/luadev.vim @@ -21,7 +21,7 @@ function! s:luadev_run_operator(is_op) let lines[0] = lines[0][col1 - 1:] end let lines = join(lines, "\n")."\n" - call luaeval("require'luadev'.exec(_A)", lines) + call v:lua.require'luadev'.exec(lines) endfunction function! s:get_current_word()