~detegr/nvim-bqn

Change command names and mappings v1 APPLIED

Andrey Popp: 2
 Change command names and mappings
 Set diagnostics for errors reporyed by CBQN

 3 files changed, 56 insertions(+), 20 deletions(-)
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/27166/mbox | git am -3
Learn more about email & git

[PATCH 1/2] Change command names and mappings Export this patch

This changes command names to be prefixed with `:BQN` for better
discovery (completion in command mode).

Then this introduces plug mappings and automatically maps them to

- `<CR>` evals, till the line (or range), as previously
- `<leader>bf` evals the whole file
- `<leader>bc` clears output after the line (or range)
---

The changes as we've discussed before.

I didn't map `<plug>(bqn_clear_file)` here as I'm not sure what would be the
best key for that, was thinking of `<leader>bcf` but then it introduces a delay
when you press `<leader>bc` as neovim waits for the possible continuation.
 ftplugin/bqn.vim |  8 +++++---
 plugin/bqn.vim   | 28 ++++++++++++++++++----------
 2 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/ftplugin/bqn.vim b/ftplugin/bqn.vim
index 069cb05..5b59c4c 100644
--- a/ftplugin/bqn.vim
+++ b/ftplugin/bqn.vim
@@ -1,3 +1,5 @@
nnoremap <buffer> <CR> :EvalBQNTillLine<CR>
nnoremap <buffer> <C-Space> :EvalBQNFile<CR>
xnoremap <buffer> <CR> :EvalBQNRange<CR>
nmap <buffer> <CR> <plug>(bqn_eval_till_line)
xmap <buffer> <CR> <plug>(bqn_eval_range)
nmap <buffer> <leader>bf <plug>(bqn_eval_file)
nmap <buffer> <leader>bc <plug>(bqn_clear_after_line)
xmap <buffer> <leader>bc <plug>(bqn_clear_range)
diff --git a/plugin/bqn.vim b/plugin/bqn.vim
index 9c84e58..de9e1f6 100644
--- a/plugin/bqn.vim
+++ b/plugin/bqn.vim
@@ -1,22 +1,22 @@
function! EvalBQNTillLine()
function! BQNEvalTillLine()
    return luaeval(
          \ 'require("bqn").evalBQN(0, _A[1], true)',
          \ [line(".")])
endfunction

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

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

function! ClearBQNRange()
function! BQNClearRange()
    return luaeval(
          \ 'require("bqn").clearBQN(_A[1] - 1, _A[2])',
          \ [a:firstline, a:lastline])
@@ -25,10 +25,18 @@ endfunction
hi link bqnoutok Comment
hi link bqnouterr Error

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

command! ClearBQNAfterLine call ClearBQNAfterLine()
command! -range ClearBQNRange <line1>,<line2>call ClearBQNRange()
command! ClearBQNFile :lua require("bqn").clearBQN(0, -1)
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>
nnoremap <silent> <plug>(bqn_eval_range) :BQNEvalRange<CR>
nnoremap <silent> <plug>(bqn_eval_file) :BQNEvalFile<CR>

nnoremap <silent> <plug>(bqn_clear_after_line) :BQNClearAfterLine<CR>
nnoremap <silent> <plug>(bqn_clear_range) :BQNClearRange<CR>
nnoremap <silent> <plug>(bqn_clear_file) :BQNClearFile<CR>
-- 
2.30.2
Hi.

[PATCH 2/2] Set diagnostics for errors reporyed by CBQN Export this patch

Once we see an error returned from CBQN we set diagnostics via
`vim.diagnostic` API. This helps to see which line contains the error.

The diagnostic set is "ephemeral" as it will be cleared (or replaced
with another one) once we eval another piece of code.
---

Let me know what you think! I've found this useful to quickly find an offending
line. Would be cool if CBQN reported a column as well...
 lua/bqn.lua | 40 +++++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/lua/bqn.lua b/lua/bqn.lua
index 33b95c8..fcd3ee8 100644
--- a/lua/bqn.lua
+++ b/lua/bqn.lua
@@ -1,6 +1,15 @@
local ns = vim.api.nvim_create_namespace('bqnout')

local function enumerate(it)
  local idx, v = 0, nil
  return function()
    v, idx = it(), idx + 1
    return v, idx
  end
end

function clearBQN(from, to)
  vim.diagnostic.reset(ns, 0)
  vim.api.nvim_buf_clear_namespace(0, ns, from, to)
end

@@ -45,20 +54,37 @@ function evalBQN(from, to, pretty)
    local executable = assert(io.popen(cmd))
    local output = executable:read('*all')

    local error = nil
    local lines = {}
    local line_count = 0
    local is_error = nil
    for line in output:gmatch("[^\n]+") do
        if is_error == nil then
          is_error = line:find("^Error:") ~= nil
    for line, lnum in enumerate(output:gmatch("[^\n]+")) do
        if lnum == 1 then
          local message = line:gsub("^Error: (.*)", "%1")
          if message ~= line then
            error = {message=message}
          end
        end
        if error ~= nil and lnum == 2 then
          local lnum = line:gsub("^%(%-p%):(%d+):", "%1")
          error.lnum = tonumber(lnum) + from - 1
        end
        local hl = 'bqnoutok'
        if is_error then hl = 'bqnouterr' end
        if error ~= nil then hl = 'bqnouterr' end
        table.insert(lines, {{' ' .. line, hl}})
        line_count = line_count + 1
    end
    table.insert(lines, {{' ', 'bqnoutok'}})

    -- Reset and show diagnostics
    vim.diagnostic.reset(ns, 0)
    if error ~= nil and error.lnum ~= nil then
      vim.diagnostic.set(ns, 0, {{
        message=error.message,
        lnum=error.lnum,
        col=0,
        severity=vim.diagnostic.severity.ERROR,
        source='BQN',
      }})
    end

    -- Compute `cto` (clear to) position by looking forward from `to` till we
    -- find first non-empty line. We do this so we clear all "orphaned" virtual
    -- line blocks (which correspond to already deleted lines).
-- 
2.30.2