Skip to content

Commit

Permalink
addin_selectWord works
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Aug 2, 2021
1 parent 939c0c6 commit 6fb73ec
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 23 deletions.
41 changes: 30 additions & 11 deletions R/addin.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ addin_insertReturn <- function() {
rstudioapi::insertText("%<>% ")
}

#' @export
addin_selectWord <- function() {
r = rstudioapi::getActiveDocumentContext()
browser()
print(r)
}

# fix new line ending in windows system
# If content is empty, not write
#' @import clipr
Expand Down Expand Up @@ -55,7 +48,6 @@ addin_cutLines <- function() {
#' @rdname addin_cutLines
#' @export
addin_copyLines <- function(output = FALSE){
# info <- getSourceEditorContext()
info <- getActiveDocumentContext()

# set ranges
Expand All @@ -75,6 +67,33 @@ addin_copyLines <- function(output = FALSE){
if (output) return(info)
}

#' @export
addin_selectWord <- function() {
info = rstudioapi::getActiveDocumentContext()
rng <- info$selection[[1]]$range

row = rng$start[1]
col = rng$start[2]
nline <- rng$end[1] - rng$start[1] + 1
nchar <- rng$end[2] - rng$start[2] + 1

# str_post = str_extract(row, glue("(?<=.{{col}})\\w+"))
tryCatch({
str = info$contents[row]
pos = str_locate_all(str, "[\\w\\.]+") %>%
.[start <= col & end >= col, ]

rng$start[2] = pos$start
rng$end[2] = pos$end + 1
info$selection[[1]]$range <- rng
info$selection[[1]]$text <- info$contents[rng$start[1]]

rstudioapi::setSelectionRanges(rng, info$id)
}, error = function(e) {
message(sprintf('%s', e$message))
})
}

#' blind shortcuts to rstudio addin
#' @export
key_blind <- function(){
Expand All @@ -85,12 +104,12 @@ key_blind <- function(){

if (!dir.exists(indir)) dir.create(indir, recursive = TRUE)
if (!file.exists(file_addin)) writeLines("{}", file_addin)

options_addin <- list(
"Ipaper::addin_copyLines" = "Alt+C",
"Ipaper::addin_cutLines" = "Alt+X",
"Ipaper::addin_cutLines" = "Ctrl+X",
"Ipaper::addin_insertDo" = "Ctrl+Alt+D",
"Ipaper::addin_selectWord" = "Alt+D",
"Ipaper::addin_selectWord" = "Ctrl+D",
"Ipaper::addin_insertIn" = "Ctrl+Shift+I",
"Ipaper::addin_insertReturn" = "Ctrl+Shift+,",
"Ipaper::smerge" = "Ctrl+Shift+G",
Expand Down
9 changes: 7 additions & 2 deletions R/file_name.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ str_extract <- function(x, pattern) {
substr(x, ans, ans + attr(ans, "match.length"))
}

str_locate_all <- function(x, pattern) {
str_locate_all.vec <- function(x, pattern) {
ans <- gregexpr(pattern, x, perl = TRUE)[[1]]
ans
}

str_locate_all <- function(x, pattern) {
ans <- gregexpr(pattern, x, perl = TRUE)[[1]]
data.table(I = seq_along(ans), start = ans, end = attr(ans, "match.length") + ans - 1)
}

#' @export
#' @rdname file_name
file_ext <- function(file) {
Expand All @@ -26,7 +31,7 @@ file_ext <- function(file) {
#' @export
file_name <- function(file) {
file <- basename(file)
pos <- str_locate_all(file, "\\.")
pos <- str_locate_all.vec(file, "\\.")
pos <- pos[[length(pos)]]
# I_dot <- pos[nrow(pos), 1]
# no dot
Expand Down
19 changes: 19 additions & 0 deletions R/tools_plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

add_gridLine <- function(dates, col = "grey60", lty = 3, ...) {
years <- year(dates)
date_beg <- ymd( min(years) *1e4 + 0101 )
date_end <- ymd( max(years) *1e4 + 0101 )

t_grids <- seq.Date(date_beg, date_end, by = "year")
abline(v = t_grids, col = col, lty = lty, ...)
}

make_dt <- function(..., ncol = 3) {
x <- list(...)
n <- length(x)
nrow <- floor(n / ncol)
lapply(1:nrow, function(i) {
ind <- seq((i - 1) * ncol + 1, i * ncol)
x[ind] %>% as.data.table()
}) %>% do.call(rbind, .)
}
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ install.packages("../Ipaper_0.1.5.9000.tar.gz", repos = NULL, type = "source", d

| Description | Shortcut |
|---------------------------------------------------|--------------|
| copy lines at the cursor position (sublime style) | Alt+c |
| clip lines at the cursor position | Alt+x |
| copy lines at the cursor position (sublime style) | Alt+C |
| clip lines at the cursor position | Ctrl+X |
| Select word | Ctrl+D |
| Insert `%<>%` | Ctrl+Shift+, |
| Insert `%do% {}` | Ctrl+Shift+D |
| Insert `%in% {}` | Ctrl+Shift+I |
| Open in VSCode | Alt+Shift+V |
| Open in smerge | Ctrl+Shift+G |
| Open in subl | Alt+Shift+L |

Note that `VSCode`, `smerge` or `subl` should be in the system path, if you want to use it.

After install, run `Ipaper::key_blind()` to make those shortcuts work.

### Visualization

Expand Down
16 changes: 8 additions & 8 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
Name: Insert %in%
Name: Insert %in% (Ctrl+Shift+I)
Description: Inserts `%in%` at the cursor position.
Binding: addin_insertIn
Interactive: false

Name: Insert %do% {}
Name: Insert %do% {} (Ctrl+Alt+D)
Description: Inserts `%do% {}` at the cursor position.
Binding: addin_insertDo
Interactive: false

Name: Insert %<>%
Name: Insert %<>% (Ctrl+Shift+,)
Description: Inserts `%<>%` at the cursor position.
Binding: addin_insertReturn
Interactive: false

Name: Copy lines
Name: Copy lines (Alt+C)
Description: Copy lines at the cursor position.
Binding: addin_copyLines
Interactive: false

Name: Cut lines
Name: Cut lines (Ctrl+X)
Description: Cut lines at the cursor position.
Binding: addin_cutLines
Interactive: false
Expand All @@ -28,17 +28,17 @@ Description: Open current working project in smerge
Binding: smerge
Interactive: false

Name: Open in subl
Name: Open in subl (Alt+Shift+L)
Description: Open current working directory in sublime
Binding: subl
Interactive: false

Name: Open in VSCode
Name: Open in VSCode (Alt+Shift+V)
Description: Open current working directory in VSCode
Binding: code
Interactive: false

Name: Select word
Name: Select word (Ctrl+D)
Description: Select word
Binding: addin_selectWord
Interactive: false

0 comments on commit 6fb73ec

Please sign in to comment.