Vim-R-plugin icon indicating copy to clipboard operation
Vim-R-plugin copied to clipboard

Improve support for \ff

Open klmr opened this issue 11 years ago • 2 comments

I particularly like the </kbd>rf command to load a function and jump to the next. However, there are two issues with Vim-R’s implementation which means that I cannot use the feature to its full strength:

  1. Functions need to have a function body enclosed by { … }. Functions consisting of a single statement – e.g. id <- function (x) x – are not supported. This is a shame, since {s in R is unlike braces in C-like languages: they are literally just another R function which groups multiple expressions, and using single expressions unbraced is well in line with R’s functional philosophy.
  2. Functions need to be assigned with <-, using = does not work (the error message “The function assign operator <- was not found.” is displayed).

Both of these things should be fixed, i.e.

  • [ ] Support single-expression function bodies
  • [ ] Support function assignment via =.

klmr avatar May 01 '14 20:05 klmr

I agree that to be consistent with its name the plugin's function “SendFunctionToR” should be able to send single line functions and functions assigned via =. The algorithm used to find the function body is very simple:

  1. Look backwards for the string "function".
  2. Check if in the same line or in the previous line there is the string "<-".
  3. Find the opening brace.
  4. Find the closing brace.

In fact, to be perfect, the SendFunctionToR function should be more sophisticated than that, and it should deal with ->, <<- and ->>. However, I think that the trouble of making the required changes isn't worthwhile for some reasons:

  • There are already shortcuts to send a single line, be it a function or not: <LocalLeader>d and <LocalLeader>l. These shortcuts can be used to send single line functions.
  • The assignment operator that most people recommend to use is <-.
  • The plugin has a shortcut to make it easier to type " <- ", and this is what I use.

I'll replace the SendFunctionToR when I have a lot of spare time to rewrite it (may never happen), or when someone writes a patch improving it. In the meanwhile, we can let this issue open.

Note: The default command to send a function to R Console is <LocalLeader>ff.

jalvesaq avatar May 02 '14 00:05 jalvesaq

The assignment operator that most people recommend to use is <-.

And that’s what I did as well, until fairly recently, but then I noticed that the arguments for using <- are actually quite poor, and there are at least two arguments against it (confuses new programmers because virtually every other mainstream language uses =; potentially introduces bugs, such as x < -5).

Note: The default command to send a function to R Console is <LocalLeader>ff.

Ah yeah, of course.

I’ll see whether I can contribute a patch some time; my Vim script skills are quite poor, unfortunately.

klmr avatar May 02 '14 07:05 klmr