Open MilesMcBain opened 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.
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:
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
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.
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
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
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
orthing
.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.