Skip to content

Enable multiple-level fold section of R document like RStudio#566

Merged
renkun-ken merged 33 commits intoREditorSupport:masterfrom
Yunuuuu:master
Aug 22, 2022
Merged

Enable multiple-level fold section of R document like RStudio#566
renkun-ken merged 33 commits intoREditorSupport:masterfrom
Yunuuuu:master

Conversation

@Yunuuuu
Copy link

@Yunuuuu Yunuuuu commented Aug 2, 2022

Here is some example code to explain this work:

# section one ----
a <- 1:10

## first sub-section ----
b <- letters

### first sub-sub-section ----
d <- 1:2

## second sub-section ----
e <- LETTERS

### sub-sub-section ----
f <- 1:3

#### sub-sub-sub-section ----
g <- 1:20

# complex nested fold section can work well -----
a <- function(x) {b <- function() {
        # collapse following section will not go beyond this function `b`
        # section for function b ------
        x <- 1:2
        x
    }

    # section one ----
    a <- 1:10

    ## first sub-section ----
    b <- letters

    x <- function() {
        # section for function x -----
        c(a, b, d)
    }
}

image
This is what the outline look like:
image

}

# for a r document, we split the document into two element, one for document
# content out of blocks, another for document content in blocks.
Copy link
Author

Choose a reason for hiding this comment

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

blocks ranges are codes between pairs like ("[" and "]"), ("(" and ")"), and ("{" and "}").

line_seq <- start_line:end_line
# only keep blocks between start_line and end_line
block_lines_list <- lapply(
block_lines_list, "[",
Copy link
Author

@Yunuuuu Yunuuuu Aug 2, 2022

Choose a reason for hiding this comment

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

block_lines_list is a list with two element, the first element is all block start lines and the second is all block end lines

line_seq, block_lines_list
)
sections_in_blocks <- NULL
if (length(block_lines_list) && length(block_lines_list[[1L]])) {
Copy link
Author

Choose a reason for hiding this comment

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

if there exists some blocks, we firstly extract the outermost blocks (highest_level_block_lines) and treat the content in these blocks as a new R document, so we can re-use this function to parse them.

@renkun-ken
Copy link
Member

Thanks for re-submitting the PR. I'll take a look later today.

@renkun-ken
Copy link
Member

I re-arranged some functions so that section.R only contains shared functions, and renamed some functions so that they are more consistent with the rest.

@randy3k
Copy link
Member

randy3k commented Aug 17, 2022

Thank you for working on it!

@renkun-ken
Copy link
Member

type = "block",
start_line = start_line,
end_line = end_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

expect_equal(result[[6]]$startLine, 9)
expect_equal(result[[6]]$endLine, 10)
expect_equal(result[[6]]$endLine, 10) # mark
})
Copy link
Author

Choose a reason for hiding this comment

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

By excluding lines with close brackets, this can be fixed

@Yunuuuu
Copy link
Author

Yunuuuu commented Aug 18, 2022

Thanks for the reviewing @renkun-ken, I have run R CMD check in my computer which signal no errors

@renkun-ken
Copy link
Member

In addition to updating existing test cases, are you interested in adding some new test cases for the newly introduced features in this PR?

@Yunuuuu
Copy link
Author

Yunuuuu commented Aug 19, 2022

Sorry for the late reply @renkun-ken, I'll improve the test tomorow, it's a little busy these days.

@Yunuuuu
Copy link
Author

Yunuuuu commented Aug 22, 2022

For symbols, the "Document section symbol works" test in test-symbol.R file has provide nested section symbol test

@Yunuuuu
Copy link
Author

Yunuuuu commented Aug 22, 2022

Sorry for the delayed test commits, it's busy for the new school semester,

Copy link
Member

@renkun-ken renkun-ken left a comment

Choose a reason for hiding this comment

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

LGTM Thanks for working on this!

@renkun-ken renkun-ken merged commit f159575 into REditorSupport:master Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants