rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
170 stars 37 forks source link

Request: API function to get object at cursor? #207

Open MilesMcBain opened 4 years ago

MilesMcBain commented 4 years ago

It would return the name of the object the cursor is currently on. Similar to Emacs' symbol-at-point.

It saves users having to make a selection that spans an object name to initiate automations that involve an object binding.

In the case of object$thing the command returns the parts either side of the $ depending on where the cursor is. i.e. object or thing.

I homebrewed up an implementation for {drake}, and am just about to make another package that I would like to use it in. It would be great if it could be supported in the official API.

MilesMcBain commented 4 years ago

Edit, I guess if it wanted to be really nice it could return the span of the object as a range for people who want to manipulate the text. Although that's not the motivating case for me.

kevinushey commented 4 years ago

I'm a bit apprehensive about this just because it would be easy for this feature to spiral out of control a bit -- e.g. in some cases you might want all of foo$bar; sometimes you might want foo@bar; maybe in foo[[bar]] you want foo and not bar, and so on.

I'd prefer a solution around these lines:

  1. Get the current line + cursor position,
  2. Get the R tokens for that line,
  3. Get the tokens as required.

FWIW the sourcetools package makes this somewhat easier, e.g.

> sourcetools::tokenize(text = "foo$bar")
  value row column     type
1   foo   1      1   symbol
2     $   1      4 operator
3   bar   1      5   symbol
MilesMcBain commented 4 years ago

Thanks for the heads-up on sourcetools. I already want to refactor some stuff to use it.

I can see why you are worried about feature creep. In my mind this is one of those 80% things that works very well in a large number of common cases but falls down at the edges - in this case with nested objects.

Sometimes when I code something that uses the at-point style interface I prefer the selection, if one has been made. So the user has the option to do selections - the status quo - for nested stuff.

MilesMcBain commented 3 years ago

I reeeeaallly think this should be up for consideration. In VSCode we have a function called r.runCommandWithSelectionOrWord which powers many useful keybindings and automations.

The cases you highlight are occasionally issues, but the user can always fall back to making a selection that covers the precise text to be used in the command.

So can we change this to a request for an API function to get object at cursor or selected text?

Or maybe implement the same "runner" pattern: https://github.com/Ikuyadeu/vscode-R/wiki/Keyboard-shortcuts#creating-keybindings-for-r-commands

MilesMcBain commented 3 years ago

I increasingly find myself wanting this across multiple packages. I have implemented it a few times. I think this is the best one:

https://github.com/MilesMcBain/rmdocs/blob/main/R/rstudio.R#L17