-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathrstudioapi.R
315 lines (273 loc) · 9.76 KB
/
rstudioapi.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
getActiveDocumentContext <- function() {
# In RStudio this returns either a document context for either the active
# source editor or active console.
# In VSCode this only ever returns the active (or last active) text editor.
# This is because it is currently not possible to tell in VSCode whether
# a text editor or terminal has focus. The concept of active is different.
# It means currently using or most recently used, and applies to text
# editors and terminals separately.
# This shoudln't be much of a limitation as the only context returned for
# the console was the current selection, so it is not very useful.
editor_context <- rstudioapi_call("active_editor_context")
make_rs_document_context(editor_context)
}
getSourceEditorContext <- getActiveDocumentContext
verifyAvailable <- function(version_needed = NULL) {
if (is.null(version_needed)) TRUE else FALSE
}
isAvailable <- function(version_needed = NULL, child_ok) {
verifyAvailable(version_needed)
}
insertText <- function(location, text, id = NULL) {
## insertText also supports insertText("text"), insertText(text = "text"),
## allowing the location parameter to be used for the text when
## text itself is null.
## This is dispatched as a separate request type
if (missing(text) && is.character(location) && length(location) == 1) {
## handling insertText("text")
return(invisible(
rstudioapi_call("replace_text_in_current_selection",
text = location,
id = id
)
))
} else if (missing(location)) {
## handling insertText(text = "text")
return(invisible(rstudioapi_call(
"replace_text_in_current_selection",
text = text,
id = id
)))
} else if (is.null(location) && missing(text)) {
## handling insertText(NULL)
return(invisible(NULL))
}
## ensure normalised_location is a list containing a possible mix of
## document_position and document_range objects
normalised_location <- normalise_pos_or_range_arg(location)
normalised_text <- normalise_text_arg(text, length(normalised_location))
## Having normalised we are guaranteed these are the same length.
## Package up all the edits in a query to send to VSCode in an object
## This is done so the edits can be applied in a single edit object, which
## is hopefull closest to RStudio behaviour.
query <-
mapply(function(location, text) {
list(
operation = if (rstudioapi::is.document_range(location)) {
"modifyRange"
} else {
"insertText"
},
location = location,
text = text
)
},
normalised_location,
normalised_text,
SIMPLIFY = FALSE
)
invisible(
rstudioapi_call("insert_or_modify_text", query = query, id = id)
)
}
modifyRange <- insertText
readPreference <- function(name, default) {
## in future we could map some rstudio preferences to vscode settings.
## since the caller must provide a default this should work.
default
}
readRStudioPreference <- readPreference
.vsc_rstudioapi_env <- environment()
hasFun <- function(name, version_needed = NULL, ...) {
if (!is.null(version_needed)) {
return(FALSE)
}
obj <- .vsc_rstudioapi_env[[name]]
is.function(obj) && !identical(obj, .vsc_not_yet_implemented)
}
findFun <- function(name, version_needed = NULL, ...) {
if (!is.null(version_needed)) {
stop("VSCode does not support used of 'version_needed'.")
}
if (hasFun(name, version_needed = version_needed, ...)) {
.vsc_rstudioapi_env[[name]]
} else {
stop("Cannot find function '", name, "'")
}
}
showDialog <- function(title, message, url = "") {
message <- sprintf("%s: %s \n%s", title, message, url)
invisible(
rstudioapi_call("show_dialog", message = message)
)
}
navigateToFile <- function(file, line = -1L, column = -1L) {
# normalise path since relative paths don't work as URIs in VSC
invisible(
rstudioapi_call(
"navigate_to_file",
file = normalizePath(file),
line = line,
column = column
)
)
}
setSelectionRanges <- function(ranges, id = NULL) {
ranges_or_positions <- normalise_pos_or_range_arg(ranges)
ranges <- lapply(ranges_or_positions, function(location) {
if (rstudioapi::is.document_position(location)) {
rstudioapi::document_range(location, location)
} else {
location
}
})
invisible(
rstudioapi_call("set_selection_ranges", ranges = ranges, id = id)
)
}
setCursorPosition <- setSelectionRanges
documentSave <- function(id = NULL) {
invisible(
rstudioapi_call("document_save", id = id)
)
}
getActiveProject <- function() {
path_object <- rstudioapi_call("get_project_path")
if (is.null(path_object$path)) {
stop(
"No folder for active document. ",
"Is it unsaved? Try saving and run addin again."
)
}
path_object$path
}
.vsc_document_context <- function(id = NULL) {
doc_context <- rstudioapi_call("document_context", id = id)
doc_context
}
documentId <- function(allowConsole = TRUE) document_context()$id$external
documentPath <- function(id = NULL) document_context(id)$id$path
documentSaveAll <- function() {
invisible(
rstudioapi_call("document_save_all")
)
}
documentNew <- function(text,
type = c("r", "rmarkdown", "sql"),
position = rstudioapi::document_position(0, 0),
execute = FALSE) {
if (!rstudioapi::is.document_position((position))) {
stop("DocumentNew requires a document_position object")
}
if (length(text) != 1 || !is.character(text)) {
stop("text for DocumentNew must be a length one character vector.")
}
if (execute) {
message(
"VSCode {rstudioapi} emulation does not support ",
" executing documents upon creation"
)
}
invisible(
rstudioapi_call(
"document_new",
text = text,
type = type,
position = position
)
)
}
setDocumentContents <- function(text, id = NULL) {
whole_document_range <-
rstudioapi::document_range(
rstudioapi::document_position(0, 0),
rstudioapi::document_position(Inf, Inf)
)
insertText(whole_document_range, text, id)
}
restartSession <- function() {
invisible(
rstudioapi_call("restart_r")
)
}
viewer <- function(url, height = NULL) {
# cant bind to this directly because it's not created when the binding is
# made.
.vsc.viewer(url)
}
getVersion <- function() {
numeric_version(0)
}
versionInfo <- function() {
list(
citation = "",
mode = "vscode",
version = numeric_version(0),
release_name = "vscode"
)
}
sendToConsole <- function(code, execute = TRUE, echo = TRUE, focus = FALSE) {
if (!echo) {
stop("rstudioapi::sendToConsole only supports echo = TRUE in VSCode.")
}
code_to_run <- paste0(code, collapse = "\n")
invisible(
rstudioapi_call("send_to_console", code = code_to_run, execute = execute, focus = focus)
)
}
# Unimplemented API calls that will error if called.
.vsc_not_yet_implemented <- function(...) {
stop("This {rstudioapi} function is not currently implemented for VSCode.")
}
getConsoleEditorContext <- .vsc_not_yet_implemented
sourceMarkers <- .vsc_not_yet_implemented
documentClose <- .vsc_not_yet_implemented
showPrompt <- .vsc_not_yet_implemented
showQuestion <- .vsc_not_yet_implemented
updateDialog <- .vsc_not_yet_implemented
openProject <- .vsc_not_yet_implemented
initializeProject <- .vsc_not_yet_implemented
addTheme <- .vsc_not_yet_implemented
applyTheme <- .vsc_not_yet_implemented
convertTheme <- .vsc_not_yet_implemented
getThemeInfo <- .vsc_not_yet_implemented
getThemes <- .vsc_not_yet_implemented
removeTheme <- .vsc_not_yet_implemented
jobAdd <- .vsc_not_yet_implemented
jobAddOutput <- .vsc_not_yet_implemented
jobAddProgress <- .vsc_not_yet_implemented
jobRemove <- .vsc_not_yet_implemented
jobRunScript <- .vsc_not_yet_implemented
jobSetProgress <- .vsc_not_yet_implemented
jobSetState <- .vsc_not_yet_implemented
jobSetStatus <- .vsc_not_yet_implemented
launcherGetInfo <- .vsc_not_yet_implemented
launcherAvailable <- .vsc_not_yet_implemented
launcherGetJobs <- .vsc_not_yet_implemented
launcherConfig <- .vsc_not_yet_implemented
launcherContainer <- .vsc_not_yet_implemented
launcherControlJob <- .vsc_not_yet_implemented
launcherGetJob <- .vsc_not_yet_implemented
launcherHostMount <- .vsc_not_yet_implemented
launcherNfsMount <- .vsc_not_yet_implemented
launcherPlacementConstraint <- .vsc_not_yet_implemented
launcherResourceLimit <- .vsc_not_yet_implemented
launcherSubmitJob <- .vsc_not_yet_implemented
launcherSubmitR <- .vsc_not_yet_implemented
previewRd <- .vsc_not_yet_implemented
previewSql <- .vsc_not_yet_implemented
writePreference <- .vsc_not_yet_implemented
writeRStudioPreference <- .vsc_not_yet_implemented
getPersistentValue <- .vsc_not_yet_implemented
setPersistentValue <- .vsc_not_yet_implemented
savePlotAsImage <- .vsc_not_yet_implemented
createProjectTemplate <- .vsc_not_yet_implemented
hasColourConsole <- .vsc_not_yet_implemented
bugReport <- .vsc_not_yet_implemented
buildToolsCheck <- .vsc_not_yet_implemented
buildToolsInstall <- .vsc_not_yet_implemented
buildToolsExec <- .vsc_not_yet_implemented
dictionariesPath <- .vsc_not_yet_implemented
userDictionariesPath <- .vsc_not_yet_implemented
executeCommand <- .vsc_not_yet_implemented
translateLocalUrl <- .vsc_not_yet_implemented