Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send code blocks #19

Open
medwatt opened this issue Oct 7, 2023 · 2 comments
Open

Send code blocks #19

medwatt opened this issue Oct 7, 2023 · 2 comments

Comments

@medwatt
Copy link

medwatt commented Oct 7, 2023

Thanks for this plugin. I was wondering if you could add a feature which can send the block of text to the REPL without the need to visually select.

Consider the code block below.

-- description of function {{{
code
code
code
-- }}}

As long as the cursor is inside the block, calling the function REPLSendBlock would send the code block without having to visually select the block. To make this work, the user would have to provide what an opening and closing regex to mark the beginning and ending of the block. In the example above, opening = "^%-%-[%w%s]+{{{$" and closing = "^%-%-}}}$".

An crude example of how this could be implemented is given below.

local function find_code_block(start_pattern, end_pattern)
    local cur_line = vim.api.nvim_win_get_cursor(0)[1]
    local start_line = nil
    local end_line = nil

    -- Search for starting line
    for i = cur_line, 1, -1 do
        local line_content = vim.api.nvim_buf_get_lines(0, i - 1, i, false)[1]
        if line_content:match(start_pattern) then
            start_line = i
            break
        end
    end

    -- Search for ending line
    local total_lines = vim.api.nvim_buf_line_count(0)
    for i = cur_line, total_lines do
        local line_content = vim.api.nvim_buf_get_lines(0, i - 1, i, false)[1]
        if line_content:match(end_pattern) then
            end_line = i
            break
        end
    end

    -- Output the result
    if start_line and end_line then
        return { start_line, end_line }
    else
        return nil
    end
end

api.nvim_create_user_command('REPLSendBlock', function(opts)
    local id = opts.count
    local name = opts.args
    local current_buffer = api.nvim_get_current_buf()

    local block = find_code_block("^#[%w%s]+{{{$", "^#%s+}}}$")
    if block then
        local start_line, end_line = unpack(block)
        local lines = api.nvim_buf_get_lines(0, start_line, end_line-1, false)
        M._send_strings(id, name, current_buffer, lines)
    end

end, {
    count = true,
    nargs = '?',
    desc = [[
Send code block to REPL `i` or the REPL that current buffer is attached to.
]],
})
@milanglacier
Copy link
Owner

milanglacier commented Oct 8, 2023

I don’t think implement this thing into this plugin as a core command is a good idea. Code block is essentially a range of text, or in the vim notion, a text object. And this plug-in already has a command REPLSendMotion which can operate on text object.

By implementing the “code block” as a text objects, you can do whatever operations to the code block, not limited to send them to REPL but many other things like copy, paste, formatting, etc……

I will add a distributional module dist which will include my own function to create code block as text objects (I don’t consider it as part of the core part of this plugin so it be part of the distributional module). You can then register your code block as text object and directly send them to REPL, say:

you bind REPLSendMotion to key sequence <LocalLeader>s, and you register the code block as acand ic,

then you can just use <LocalLeader>sac to send the code block to the REPL.

@medwatt
Copy link
Author

medwatt commented Oct 9, 2023

Thanks for the input. If I may suggest two unrelated things to add:

  1. the ability to restart a REPL.
  2. clearing the REPL (similar to doing ) without having to be in the REPL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants