~detegr/nvim-bqn

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
3 2

[PATCH 1/2] Change command names and mappings

Details
Message ID
<20211207143330.258196-1-8mayday@gmail.com>
DKIM signature
missing
Download raw message
Patch: +23 -13
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

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

Details
Message ID
<20211207143330.258196-2-8mayday@gmail.com>
In-Reply-To
<20211207143330.258196-1-8mayday@gmail.com> (view parent)
DKIM signature
missing
Download raw message
Patch: +33 -7
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
Details
Message ID
<20211207193039.32qdlf7qp352zb2y@haukka.localdomain>
In-Reply-To
<20211207143330.258196-1-8mayday@gmail.com> (view parent)
DKIM signature
missing
Download raw message
Hi.

On Tue, Dec 07, 2021 at 05:33:30PM +0300, Andrey Popp wrote:
> 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.
> 
Looking good, thanks! I mapped bqn_clear_file to <leader>bC for now,
let's change that later on if someone comes up with a better binding.

>  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>
This should be xnoremap. I took the liberty to edit the commit to fix
it.
> +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>
Same goes for this.
> +nnoremap <silent> <plug>(bqn_clear_file) :BQNClearFile<CR>
> -- 
> 2.30.2
> 

-- 
Antti

Re: [PATCH 2/2] Set diagnostics for errors reporyed by CBQN

Details
Message ID
<20211207193343.bw2dnzh7h2tkab27@haukka.localdomain>
In-Reply-To
<20211207143330.258196-2-8mayday@gmail.com> (view parent)
DKIM signature
missing
Download raw message
On Tue, Dec 07, 2021 at 05:33:32PM +0300, Andrey Popp wrote:
> 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...

I think it's worth having. Usually I get the errors on the same line I'm
evaluating so I had not thought about needing this info but as it is
availabe, I don't see why we wouldn't do this.

Maybe the column could be calculated from the "arrows" CBQN draws into
the error message. Or then we could submit a patch to CBQN :) Anyway, I
think for now this is good as-is.

> 
>  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
> 

-- 
Antti
Reply to thread Export thread (mbox)