diff --git a/README.md b/README.md index d878c30..c24daa0 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,12 @@ 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)` | 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 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 +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) - - [ ] make `(Luadev-Run)` 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 18a210f..be93d92 100644 --- a/plugin/luadev.vim +++ b/plugin/luadev.vim @@ -1,23 +1,27 @@ 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(v:true) +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'() " thanks to @xolox on stackoverflow -function! s:get_visual_selection() - let [lnum1, col1] = getpos("'<")[1:2] - let [lnum2, col2] = getpos("'>")[1:2] +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 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 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 + let lines = join(lines, "\n")."\n" + call v:lua.require'luadev'.exec(lines) endfunction function! s:get_current_word()