Skip to content

Commit b4afa55

Browse files
Merge 09467fc into 4049885
2 parents 4049885 + 09467fc commit b4afa55

14 files changed

+117
-40
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ repos:
2424
- r-lib/pkgapi
2525
2626
27+
- pkgbuild
28+
- brio
29+
- decor
30+
- cpp11
2731
- id: use-tidy-description
2832
- id: spell-check
2933
exclude: >
@@ -43,6 +47,9 @@ repos:
4347
(.*/|)\.pre-commit-.*|
4448
.*\.[rR]|
4549
.*\.Rproj|
50+
.*\.cpp|
51+
.*\.h|
52+
.*\.hpp|
4653
.*\.py|
4754
.*\.feather|
4855
.*\.rds|

DESCRIPTION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Collate:
5959
'communicate.R'
6060
'compat-dplyr.R'
6161
'compat-tidyr.R'
62+
'cpp11.R'
6263
'detect-alignment-utils.R'
6364
'detect-alignment.R'
6465
'environments.R'
@@ -103,3 +104,6 @@ Collate:
103104
'vertical.R'
104105
'visit.R'
105106
'zzz.R'
107+
LinkingTo:
108+
cpp11
109+
SystemRequirements: C++11

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ importFrom(rlang,with_handlers)
4747
importFrom(utils,capture.output)
4848
importFrom(utils,tail)
4949
importFrom(utils,write.table)
50+
useDynLib(styler, .registration = TRUE)

R/addins.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ style_active_file <- function() {
7171
abort("Can only style .R, .Rmd and .Rnw files.")
7272
}
7373
rstudioapi::modifyRange(
74-
c(1, 1, length(context$contents) + 1, 1),
75-
paste0(ensure_last_n_empty(out), collapse = "\n"),
74+
c(1L, 1L, length(context$contents) + 1L, 1L),
75+
paste0(ensure_last_n_empty(out, n = 1L), collapse = "\n"),
7676
id = context$id
7777
)
7878
if (save_after_styling_is_active() == TRUE && context$path != "") {

R/cpp11.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Generated by cpp11: do not edit by hand
2+
3+
ensure_last_n_empty <- function(x, n) {
4+
.Call(`_styler_ensure_last_n_empty`, x, n)
5+
}

R/styler.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
#' style_text("a%>%b; a", scope = "tokens")
1919
"_PACKAGE"
2020

21+
## usethis namespace: start
22+
#' @useDynLib styler, .registration = TRUE
23+
## usethis namespace: end
24+
NULL
25+
2126
if (getRversion() >= "2.15.1") {
2227
utils::globalVariables(c(
2328
".",

R/utils.R

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@ line_col_names <- function() {
44
c("line1", "line2", "col1", "col2")
55
}
66

7-
#' Ensure there is one (and only one) blank line at the end of a vector
8-
#' @examples
9-
#' styler:::ensure_last_n_empty("")
10-
#' styler:::ensure_last_n_empty(letters)
11-
#' styler:::ensure_last_n_empty(c(letters, "", "", ""))
12-
#' @keywords internal
13-
ensure_last_n_empty <- function(x, n = 1) {
14-
if (all(x == "")) {
15-
return("")
16-
}
17-
x <- c(x, "", "")
18-
x <- x[seq(1, length(x) - which(rev(x) != "")[1] + 1L)]
19-
c(x, rep("", n))
20-
}
21-
227
#' Replace the newline character with a line break
238
#'
249
#' @param text A character vector

inst/WORDLIST

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ benchmarking
1515
biocthis
1616
bootswatch
1717
BugReports
18+
CallEntries
19+
CallMethodDef
1820
cancelling
1921
cff
2022
chnages
@@ -26,6 +28,7 @@ coercible
2628
compat
2729
config
2830
CONST
31+
const
2932
coventions
3033
covr
3134
cpp
@@ -40,6 +43,9 @@ desc
4043
dev
4144
devtools
4245
dir
46+
DL
47+
dll
48+
DllInfo
4349
docsearch
4450
dont
4551
dontrun
@@ -62,10 +68,13 @@ examplesIf
6268
exampletestr
6369
expr
6470
expr EQ
71+
extern
6572
fileext
6673
filetype
74+
forceSymbols
6775
forcond
6876
formatter
77+
FUNC
6978
funct
7079
gadenbuie
7180
gcc
@@ -81,6 +90,7 @@ grkstyle
8190
GSOC
8291
hashFiles
8392
helpfiles
93+
hpp
8494
href
8595
http
8696
https
@@ -89,8 +99,10 @@ ifelse
8999
impl
90100
Indrajeet
91101
infinitively
102+
init
92103
initializer
93104
inode
105+
inserter
94106
integrations
95107
interaces
96108
invasiveness
@@ -112,6 +124,7 @@ LF
112124
LIBS
113125
lifecycle
114126
Ligges
127+
LinkingTo
115128
lintr
116129
linux
117130
lorenz
@@ -161,13 +174,15 @@ purrr
161174
qmd
162175
Qmd
163176
questionr
177+
rbegin
164178
rcmdcheck
165179
RcppExports
166180
rds
167181
readline
168182
readme
169183
README
170184
rebased
185+
registerRoutines
171186
reindent
172187
reindented
173188
reindention
@@ -217,6 +232,8 @@ sessioninfo
217232
setCacheRootPath
218233
setdiff
219234
setenv
235+
sexp
236+
SEXP
220237
shinydashboardPlus
221238
shinymeta
222239
shinyMonacoEditor
@@ -241,6 +258,7 @@ Sys
241258
sysreq
242259
sysreqs
243260
systemPipeShiny
261+
SystemRequirements
244262
tempfile
245263
testthat
246264
tibble
@@ -268,6 +286,7 @@ unnest
268286
unparsable
269287
unstyled
270288
upsetjs
289+
useDynamicSymbols
271290
usethis
272291
utf
273292
Uwe

man/ensure_last_n_empty.Rd

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.dll
2+
*.o
3+
*.so

src/cpp11.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Generated by cpp11: do not edit by hand
2+
// clang-format off
3+
4+
5+
#include "cpp11/declarations.hpp"
6+
#include <R_ext/Visibility.h>
7+
8+
// ensure_last_n_empty.cpp
9+
std::vector<std::string> ensure_last_n_empty(std::vector<std::string> x, int n);
10+
extern "C" SEXP _styler_ensure_last_n_empty(SEXP x, SEXP n) {
11+
BEGIN_CPP11
12+
return cpp11::as_sexp(ensure_last_n_empty(cpp11::as_cpp<cpp11::decay_t<std::vector<std::string>>>(x), cpp11::as_cpp<cpp11::decay_t<int>>(n)));
13+
END_CPP11
14+
}
15+
16+
extern "C" {
17+
static const R_CallMethodDef CallEntries[] = {
18+
{"_styler_ensure_last_n_empty", (DL_FUNC) &_styler_ensure_last_n_empty, 2},
19+
{NULL, NULL, 0}
20+
};
21+
}
22+
23+
extern "C" attribute_visible void R_init_styler(DllInfo* dll){
24+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
25+
R_useDynamicSymbols(dll, FALSE);
26+
R_forceSymbols(dll, TRUE);
27+
}

src/ensure_last_n_empty.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <cpp11.hpp>
2+
#include <string>
3+
#include <vector>
4+
#include <algorithm>
5+
using namespace cpp11;
6+
7+
//' Ensure there is one (and only one) blank line at the end of a vector
8+
//' @examples
9+
//' styler:::ensure_last_n_empty("")
10+
//' styler:::ensure_last_n_empty(letters)
11+
//' styler:::ensure_last_n_empty(c(letters, "", "", ""))
12+
//' @keywords internal
13+
[[cpp11::register]]
14+
std::vector<std::string> ensure_last_n_empty(std::vector<std::string> x, int n = 1)
15+
{
16+
17+
// Return early if all elements are empty
18+
// There should be only single empty element, no matter the value of `n`
19+
if (std::all_of(x.begin(), x.end(), [](std::string s) { return s == ""; }))
20+
{
21+
std::vector<std::string> x{""};
22+
return x;
23+
}
24+
25+
// If an empty element isn't found at the end, add `n` of them
26+
if (x.back() != "" && n > 0)
27+
{
28+
std::fill_n(std::back_inserter(x), n, "");
29+
return x;
30+
}
31+
32+
auto it = std::find_if(x.rbegin(), x.rend(), [](std::string s) { return s != ""; });
33+
x.erase(it.base(), x.end());
34+
std::fill_n(std::back_inserter(x), n, "");
35+
return x;
36+
}

tests/testthat/test-varia.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
test_that("ensure_last_n_empty", {
44
expect_equal(
5-
ensure_last_n_empty("x"),
5+
ensure_last_n_empty("x", 1L),
66
c("x", "")
77
)
88
expect_equal(
9-
ensure_last_n_empty(c("x", "")),
9+
ensure_last_n_empty(c("x", ""), 1L),
1010
c("x", "")
1111
)
1212
expect_equal(
13-
ensure_last_n_empty(c("1", "2")),
13+
ensure_last_n_empty(c("1", "2"), 1L),
1414
c("1", "2", "")
1515
)
1616
})

touchstone/script.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
library(touchstone)
22

3+
number_of_runs <- 50L
4+
35
branch_install()
46

57
clear_branch_caches <- function() {
@@ -18,7 +20,7 @@ benchmark_run(
1820
cache_deactivate()
1921
},
2022
without_cache = style_pkg("touchstone/sources/here", filetype = c("R", "rmd")),
21-
n = 30
23+
n = number_of_runs
2224
)
2325

2426
clear_branch_caches()
@@ -28,7 +30,7 @@ benchmark_run(
2830
cache_activate(gert::git_branch())
2931
},
3032
cache_applying = style_pkg("touchstone/sources/here", filetype = c("R", "rmd")),
31-
n = 30
33+
n = number_of_runs
3234
)
3335

3436
clear_branch_caches()
@@ -42,7 +44,7 @@ benchmark_run(
4244
gert::git_reset_hard(repo = "touchstone/sources/here")
4345
style_pkg("touchstone/sources/here", filetype = c("R", "rmd"))
4446
},
45-
n = 30
47+
n = number_of_runs
4648
)
4749

4850
clear_branch_caches()

0 commit comments

Comments
 (0)