Skip to content

Commit

Permalink
this will still break if there is a comment between function and defi…
Browse files Browse the repository at this point in the history
…nition - but think it is more robust
  • Loading branch information
RobertASmith committed Jan 26, 2024
1 parent bb11003 commit 9e015aa
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions R/cheers_checker.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
")



Expand Down

0 comments on commit 9e015aa

Please sign in to comment.