Skip to content

Commit

Permalink
refactoring about context elements extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
plecharpent committed Oct 12, 2020
1 parent ca206ba commit b09d5fa
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions R/get_doc_word_selection.R
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]])
}


0 comments on commit b09d5fa

Please sign in to comment.