From 0835feb33a297ac9d853c08e54f3f77164e9477f Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Sat, 13 Mar 2021 14:31:47 +0800 Subject: [PATCH 1/3] Support options in arg completion --- R/completion.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/R/completion.R b/R/completion.R index 9c5e3015..b825f133 100644 --- a/R/completion.R +++ b/R/completion.R @@ -112,6 +112,11 @@ arg_completion <- function(uri, workspace, point, token, funct, package = NULL, if (!is.null(package)) { args <- names(workspace$get_formals(funct, package, exported_only = exported_only)) + + if (package == "base" && funct == "options") { + args <- c(args, names(options())) + } + if (is.character(args)) { token_args <- args[startsWith(args, token)] token_data <- list( From a4056be007d5fee8ddd123bb8b57057d7d676b6c Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Sat, 13 Mar 2021 14:41:12 +0800 Subject: [PATCH 2/3] Add test cases --- tests/testthat/test-completion.R | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/testthat/test-completion.R b/tests/testthat/test-completion.R index 7e113e38..c0177a57 100644 --- a/tests/testthat/test-completion.R +++ b/tests/testthat/test-completion.R @@ -88,6 +88,33 @@ test_that("Completion of function arguments works", { expect_length(arg_items, 0) }) +test_that("Completion of options works", { + skip_on_cran() + client <- language_client() + + temp_file <- withr::local_tempfile(fileext = ".R") + writeLines( + c( + "options(sci", + "options(scipen = 999, useFancy" + ), + temp_file) + + client %>% did_save(temp_file) + + result <- client %>% respond_completion(temp_file, c(0, 11)) + arg_items <- result$items %>% + keep(~ identical(.$data$type, "parameter")) %>% + map_chr(~ .$label) + expect_identical(arg_items, "scipen") + + result <- client %>% respond_completion(temp_file, c(1, 30)) + arg_items <- result$items %>% + keep(~ identical(.$data$type, "parameter")) %>% + map_chr(~ .$label) + expect_identical(arg_items, "useFancyQuotes") +}) + test_that("Completion of function arguments preserves the order of arguments", { skip_on_cran() client <- language_client() From 68a3ee9db57164af8f520f398d4aa5045d75795d Mon Sep 17 00:00:00 2001 From: Kun Ren Date: Wed, 17 Mar 2021 22:20:41 +0800 Subject: [PATCH 3/3] Use .Options for better performance --- R/completion.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/completion.R b/R/completion.R index b825f133..7caaa3b0 100644 --- a/R/completion.R +++ b/R/completion.R @@ -114,7 +114,7 @@ arg_completion <- function(uri, workspace, point, token, funct, package = NULL, args <- names(workspace$get_formals(funct, package, exported_only = exported_only)) if (package == "base" && funct == "options") { - args <- c(args, names(options())) + args <- c(args, names(.Options)) } if (is.character(args)) {