Skip to content

Conversation

@ElianHugh
Copy link
Collaborator

Implements jupyter code block style decorators for .R files, piggybacking on the previous rmarkdown code lens implementation.

What problem did you solve?

Closes #191

Screenshot

120947678-be001980-c72f-11eb-91c3-4dae45e42c6b

(If you do not have screenshot) How can I check this pull request?

Open .R file, and make a code block like so:

# %% I am a chunk
mtcars
# %%

# %% So am I
2 * 2
# %%

Markdown code fences should not work in .R files, and the new decorator should not work in rmd files

Implements jupyter code block style decorators for .R files, uses rmd code lens
src/rmarkdown.ts Outdated
if (text.match(/^\s*```+\s*\{\w+\s*.*$/g)) {
return true;
function isRDocument(document: TextDocument) {
return (languages.match('r', document) > 0);
Copy link
Member

@renkun-ken renkun-ken Jun 7, 2021

Choose a reason for hiding this comment

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

Is there a difference using document.languageId instead of languages.match('r', document) > 0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I haven't found any difference, thanks for picking that up! Not sure what the use case would be if languageId exists

src/rmarkdown.ts Outdated
CancellationToken, CodeLens, CodeLensProvider,
CompletionItem, CompletionItemProvider,
Event, EventEmitter, Position, Range, TextDocument, TextEditorDecorationType, window, Selection
Event, EventEmitter, Position, Range, TextDocument, TextEditorDecorationType, window, Selection, languages
Copy link
Member

Choose a reason for hiding this comment

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

languages seems unnecessary now.

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

@renkun-ken renkun-ken merged commit fb1235b into REditorSupport:master Jun 7, 2021
@ElianHugh ElianHugh deleted the code-cells-PR branch June 8, 2021 02:12
@ElianHugh ElianHugh restored the code-cells-PR branch June 8, 2021 02:16
@apoorvalal
Copy link

Thanks for implementing this! Was wondering if it wouldn't be too much trouble to make cell delimiters single #%%s instead of requiring both top and bottom delimiters.

For example, percent scripts produced by jupytext currently skip alternate cells because the #%% at the bottom of a the i-th cell isn't interpreted as the beginning of the i+1-th cell.
image

I'm assuming this is because the current implementation replaces the rmarkdown ``` delimiter with #%%, so it expects both top and bottom fences.

Also, seems like the shortcut to run chunk doesn't work for R scripts fenced by #%%; it runs the entire script when i use Ctrl+Shift+Enter (run chunk shortcut on linux). Clicking on Run Chunk, however, produces the desired behaviour (i.e. it runs just the current cell).

@ElianHugh
Copy link
Collaborator Author

Thank you for the feedback! Just to be 100% clear on expected functionality, would it work like the following:

# %% 
chunk 1

# %%
chunk 2




still chunk 2

i.e., a chunk continues until a new chunk is defined via # %%?

@apoorvalal
Copy link

Yep, exactly. Since there are no text blocks in scripts, there's no need for a separate bottom delimiter.

@Yunuuuu
Copy link

Yunuuuu commented Jan 5, 2022

Thanks for this excellent feature. Can the background between both # %% be customized?

@renkun-ken
Copy link
Member

Thanks for this excellent feature. Can the background between both # %% be customized?

r.rmarkdown.chunkBackgroundColor.

@Yunuuuu
Copy link

Yunuuuu commented Jan 5, 2022

Thanks! renkun! I didn't realize this is the same with rmarkdown. In this case, the problem raised in #901, which indicated Ctrl + Shift + ENTER couldn't work to run current chunk, is because of the when clause:
image

@renkun-ken
Copy link
Member

Could you try changing the when clause to editorTextFocus && (editorLangId == 'r' || editorLangId == 'rmd') and see if it works?

@Yunuuuu
Copy link

Yunuuuu commented Jan 6, 2022

yeah, this can work. I have tried it last night. Just post here to let @ElianHugh know.

@renkun-ken
Copy link
Member

@ElianHugh Looks like we should allow all these commands to run in both r and rmd?

@Yunuuuu
Copy link

Yunuuuu commented Jan 6, 2022

@renkun-ken, thanks for your help. I find the paranthesis is not supported in when clause, as pointed in microsoft/vscode#91473. so the when clause here should be editorLangId == r && editorTextFocus || editorLangId == rmd && editorTextFocus

@ElianHugh
Copy link
Collaborator Author

Seems like a good change to me! I'd like to push this along with some of the jupyter code cell changes, but feel free to push a change sooner if needed

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.

add a way to declare a cell and a command run cell

4 participants