R-nvim / R.nvim

Neovim plugin to edit R files
GNU General Public License v3.0
129 stars 15 forks source link

Send/source R functions #61

Closed PMassicotte closed 4 months ago

PMassicotte commented 4 months ago

This PR creates 3 news functions:

    RSendCurrentFun     = { m = "", k = "", c = "Send",     d = "Send the current function" },
    RDSendCurrentFun    = { m = "", k = "", c = "Send",     d = "Send the current function and move the cursor to the end of the function definition" },
    RSourceAllFun       = { m = "", k = "", c = "Send",     d = "Send all the top level functions in the current buffer" },

<localleader>fc, (function current) <localleader>fd, (function current and move down) <localleader>fa, (function all)

jalvesaq commented 4 months ago

Congratulations! The functions work well. But I'm getting these warnings:

Missing <Plug> label in description table: 'RSourceCurrentFun'
Missing <Plug> label in description table: 'RDSourceCurrentFun'

You used different labels in the map_desc table and while creating the maps: RSourceCurrentFun x RSendCurrentFun, and RDSourceCurrentFun x RDSendCurrentFun.

jalvesaq commented 4 months ago

It seems that the cursor is moving to the next line after the function, not the next non-empty line. Also, maybe you could send all functions in a single 'source()` command, as below:

    local lines = {}
    for id, node in r_fun_query:iter_captures(root_node, bufnr, 0, -1) do
        local name = r_fun_query.captures[id]

        -- Kinda hacky, but it works. Check if the parent of the function is
        -- the root node, if so, it's a top level function
        local s, _, _, _ = node:parent():range()

        if name == "rfun" and s == 0 then
            local start_row, _, end_row, _ = node:range()

            -- Only send the function if the user wants to capture the current
            -- function
            if
                capture_all or (cursor_pos >= start_row + 1 and cursor_pos <= end_row + 1)
            then
                M.source_lines(lines, nil)
                lines = vim.fn.extend(
                    lines,
                    vim.api.nvim_buf_get_lines(bufnr, start_row, end_row + 1, false)
                )
                if move_down == true then
                    vim.api.nvim_win_set_cursor(bufnr, { end_row + 1, 0 })
                    cursor.move_next_line()
                end
            end
        end
    end
    if #lines then M.source_lines(lines) end
PMassicotte commented 4 months ago

Tx for the feedback, I will make the change tomorrow morning!

PMassicotte commented 4 months ago

Congratulations! The functions work well. But I'm getting these warnings:

Missing <Plug> label in description table: 'RSourceCurrentFun'
Missing <Plug> label in description table: 'RDSourceCurrentFun'

You used different labels in the map_desc table and while creating the maps: RSourceCurrentFun x RSendCurrentFun, and RDSourceCurrentFun x RDSendCurrentFun.

Good catch!

jalvesaq commented 4 months ago

Is it ready to merge, or do you still plan to work on it?

PMassicotte commented 4 months ago

All good for now!

jalvesaq commented 4 months ago

Thank you! This implements a missing feature!