Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkf committed Aug 28, 2024
1 parent 779edfa commit 15b3846
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: codegrip
Title: Syntax-Based Editing and Navigation of R Code
Version: 0.0.0.9000
Version: 0.0.0.9001
Authors@R: c(
person("Lionel", "Henry", ,"[email protected]", c("aut", "cre")),
person("Posit PBC", role = c("cph", "fnd"))
Expand All @@ -19,7 +19,7 @@ Suggests:
rstudioapi,
testthat (>= 3.0.0)
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Config/testthat/edition: 3
URL: https://github.com/lionel-/codegrip
BugReports: https://github.com/lionel-/codegrip/issues
17 changes: 9 additions & 8 deletions R/reshape.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ can_reshape <- function(data) {
reshape_info <- function(line, col, ..., info, to = NULL) {
info <- parse_info_complete(info)
call <- find_function_call(line, col, data = info$xml)
call_lines <- node_text_lines(call, info = info)

if (is_null(call)) {
return()
}

pos <- node_positions(call)
cursor_lines <- info$lines[seq(from = pos$line1, to = line)]
n <- length(cursor_lines)
cursor_lines[[n]] <- substring(cursor_lines[[n]], 1, col)
cursor_lines[[1]] <- substring(cursor_lines[[1]], pos$col1)
n_cursor_chars <- sum(nchar(gsub("\\s", "", cursor_lines)))
n_ns_chars <- count_nonspace_chars_to(
call_lines,
line = 1L + line - pos$line1,
col = if (line == 1) 1L + col - pos$col1 else col
)

if (is_null(to)) {
if (node_call_type(call) == "prefix") {
Expand Down Expand Up @@ -74,14 +75,14 @@ reshape_info <- function(line, col, ..., info, to = NULL) {
abort("Unexpected value for `to`.", .internal = TRUE)
)

n_char_re <- sprintf("^((\\s*\\S){%s}).*", n_cursor_chars)
n_cursor_chars <- nchar(gsub(n_char_re, "\\1", reshaped))
n_char_re <- sprintf("^((\\s*\\S){%s}).*", n_ns_chars)
n_char <- nchar(gsub(n_char_re, "\\1", reshaped))

list(
reshaped = reshaped,
start = c(line = pos$line1, col = pos$col1),
end = c(line = pos$line2, col = pos$col2 + 1L),
cursor = c(char = n_cursor_chars)
cursor = c(char = n_char)
)
}

Expand Down
5 changes: 5 additions & 0 deletions R/text.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ skip_space <- function(lines, line, col, ...) {
c(line = i, col = nchar(lines[[i]]))
}

count_nonspace_chars_to <- function(lines, line, col) {
lines[[line]] <- substr(lines[[line]], 1, col)
sum(nchar(gsub("\\s", "", lines)))
}

line_is_at_whitespace <- function(line, col) {
at_end <- col == nchar(line) + 1
at_end || grepl(rx_spaces, substr(line, col, col))
Expand Down
22 changes: 22 additions & 0 deletions R/xpaths.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
xml_node_at <- function(line, col) {
# nolint start
xpath <- paste0(
# any node that is
"//*[",
# followed
"following::*[",
# immediately by a node that is
"position() = 1",
" and ",
# at or beyond 'line', and
"number(@line1) >= %s",
" and ",
# at or beyond 'col'
"number(@col1) > %s",
"]",
"]"
)
# nolint end
sprintf(xpath, line, col)
}

0 comments on commit 15b3846

Please sign in to comment.