|
| 1 | +# This file is executed with its containing directory as wd |
| 2 | + |
| 3 | +# Remember the working directory (should be extension subfolder that contains this script) |
| 4 | +dir_init <- getwd() |
| 5 | + |
| 6 | + |
| 7 | +# This function is run at the beginning of R's startup sequence |
| 8 | +# Code that is meant to be run at the end of the startup should go in `init_last` |
| 9 | +init_first <- function() { |
| 10 | + # return early if not a vscode term session |
| 11 | + if ( |
| 12 | + !interactive() |
| 13 | + || Sys.getenv("RSTUDIO") != "" |
| 14 | + || Sys.getenv("TERM_PROGRAM") != "vscode" |
| 15 | + ) { |
| 16 | + return() |
| 17 | + } |
| 18 | + |
| 19 | + # check required packages |
| 20 | + required_packages <- c("jsonlite", "rlang") |
| 21 | + missing_packages <- required_packages[ |
| 22 | + !vapply(required_packages, requireNamespace, |
| 23 | + logical(1L), quietly = TRUE |
| 24 | + ) |
| 25 | + ] |
| 26 | + |
| 27 | + if (length(missing_packages)) { |
| 28 | + message( |
| 29 | + "VSCode R Session Watcher requires ", |
| 30 | + toString(missing_packages), ". ", |
| 31 | + "Please install manually in order to use VSCode-R." |
| 32 | + ) |
| 33 | + } else { |
| 34 | + # Initialize vsc utils after loading other default packages |
| 35 | + assign(".First.sys", init_last, envir = globalenv()) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +old.First.sys <- .First.sys |
| 40 | + |
| 41 | +# Overwrite for `.First.sys` |
| 42 | +# Is used to make sure that all default packages are loaded first |
| 43 | +# Will be assigned to and called from the global environment, |
| 44 | +# Will be run with wd being the user's working directory (!) |
| 45 | +init_last <- function() { |
| 46 | + old.First.sys() |
| 47 | + |
| 48 | + # cleanup previous version |
| 49 | + removeTaskCallback("vscode-R") |
| 50 | + options(vscodeR = NULL) |
| 51 | + .vsc.name <- "tools:vscode" |
| 52 | + if (.vsc.name %in% search()) { |
| 53 | + detach(.vsc.name, character.only = TRUE) |
| 54 | + } |
| 55 | + |
| 56 | + # Source vsc utils in new environmeent |
| 57 | + .vsc <- new.env() |
| 58 | + source(file.path(dir_init, "vsc.R"), local = .vsc) |
| 59 | + |
| 60 | + # attach functions that are meant to be called by the user/vscode |
| 61 | + exports <- local({ |
| 62 | + .vsc <- .vsc |
| 63 | + .vsc.attach <- .vsc$attach |
| 64 | + .vsc.view <- .vsc$show_dataview |
| 65 | + .vsc.browser <- .vsc$show_browser |
| 66 | + .vsc.viewer <- .vsc$show_viewer |
| 67 | + .vsc.page_viewer <- .vsc$show_page_viewer |
| 68 | + View <- .vsc.view |
| 69 | + environment() |
| 70 | + }) |
| 71 | + attach(exports, name = .vsc.name, warn.conflicts = FALSE) |
| 72 | + |
| 73 | + # overwrite S3 bindings from other packages |
| 74 | + suppressWarnings({ |
| 75 | + if (!identical(getOption("vsc.helpPanel", "Two"), FALSE)) { |
| 76 | + # Overwrite print function for results of `?` |
| 77 | + .vsc$.S3method( |
| 78 | + "print", |
| 79 | + "help_files_with_topic", |
| 80 | + .vsc$print.help_files_with_topic |
| 81 | + ) |
| 82 | + # Overwrite print function for results of `??` |
| 83 | + .vsc$.S3method( |
| 84 | + "print", |
| 85 | + "hsearch", |
| 86 | + .vsc$print.hsearch |
| 87 | + ) |
| 88 | + } |
| 89 | + # Further S3 overwrites can go here |
| 90 | + # ... |
| 91 | + }) |
| 92 | + |
| 93 | + # remove this function from globalenv() |
| 94 | + suppressWarnings( |
| 95 | + rm(".First.sys", envir = globalenv()) |
| 96 | + ) |
| 97 | + |
| 98 | + # Attach to vscode |
| 99 | + exports$.vsc.attach() |
| 100 | + |
| 101 | + invisible() |
| 102 | +} |
| 103 | + |
| 104 | +init_first() |
0 commit comments