Skip to content

Commit 56b5c8e

Browse files
committed
prototype of the function that divides the character code by calls, and keeps comments
1 parent 38bd16f commit 56b5c8e

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

R/extract_code_as_is_prototype.R

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
code <- "
2+
# initial comment line 1
3+
# initial comment line 2
4+
a <- 1 # A comment
5+
b1 <- 2; b2 <- 2;b3 = 3 # inline comment
6+
c <- 3 # C comment
7+
# inbetween comment
8+
d <- 4
9+
# finishing comment line 1
10+
# finishing comment line 2
11+
"
12+
13+
parsed_code <- parse(text = code)
14+
comments <- extract_comments(parsed_code)
15+
pd <- utils::getParseData(parsed_code)
16+
17+
pd <- pd[pd$token != "';'", ]
18+
19+
get_line_ids <- function(pd) {
20+
if (pd$token[1] == "COMMENT") {
21+
first_comment <- 1:(which(pd$parent == 0)[1]-1)
22+
pd_first_comment <- pd[first_comment, ]
23+
pd <- pd[-first_comment, ]
24+
25+
n <- nrow(pd_first_comment)
26+
first_comment_ids <- data.frame(
27+
lines = c(pd_first_comment[1, "line1"], pd_first_comment[n, "line2"]),
28+
cols = c(pd_first_comment[1, "col1"], pd_first_comment[n, "col2"])
29+
)
30+
} else {
31+
first_comment_ids <- NULL
32+
}
33+
34+
if (pd$token[nrow(pd)] == "COMMENT") {
35+
last_comment <- which(pd$parent == 0 & pd$token == "COMMENT")
36+
pd_last_comment <- pd[last_comment, ]
37+
pd <- pd[-last_comment, ]
38+
39+
n <- nrow(pd_last_comment)
40+
last_comment_ids <- data.frame(
41+
lines = c(pd_last_comment[1, "line1"], pd_last_comment[n, "line2"]),
42+
cols = c(pd_last_comment[1, "col1"], pd_last_comment[n, "col2"])
43+
)
44+
} else {
45+
last_comment_ids <- NULL
46+
}
47+
48+
49+
calls_start <- which(pd$parent == 0)
50+
calls_end <- c(which(pd$parent == 0)[-1] - 1, nrow(pd))
51+
52+
call_ids <- list()
53+
for(i in seq_along(calls_start)) {
54+
call <- pd[c(calls_start[i], calls_end[i]), ]
55+
call_ids[[i]] <-
56+
data.frame(
57+
lines = c(call[1, "line1"], call[2, "line2"]),
58+
cols = c(call[1, "col1"], call[2, "col2"])
59+
)
60+
}
61+
62+
63+
Filter(Negate(is.null), c(list(first_comment_ids), call_ids, list(last_comment_ids)))
64+
}
65+
66+
split_code <- function(code, lines_ids) {
67+
68+
code_split <- strsplit(code, split = "\n", fixed = TRUE)[[1]]
69+
code_split_calls <- list()
70+
71+
for(i in seq_along(lines_ids)) {
72+
73+
code_lines <- code_split[lines_ids[[i]]$lines[1]:lines_ids[[i]]$lines[2]]
74+
75+
if (length(code_lines) == 1) {
76+
code_lines <- substr(code_lines, lines_ids[[i]]$cols[1], lines_ids[[i]]$cols[2])
77+
} else {
78+
code_lines[1] <- substr(code_lines[1], lines_ids[[i]]$cols[1], nchar(code_lines[1]))
79+
code_lines[length(code_lines)] <- substr(code_lines[length(code_lines)], 1, lines_ids[[i]]$cols[2])
80+
}
81+
82+
83+
code_split_calls[[i]] <- paste(code_lines, collapse = "\n")
84+
}
85+
code_split_calls
86+
}
87+
88+
lines_ids <- get_line_ids(pd)
89+
90+
code_by_calls <- split_code(code, lines_ids)
91+
92+
code_by_calls
93+
94+

0 commit comments

Comments
 (0)