Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ca4a3e4
implement multiple-levels r document fold
Mar 8, 2022
10d44c6
unify plural
Mar 8, 2022
0852a38
repair type="subsection" not work in get_r_document_sections
Mar 8, 2022
bd5f018
fix continuous comment line break get_r_document_label_sections
Mar 8, 2022
f7404df
adjust to filter regex
Mar 8, 2022
a154b6e
using over two blank lines to set section breaks
Mar 14, 2022
667c8bc
fix break lines don't work for document symbol in vscode outline viewer
Mar 15, 2022
0d61b57
fix typos and standard function and parameters
Mar 15, 2022
b65f302
fixs blocks in rmd document
Mar 15, 2022
809ea4f
add comment
Mar 15, 2022
ec4fa41
add comment
Mar 15, 2022
4562cec
add "#" to indicate section levels
Mar 15, 2022
c038624
add comment
Mar 23, 2022
a9a1180
unify plural
Mar 23, 2022
956575a
fix typos
Mar 23, 2022
58b8f64
use suggesions of renkun by complying with RStudio
Mar 23, 2022
3000268
add note
Mar 23, 2022
b89653b
implement section ranges in blocks
Mar 24, 2022
6be5fe7
omit unsed
Mar 24, 2022
5a07d32
fix problems that one line section cannot work
Mar 24, 2022
759eca4
omit unused code
Mar 24, 2022
8c1e764
better treat section ranges in blocks
Mar 25, 2022
cfe2db0
use list to store `block_lines` for performance and make plural more …
Mar 25, 2022
e18d947
fix error when no blocks
Apr 12, 2022
8ef9e92
add more comment and rename some varibles
Apr 16, 2022
57852ed
Merge remote-tracking branch 'upstream/master'
May 14, 2022
e428e5c
Merge remote-tracking branch 'upstream/master'
May 31, 2022
efc7f75
fix error when handling a line with only `Comment characters` and whi…
Jun 4, 2022
db3bd09
Merge branch 'REditorSupport:master' into master
Jul 14, 2022
cc1b35f
Rearrange file names and functions
renkun-ken Aug 17, 2022
f2dee25
fix R CMD check errors
Aug 18, 2022
251979b
Update functions
renkun-ken Aug 18, 2022
9ba19ff
test multiple-level section works well
Aug 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ ByteCompile: yes
Encoding: UTF-8
NeedsCompilation: yes
Roxygen: list(markdown = TRUE, r6 = FALSE)
RoxygenNote: 7.1.2
RoxygenNote: 7.2.1
Config/testthat/edition: 3
119 changes: 80 additions & 39 deletions R/folding.R
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
FoldingRangeKind <- list(
Comment = "comment",
Imports = "imports",
Region = "region"
Comment = "comment",
Imports = "imports",
Region = "region"
)

get_block_folding_ranges <- function(xdoc) {
blocks <- xml_find_all(xdoc, "//expr[@line1 < @line2 and
(OP-LEFT-PAREN | OP-LEFT-BRACKET | OP-LEFT-BRACE)/@line1 <
(OP-RIGHT-PAREN | OP-RIGHT-BRACKET | OP-RIGHT-BRACE)/@line1]"
)
if (length(blocks) == 0) {
get_comment_folding_ranges <- function(xdoc) {
comments <- xml_find_all(xdoc, "//COMMENT")
if (identical(length(comments), 0L)) {
return(NULL)
}

block_start <- xml_find_first(blocks, "OP-LEFT-PAREN | OP-LEFT-BRACKET | OP-LEFT-BRACE")
block_end <- xml_find_first(blocks, "OP-RIGHT-PAREN | OP-RIGHT-BRACKET | OP-RIGHT-BRACE")

block_start_line <- as.integer(xml_attr(block_start, "line1"))
block_end_line <- as.integer(xml_attr(block_end, "line1"))

block_folding_ranges <- .mapply(function(start_line, end_line) {
list(
startLine = start_line - 1,
endLine = end_line - 2,
kind = FoldingRangeKind$Region
comments <- comments[
!grepl(
paste0(
"(", section_range_regex, ")\\s*$"
),
xml_text(comments, trim = FALSE),
perl = TRUE
)
}, list(block_start_line, block_end_line), NULL)
block_folding_ranges
}

get_comment_folding_ranges <- function(xdoc) {
comments <- xml_find_all(xdoc, "//COMMENT")
if (length(comments) == 0) {
]
if (identical(length(comments), 0L)) {
return(NULL)
}
comm_line1 <- as.integer(xml_attr(comments, "line1"))
Expand Down Expand Up @@ -64,9 +51,12 @@ get_comment_folding_ranges <- function(xdoc) {
comm_folding_ranges
}

get_section_folding_ranges <- function(uri, document) {
sections <- get_document_sections(uri, document, type = c("section", "chunk"))
if (length(sections) == 0) {
get_section_and_block_folding_ranges <- function(document, xdoc) {

sections <- get_document_sections_and_blocks(
document = document, xdoc = xdoc
)
if (!length(sections)) {
return(NULL)
}
section_folding_ranges <- lapply(sections, function(section) {
Expand All @@ -79,11 +69,62 @@ get_section_folding_ranges <- function(uri, document) {
section_folding_ranges
}

#' Main util function to get folding range (sections and blocks).
#' sections ranges are indicated by `section_mark_suffix`
#' blocks ranges are codes between pairs like ("[" and "]"), ("(" and ")"), and ("{" and "}").
#' @noRd
get_document_sections_and_blocks <- function(document, xdoc) {
if (document$is_rmarkdown) {
get_rmd_document_sections_and_blocks(document$content, xdoc = xdoc)
} else {
get_r_document_sections_and_blocks(
content = document$content, xdoc = xdoc, symbol = FALSE
)
}
}

#' @noRd
get_document_blocks <- function(xdoc) {
if (is.null(xdoc)) {
return(NULL)
}
blocks <- xml_find_all(xdoc, "//expr[@line1 < @line2 and
(OP-LEFT-PAREN | OP-LEFT-BRACKET | OP-LEFT-BRACE)/@line1 <
(OP-RIGHT-PAREN | OP-RIGHT-BRACKET | OP-RIGHT-BRACE)/@line1]")
if (!length(blocks)) { # prevent floating point comparision
return(NULL)
}

block_start <- xml_find_first(blocks, "OP-LEFT-PAREN | OP-LEFT-BRACKET | OP-LEFT-BRACE")
block_end <- xml_find_first(blocks, "OP-RIGHT-PAREN | OP-RIGHT-BRACKET | OP-RIGHT-BRACE")

block_start_line <- as.integer(xml_attr(block_start, "line1"))
block_end_line <- as.integer(xml_attr(block_end, "line1"))

block_folding_ranges <- .mapply(function(start_line, end_line) {
list(
type = "block",
start_line = start_line,
end_line = end_line - 1L
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exclude lines where close brackets stand

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what r documents do

)
}, list(block_start_line, block_end_line), NULL)
block_folding_ranges
}

#---------------------------rmd document utils functions----------------------#
#' rmd document util function to get folding range - sections and blocks.
#' @noRd
get_rmd_document_sections_and_blocks <- function(content, xdoc) {
blocks <- get_document_blocks(xdoc)
sections <- get_rmd_document_sections(
content, c("section", "chunk")
)
c(blocks, sections)
}

#' Get all the folding ranges in the document
#' @noRd
document_folding_range_reply <- function(id, uri, workspace, document) {
result <- NULL

parse_data <- workspace$get_parse_data(uri)
if (is.null(parse_data) ||
(!is.null(parse_data$version) && parse_data$version != document$version)) {
Expand All @@ -92,16 +133,16 @@ document_folding_range_reply <- function(id, uri, workspace, document) {

xdoc <- parse_data$xml_doc
if (!is.null(xdoc)) {
result <- c(result,
get_block_folding_ranges(xdoc),
get_comment_folding_ranges(xdoc)
)
comment_ranges <- get_comment_folding_ranges(xdoc)
}
section_ranges <- get_section_and_block_folding_ranges(
document, xdoc
)

result <- c(result, get_section_folding_ranges(uri, document))
result <- c(comment_ranges, section_ranges)

result <- unique(result)
if (length(result) == 0) {
if (!length(result)) { # prevent floating point comparision
Response$new(id)
} else {
Response$new(
Expand Down
Loading