Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code chunk handling in base .R files #1454

Merged
merged 6 commits into from
Nov 26, 2023
Merged

Improve code chunk handling in base .R files #1454

merged 6 commits into from
Nov 26, 2023

Conversation

kylebutts
Copy link
Contributor

@kylebutts kylebutts commented Nov 24, 2023

This PR fixes #1453 and fixes some bugs in handling of code chunks in .R and .Rmd files.

The first change is with getChunks(). Previously, for .R files, the scan would find the first # %% and mark it as the start line. Then, it would go until it found the next # %% and incorrectly mark this as the end line. The line just before the next # %% needs to be the end line (see #1453). This PR fixes this:
CleanShot 2023-11-24 at 15 40 35@2x

The second change is with getCurrentChunk(). I have simplified this code greatly and do much less work. Basically, I check for three edge cases (no chunks, line < first chunk, line < last chunk) and return early. Otherwise, I select the first chunk with .endLine > line. This works both for cursor within chunk and cursor between chunks.

There is one change worth noting. Before, getCurrentChunk() would return undefined when line was after the last chunk. I instead return the last chunk and fix bugs with any commands this impacted.

Testing

To test this, I created these two files (temp.R and temp.Rmd) and tried out the commands for running chunks, selecting current chunk, and going to next and previous chunk.

# %% Chunk 1
print("Chunk 1")

# %% Chunk 2

print("Chunk 2")

# %% Chunk 3
print("Chunk 3")


---
---

```{r}

print("Chunk 1")

```


```{r}
print("Chunk 2")
```

```{r}
print("Chunk 3")
```

- isChunkEndLine was selecting line with the *next* `# %%`, but
needs to select the previous line

- The last chunk was not selected because there is not another `# %%`
after the last one. Instead, add a check for the
last line of the file and close out the last chunk.
- Loop through chunks and grab the first one where the `chunk.endLine >= line`
- Fix bug with `runPreviousChunk` when line is below last chunk
section header is used, e.g. `# Header ----`
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 c05d696 into REditorSupport:master Nov 26, 2023
8 checks passed
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.

Code chunks not being split correctly
2 participants