From 9e015aac1216087f53b4f22fed135c9f0f5e0186 Mon Sep 17 00:00:00 2001 From: RobertASmith Date: Fri, 26 Jan 2024 20:57:33 +0000 Subject: [PATCH] this will still break if there is a comment between function and definition - but think it is more robust --- R/cheers_checker.R | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/R/cheers_checker.R b/R/cheers_checker.R index 7f0d62f..930670b 100644 --- a/R/cheers_checker.R +++ b/R/cheers_checker.R @@ -106,27 +106,48 @@ extract_function_name <- function(string) { #' extract_function_name2 <- function(string){ - foo_def_start <- stringr::str_locate_all(pattern = "(\\s|=|-)function\\s*\\(", - string = string)[[1]][1,1] + string <- stringr::str_replace_all(string, + pattern = c("\n"), + replacement = " ") - assign_operand_locations <- stringr::str_locate_all(pattern = c("=|<-"), - string = string)[[1]][, "start"] + foo_assign_operand_location <- stringr::str_locate_all(pattern = "(\\s|=|<-)function\\s*\\(", + string = string) |> + unlist() |> + head(1) - foo_assign_operand_location <- find_previous_vector_element(value = foo_def_start, - vector = assign_operand_locations) + #assign_operand_locations <- stringr::str_locate_all(pattern = c("=|<-"), + # string = string)[[1]][, "start"] - foo_name <- substr(string, 1, foo_assign_operand_location-1) |> + #foo_assign_operand_location <- find_previous_vector_element(value = foo_def_start, + # vector = assign_operand_locations) + + v_chars <- substr(x = string, + start = 1, + stop = foo_assign_operand_location - 1) |> stringr::str_replace_all(pattern = c("\n"), replacement = " ") |> strsplit(split = " ") |> - unlist() |> - utils::tail(n = 1) + unlist() + + + foo_name <- v_chars[which(!(v_chars %in% c("", "=", "<-")))] |> + utils::tail(n = 1) + + # replace any persisting assignment + foo_name <- stringr::str_replace_all(pattern = c("=|<-"), + string = foo_name, + replacement = "") return(foo_name) } - +extract_function_name2(string = " # hi this is silly ... +myFoo <- +function () { +print('hi') +} + ")