Skip to content

Commit 47cc052

Browse files
committed
Format selection instead of full document if there's one
1 parent ab1807d commit 47cc052

2 files changed

Lines changed: 85 additions & 11 deletions

File tree

R/formatFileAddin.R

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@ reformatFileAddin <- function() {
66
# Get the document data
77
context <- rstudioapi::getActiveDocumentContext()
88
docId <- context$id
9-
docContents <- context$contents
10-
docPath <- context$path
119

12-
# Check if it's a R file
13-
sp <- strsplit(x = docPath, split = ".", fixed = TRUE)
14-
t <- sp[[1]]
15-
ext <- t[length(t)]
16-
if (length(ext) && tolower(ext) == "r") {
17-
# Reformat it
18-
output <- paste(capture.output(formatR::tidy_source(text = docContents)), collapse = "\n")
19-
rstudioapi::setDocumentContents(output, docId)
10+
# If there's a selection, format that
11+
selection <- rstudioapi::primary_selection(context)
12+
selectionText <- selection$text
13+
if (nchar(selectionText) > 0) {
14+
output <- paste(capture.output(formatR::tidy_source(text = selectionText)), collapse = "\n")
15+
rstudioapi::modifyRange(location = selection$range, text = output, id = docId)
2016
} else {
21-
print("formatFile: Only R files are supported. Please focus on an editor with a R file open.")
17+
# If not, try to format the whole file. Check if it's a R file
18+
docPath <- context$path
19+
sp <- strsplit(x = docPath, split = ".", fixed = TRUE)
20+
t <-sp[[1]]
21+
ext<- t[length(t)]
22+
if (length(ext) && tolower(ext) == "r") {
23+
docContents <- context$contents
24+
# Reformat it
25+
output <- paste(capture.output(formatR::tidy_source(text = docContents)), collapse = "\n")
26+
rstudioapi::setDocumentContents(output, docId)
27+
} else {
28+
rstudioapi::showDialog("formatFileAddin error", "Only R files are supported. Please focus on an editor with a R file open.", "")
29+
}
2230
}
2331
}

packrat/packrat.lock

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,64 @@ PackratVersion: 0.4.8.1
33
RVersion: 3.4.2
44
Repos: CRAN=https://cran.rstudio.com/
55

6+
Package: R6
7+
Source: CRAN
8+
Version: 2.2.2
9+
Hash: b2366cd9d2f3851a5704b4e192b985c2
10+
11+
Package: curl
12+
Source: CRAN
13+
Version: 3.0
14+
Hash: fb0029f1c84bf9b510c4f55cda7103f1
15+
16+
Package: devtools
17+
Source: CRAN
18+
Version: 1.13.4
19+
Hash: 80a77b22f641002c7ce0343baeaee300
20+
Requires: digest, git2r, httr, jsonlite, memoise, rstudioapi, whisker,
21+
withr
22+
23+
Package: digest
24+
Source: CRAN
25+
Version: 0.6.12
26+
Hash: e53fb8c58673df868183697e39a6a4d6
27+
628
Package: formatR
729
Source: CRAN
830
Version: 1.5
31+
Hash: 258cf79a8dbeedf1c981cdb53837d2af
32+
33+
Package: git2r
34+
Source: CRAN
35+
Version: 0.19.0
36+
Hash: f4f7966fc357f60dc62dbffcea917eef
37+
38+
Package: httr
39+
Source: CRAN
40+
Version: 1.3.1
41+
Hash: 2d32e01e53d532c812052e27a1021441
42+
Requires: R6, curl, jsonlite, mime, openssl
43+
44+
Package: jsonlite
45+
Source: CRAN
46+
Version: 1.5
47+
Hash: 9c51936d8dd00b2f1d4fe9d10499694c
48+
49+
Package: memoise
50+
Source: CRAN
51+
Version: 1.1.0
52+
Hash: 410fcd334bc626db100237cc1370f2e9
53+
Requires: digest
54+
55+
Package: mime
56+
Source: CRAN
57+
Version: 0.5
58+
Hash: 463550cf44fb6f0a2359368f42eebe62
59+
60+
Package: openssl
61+
Source: CRAN
62+
Version: 0.9.9
63+
Hash: 3858537ac10388a5db1687450908f257
964

1065
Package: packrat
1166
Source: CRAN
@@ -15,3 +70,14 @@ Hash: 6ad605ba7b4b476d84be6632393f5765
1570
Package: rstudioapi
1671
Source: CRAN
1772
Version: 0.7
73+
Hash: e2ebaff8160aff3e6b32e6e78a693c2d
74+
75+
Package: whisker
76+
Source: CRAN
77+
Version: 0.3-2
78+
Hash: 803d662762e532705c2c066a82d066e7
79+
80+
Package: withr
81+
Source: CRAN
82+
Version: 2.1.0
83+
Hash: 097f730987c2dc13d421b65bf01ddf08

0 commit comments

Comments
 (0)