Merge pull request #2 from muniter/run-operator

Add normal mode operator (Luadev-Run)
pull/3/head
Björn Linse 3 years ago committed by GitHub
commit f82a74b153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
------------------------- | ------
`<Plug>(Luadev-RunLine)` | Execute the current line
`<Plug>(Luadev-Run)` | in visual mode: execute visual selection
`<Plug>(Luadev-RunWord)` | Eval identifier under cursor, including `table.attr`
`<Plug>(Luadev-Complete)` | in insert mode: complete (nested) global table fields
Binding | Action
------------------------- | ------
`<Plug>(Luadev-RunLine)` | Execute the current line
`<Plug>(Luadev-Run)` | Operator to execute lua code over a movement or text object.
`<Plug>(Luadev-RunWord)` | Eval identifier under cursor, including `table.attr`
`<Plug>(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 `<Plug>(Luadev-Run)` a proper operator
- [x] make `<Plug>(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

@ -1,23 +1,27 @@
command! Luadev lua require'luadev'.start()
noremap <Plug>(Luadev-RunLine) <Cmd>lua require'luadev'.exec(vim.api.nvim_get_current_line())<cr>
vnoremap <silent> <Plug>(Luadev-Run) :<c-u>call luaeval("require'luadev'.exec(_A)", <SID>get_visual_selection())<cr>
vnoremap <silent> <Plug>(Luadev-Run) :<c-u>call <SID>luadev_run_operator(v:true)<cr>
nnoremap <silent> <Plug>(Luadev-Run) :<c-u>set opfunc=<SID>luadev_run_operator<cr>g@
noremap <silent> <Plug>(Luadev-RunWord) :<c-u>call luaeval("require'luadev'.exec(_A)", <SID>get_current_word())<cr>
inoremap <Plug>(Luadev-Complete) <Cmd>lua require'luadev.complete'()<cr>
" 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()

Loading…
Cancel
Save