-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring about context elements extraction
- Loading branch information
1 parent
ca206ba
commit b09d5fa
Showing
1 changed file
with
36 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,57 @@ | ||
get_doc_word_selection <- function(unique = TRUE) { | ||
|
||
sel <- rstudioapi::getSourceEditorContext() | ||
sel <- get_editor_context(what = "selection") | ||
|
||
# test if only one selection | ||
if (length(sel$selection) > 1) { | ||
if (length(sel) > 1) { | ||
warning("Multiple words selection !") | ||
return() | ||
} | ||
|
||
# test if it's null | ||
if (sel$selection[[1]]$text == "") { | ||
if (sel[[1]]$text == "") { | ||
warning("empty selection!") | ||
return() | ||
} | ||
|
||
# test if it'contains several words | ||
words <- strsplit(x = trimws(sel$selection[[1]]$text), split = " ")[[1]] | ||
words <- strsplit(x = trimws(sel[[1]]$text), split = " ")[[1]] | ||
|
||
if (length(words) > 1 && unique) { | ||
warning("multiple words selected") | ||
return() | ||
} | ||
|
||
return(sel$selection[[1]]) | ||
return(sel[[1]]) | ||
} | ||
|
||
|
||
|
||
get_active_doc <- function(full_path = TRUE) { | ||
|
||
path <- get_editor_context(what = "path") | ||
|
||
if(full_path) return(path) | ||
|
||
return(basename(path)) | ||
|
||
} | ||
|
||
|
||
get_editor_context <- function(what = NULL) { | ||
|
||
context <- rstudioapi::getSourceEditorContext() | ||
|
||
if (is.null(what)) return(context) | ||
|
||
if (length(what) > 1) { | ||
warning("Only one fieldname allowed !") | ||
return() | ||
} | ||
|
||
if(! what %in% names(context)) stop("Unknown context field name !") | ||
|
||
return(context[[what]]) | ||
} | ||
|
||
|