Skip to content

Commit

Permalink
Rename addins and add a heuristic for guessing the operator to align
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie committed May 30, 2018
1 parent b32a2f0 commit 02d2586
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 22 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
@@ -1,7 +1,7 @@
Package: AlignAssign
Type: Package
Title: Align the assignment operators within a highlighted area
Version: 0.3.0
Version: 0.3.0.9000
Authors@R: person("Luke", "Smith", email = "luke@protocolvital.info",
role = c("aut", "cre"))
URL: https://github.com/seasmith/AlignAssign
Expand All @@ -15,4 +15,4 @@ Imports:
License: GPL-2
Encoding: UTF-8
LazyData: true
RoxygenNote: 5.0.1
RoxygenNote: 6.0.1
3 changes: 2 additions & 1 deletion NAMESPACE
@@ -1,4 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(alignAssign)
export(alignAssign2)
export(alignAssignArrow)
export(alignAssignEqual)
58 changes: 49 additions & 9 deletions R/align_assign.R
Expand Up @@ -65,13 +65,15 @@ insertr <- function(list) {

#' Align a highlighted region's assignment operators.
#'
#' @param rgx_op Regex for assignment operator
#' @return
#' Aligns the single caret operators (\code{<-}) within a highlighted region.
#' Aligns the given or guessed operator within a highlighted region.
#' @export
alignAssign <- function() {
alignAssign <- function(rgx_op = NULL) {
capture <- capture()
area <- captureArea(capture)
loc <- findRegEx("<-", area)
if (is.null(rgx_op)) rgx_op <- guess_operator(area)
loc <- findRegEx(rgx_op, area)
insertList <- assembleInsert(loc)
insertr(insertList)
}
Expand All @@ -81,10 +83,48 @@ alignAssign <- function() {
#' @return Aligns the equal sign assignment operators (\code{=}) within a
#' highlighted region.
#' @export
alignAssign2 <- function() {
capture <- capture()
area <- captureArea(capture)
loc <- findRegEx("=", area)
insertList <- assembleInsert(loc)
insertr(insertList)
alignAssignEqual <- function() {
alignAssign("=")
}

#' Align a highlighted region's assignment operators.
#'
#' @return Aligns the single caret operators (\code{<-}) within a
#' highlighted region.
#' @export
alignAssignArrow <- function() {
alignAssign("<-")
}

guess_operator <- function(area = captureArea(capture())) {
area <- strsplit(area, "\n")
counts <- list(
"=" = vapply(gregexpr("=", area, fixed = TRUE), function(x) length(x[x > 0]), integer(1)),
"<-" = vapply(gregexpr("<-", area, fixed = TRUE), function(x) length(x[x > 0]), integer(1))
)
# Does one appear in all? (keep)
all_ones <- vapply(lapply(counts, function(x) x == 1), all, logical(1))
if (sum(all_ones) == 1) return(names(all_ones)[all_ones])

# Does only one appear at all? (keep)
nones <- vapply(lapply(counts, function(x) x == 0), all, logical(1))
if (sum(nones) == 1) {
return(names(counts)[!nones])
} else if (sum(nones) == 2) {
stop("Neither `=` or `<-` are used in the selected lines")
}

# if not in all or none then are either duplicated on a line? (discard)
mult_in_lines <- vapply(lapply(counts, function(x) x > 1), sum, integer(1))
if (sum(mult_in_lines) == 1) return(names(counts)[!mult_in_lines])

# fall back to max count
some_ones <- vapply(lapply(counts, function(x) x == 1), sum, integer(1))
all_same <- length(unique(counts)) == 1
if (!all_same) {
return(names(which.max(counts)))
} else {
warning("Couldn't guess the operator for alignment, trying ` <- `")
return("<-")
}
}
11 changes: 8 additions & 3 deletions inst/rstudio/addins.dcf
@@ -1,9 +1,14 @@
Name: Align Assign
Description: Aligns '<-' assignment operators within a highlighted area.
Description: Aligns '<-' or '=' assignment operators within a highlighted area.
Binding: alignAssign
Interactive: false

Name: Align Assign 2
Name: Align Assign Arrow
Description: Aligns '<-' assignment operators within a highlighted area.
Binding: alignAssignArrow
Interactive: false

Name: Align Assign Equal
Description: Aligns assignment '=' operators within a highlighted area.
Binding: alignAssign2
Binding: alignAssignEqual
Interactive: false
8 changes: 5 additions & 3 deletions man/alignAssign.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions man/alignAssignArrow.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions man/alignAssign2.Rd → man/alignAssignEqual.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 02d2586

Please sign in to comment.