~detegr/nvim-bqn

Move all BQN-specific code to ftplugin/bqn.vim v1 APPLIED

Hannu Hartikainen: 2
 Move all BQN-specific code to ftplugin/bqn.vim
 Switch working directory while evaluating BQN code

 3 files changed, 61 insertions(+), 52 deletions(-)
LGTM! Applied.

--
Antti
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~detegr/nvim-bqn/patches/36936/mbox | git am -3
Learn more about email & git

[PATCH 1/2] Move all BQN-specific code to ftplugin/bqn.vim Export this patch

This (slightly) speeds up loading of all files that don't have
filetype=bqn as ftplugin/bqn.vim isn't sourced. On the other hand
plugin/*.vim files are always sourced on startup.

To investigate startup behavior, run with `nvim -Vnvim.log`.
---
 ftplugin/bqn.vim | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
 plugin/bqn.vim   | 52 -----------------------------------------------
 2 files changed, 53 insertions(+), 52 deletions(-)
 delete mode 100644 plugin/bqn.vim

diff --git a/ftplugin/bqn.vim b/ftplugin/bqn.vim
index 1550ec0..8a2d00f 100644
--- a/ftplugin/bqn.vim
+++ b/ftplugin/bqn.vim
@@ -1,3 +1,56 @@
function! BQNEvalTillLine()
    return luaeval(
          \ 'require("bqn").evalBQN(0, _A[1], false)',
          \ [line(".")])
endfunction

function! BQNEvalRange() range
    return luaeval(
          \ 'require("bqn").evalBQN(_A[1] - 1, _A[2], false)',
          \ [a:firstline, a:lastline])
endfunction

function! BQNExplain()
    return luaeval(
          \ 'require("bqn").evalBQN(_A[1] - 1, _A[1], true)',
          \ [line(".")])
endfunction

function! BQNClearAfterLine()
    return luaeval(
          \ 'require("bqn").clearBQN(_A[1] - 1, -1)',
          \ [line(".")])
endfunction

function! BQNClearRange()
    return luaeval(
          \ 'require("bqn").clearBQN(_A[1] - 1, _A[2])',
          \ [a:firstline, a:lastline])
endfunction

hi link bqnoutok Comment
hi link bqnouterr Error

command! BQNEvalTillLine call BQNEvalTillLine()
command! -range BQNEvalRange <line1>,<line2>call BQNEvalRange()
command! BQNEvalFile :lua require("bqn").evalBQN(0, -1, false)

command! BQNExplain call BQNExplain()

command! BQNClearAfterLine call BQNClearAfterLine()
command! -range BQNClearRange <line1>,<line2>call BQNClearRange()
command! BQNClearFile :lua require("bqn").clearBQN(0, -1)

nnoremap <silent> <plug>(bqn_eval_till_line) :BQNEvalTillLine<CR>
xnoremap <silent> <plug>(bqn_eval_range) :BQNEvalRange<CR>
nnoremap <silent> <plug>(bqn_eval_file) :BQNEvalFile<CR>

nnoremap <silent> <plug>(bqn_explain) :BQNExplain<CR>

nnoremap <silent> <plug>(bqn_clear_after_line) :BQNClearAfterLine<CR>
xnoremap <silent> <plug>(bqn_clear_range) :BQNClearRange<CR>
nnoremap <silent> <plug>(bqn_clear_file) :BQNClearFile<CR>

nmap <buffer> <CR> <plug>(bqn_eval_till_line)
xmap <buffer> <CR> <plug>(bqn_eval_range)
nmap <buffer> <leader>bf <plug>(bqn_eval_file)
diff --git a/plugin/bqn.vim b/plugin/bqn.vim
deleted file mode 100644
index 6872065..0000000
--- a/plugin/bqn.vim
@@ -1,52 +0,0 @@
function! BQNEvalTillLine()
    return luaeval(
          \ 'require("bqn").evalBQN(0, _A[1], false)',
          \ [line(".")])
endfunction

function! BQNEvalRange() range
    return luaeval(
          \ 'require("bqn").evalBQN(_A[1] - 1, _A[2], false)',
          \ [a:firstline, a:lastline])
endfunction

function! BQNExplain()
    return luaeval(
          \ 'require("bqn").evalBQN(_A[1] - 1, _A[1], true)',
          \ [line(".")])
endfunction

function! BQNClearAfterLine()
    return luaeval(
          \ 'require("bqn").clearBQN(_A[1] - 1, -1)',
          \ [line(".")])
endfunction

function! BQNClearRange()
    return luaeval(
          \ 'require("bqn").clearBQN(_A[1] - 1, _A[2])',
          \ [a:firstline, a:lastline])
endfunction

hi link bqnoutok Comment
hi link bqnouterr Error

command! BQNEvalTillLine call BQNEvalTillLine()
command! -range BQNEvalRange <line1>,<line2>call BQNEvalRange()
command! BQNEvalFile :lua require("bqn").evalBQN(0, -1, false)

command! BQNExplain call BQNExplain()

command! BQNClearAfterLine call BQNClearAfterLine()
command! -range BQNClearRange <line1>,<line2>call BQNClearRange()
command! BQNClearFile :lua require("bqn").clearBQN(0, -1)

nnoremap <silent> <plug>(bqn_eval_till_line) :BQNEvalTillLine<CR>
xnoremap <silent> <plug>(bqn_eval_range) :BQNEvalRange<CR>
nnoremap <silent> <plug>(bqn_eval_file) :BQNEvalFile<CR>

nnoremap <silent> <plug>(bqn_explain) :BQNExplain<CR>

nnoremap <silent> <plug>(bqn_clear_after_line) :BQNClearAfterLine<CR>
xnoremap <silent> <plug>(bqn_clear_range) :BQNClearRange<CR>
nnoremap <silent> <plug>(bqn_clear_file) :BQNClearFile<CR>
-- 
2.38.1

[PATCH 2/2] Switch working directory while evaluating BQN code Export this patch

This is required if the code has any references to relative paths. E.g.
imports typically require this unless the working directory was already
the correct one.
---
 lua/bqn.lua | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/lua/bqn.lua b/lua/bqn.lua
index 8414acc..372b01a 100644
--- a/lua/bqn.lua
+++ b/lua/bqn.lua
@@ -59,6 +59,12 @@ function evalBQN(from, to, explain)
        bqn = "BQN"
    end

    -- NOTE: BQN imports from relative paths only work by setting the
    -- working directory to that of the script. We will set it back
    -- afterwards.
    local origdir = vim.api.nvim_eval("getcwd()")
    local bufdir = vim.fn.expand("%:p:h")

    -- FIXME: Lua's io.popen() does not support reading stderr, nor combining
    -- stdout and stderr to a single stream. Using 2>&1 to combine the streams
    -- in the meantime.
@@ -69,9 +75,11 @@ function evalBQN(from, to, explain)
        cmd = cmd .. "p \"" .. program .. "\" 2>&1"
    end

    vim.cmd.cd(bufdir)
    local p = assert(io.popen(cmd))
    local output = p:read('*all')
    p:close()
    vim.cmd.cd(origdir)

    local error = nil
    local lines = {}
-- 
2.38.1
LGTM! Applied.

--
Antti