rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
165 stars 35 forks source link

Code folding is not applied to all selections #249

Open gadenbuie opened 2 years ago

gadenbuie commented 2 years ago

When multiple selection ranges exist in a document, the fold command folds only the most recently selected range. foldAll ignores the selection and folds all foldable ranges. The fold command should fold all selection ranges, otherwise it's a tedious and janky process to fold multiple ranges.

Here's a reprex. Run the code below in RStudio to create a new document and select two ranges. The command to execute the fold action will be written to the console but will wait for you to run it.

library(rstudioapi)

id <- documentNew(
  text = "if (TRUE) {
  # code here
  # code here
} else {
  # code here
  # code here
}"
)

setSelectionRanges(
  id = id,
  ranges = list(
    document_range(
      document_position(2, 1),
      document_position(3, 14)
    ),
    document_range(
      document_position(5, 1),
      document_position(6, 14)
    )
  )
)

sendToConsole(
  sprintf('executeCommand("fold", "%s")', id), 
  execute = FALSE
)
Before After Expected
image image image