kevinushey / sourcetools

Tools for reading, tokenizing, and parsing R code.
MIT License
77 stars 3 forks source link

Find complete expression #3

Open hadley opened 8 years ago

hadley commented 8 years ago

Assuming that the cursor is •, it would be handy to take:

ggplot() + 
  geom_point() + 
  geom_line(•)

# OR
mtcars %>%
  filter(•) %>%
  subset() %>%

And have a keyboard shortcut that ran the complete expression. I'm not entirely sure how you'd implement this (or if it's even possible in general), but it would be super helpful if you could find the full expression given the current cursor.

kevinushey commented 8 years ago

Thinking out loud, the API for this could look something like:

tokens <- tokenize_string("<code>")
cursor <- token_cursor(tokens, row, column) ## place cursor on token at [row, col]
result <- whole_expression(cursor)

The token_cursor would be a simple stateful 'pointer' to tokens, which you could move forwards, backwards, or to matching parentheses, and so on.

result would probably be something like the document_selection used in the rstudioapi package.