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

Error when sending code to the console when there are roxygen2-type comments #106

Open
typhooncamel opened this issue Apr 4, 2024 · 7 comments
Labels

Comments

@typhooncamel
Copy link

If there are roxygen2-type comments (preceded by #') and these are part of a selection being sent to the R console, there will be an error. E.g., if you have

MyFunction = function(x)
{
  print(x)
}


#' Another function.

AnotherFunction = function(x)
{
  print(x)
}

and you visually select the two functions and send to the R session, I get Error: unexpected symbol in " Another function.".

@PMassicotte
Copy link
Collaborator

Looks like the issue is there:

line = line:gsub("^%s*#'", "")

@jalvesaq
Copy link
Member

jalvesaq commented Apr 4, 2024

This is a feature, not a bug. Someone in the past asked for this. The goal is to be able to send sample code from the ROxygen comment area, but any change in this behavior will be good for me. We can remove the feature, create an option to disable it, or leave it as is.

@typhooncamel
Copy link
Author

Ok, good to know. I'm happy either way, at least knowing that's the expected behaviour. I'd say it's a bit odd (because it prevents you from doing precisely what I was doing, sending whole blocks). Testing code in the ROxygen comment area seems somewhat niche. However, I'm happy with whatever the consensus is.

@zkamvar
Copy link

zkamvar commented Jul 31, 2024

Popping in here to say that I have been using this feature heavily when developing packages (confirming that example code works as written).

IIRC, the behaviour in Nvim-R used to be that only code between #' @examples and the end of the roxygen block were evaluated.

@dgkf
Copy link

dgkf commented Aug 9, 2024

I set this up so that it only strips out the #' prefix if all the lines being sent to the console have this prefix and do not begin with a #' @roxygentag as a way of conservatively processing code that is sent to console only when it is likely to be code.

Practically, this was a nice middle-ground - it allows you to send examples to the console, while also letting you send large blocks of code without treating the roxygen headers as code.

The little snippet is in my dotfiles, though approach with caution - I'm not particularly proficient in vimscript. Made for use with vim-slime, but it just accepts a block of text so it should be reasonably portable.

function SlimeOverride_EscapeText_r(text)
    let lines = split(a:text, '\n')
    let linesi = range(0, len(lines) - 1)

    " strip roxygen comment characters if all lines start with #' and no
    " lines continue by starting with a roxygen @tag
    let rox_re = '^\s*#\'''
    let is_rox = repeat([0], len(lines))
    for i in linesi | let is_rox[i] = lines[i] =~ rox_re | endfor
    let is_roxtag = repeat([0], len(lines))
    for i in linesi | let is_roxtag[i] = lines[i] =~ rox_re . '\s*@' | endfor
    if min(is_rox) && !max(is_roxtag)
        let rox_lines = map(deepcopy(lines), {_, x -> substitute(x, rox_re, '', "g")})
        let n_lead_ws = len(a:text)
        for i in linesi
            let n_lead_ws = min([
            \    n_lead_ws, 
            \    len(substitute(rox_lines[i], '^\(\s*\).*', '\1', "g"))
            \ ])
        endfor
        return(join(map(rox_lines, {_, x -> x[n_lead_ws:] . "\r"}), ""))
    endif

    return a:text
endfunction

@PMassicotte
Copy link
Collaborator

That could be a good logic to use here to be implemented in lua.

@SoaresAlisson
Copy link

Not a solution, but in someway related, I use a custom map/keybinding to execute code after # or #'. I didn't know that it was possible to send roxygen code directly.

keymap("n", "<A-r>#", "<esc>F#<esc>wv$<localleader>ss", { desc = "R run commented line" })

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

No branches or pull requests

6 participants