positron
positron copied to clipboard
Quarto: 'Execute Current Statement' feature in Quarto chunks
Currently, Quarto takes over Cmd+Enter to execute code inside Quarto chunks. Consequently our statement detection logic doesn't work in Quarto, and if you don't have a selection, it just sends one line at a time to the Positron console.
We could implement Execute Current Statement for Quarto by using the statement range providers to detect a region for execution. This could be accomplished using the request forwarding technique along with a new command called e.g. positron.executeStatementRangeProvider. This has the advantage of putting Quarto in charge of cursor movement and code execution, in case it wants to do anything special (UI decoration, wrapping in reticulate, etc.)
Another approach would be to implement a higher-level command Quarto could call to have Positron handle invocation of the providers and code execution; Quarto would just trigger it with the cursor position and let the command do the rest.
Related to https://github.com/posit-dev/positron/issues/1518.
Would Quarto "fake" a document corresponding to the current code chunk for the provider to parse?
I've been wondering if we need some kind of alternative API that takes a string containing the contents of the "document" along with a position, so you don't have to come up with a document URI yourself
That's more or less exactly what the request forwarding technique does. It creates a virtual document with its own URI that contains the original document but with all unrelated code replaced with whitespace. E.g. from the link above:
The virtual text document provider provides CSS language server with virtual content, where all non-CSS code is replaced with whitespace.
Quarto is already generating these virtual documents in its VS Code extension: https://github.com/quarto-dev/quarto/blob/main/apps/vscode/src/vdoc/vdoc.ts
Came up in beta feedback: https://github.com/posit-dev/positron-beta/discussions/71
We did most of this in #3107 and https://github.com/quarto-dev/quarto/pull/436/ but have some remaining thoughts to work through:
- Only R Quarto files are going through Positron's statement range provider. Python Quarto files are going through the Jupyter executors from the Quarto extension (since they have Jupyter kernels) still and the behavior is slightly different. That may be preferable for Python users and I'm not entirely sure which way we should go. Changing the behavior would involve changes to the Quarto VS Code extension, not Positron.
- Quarto files with mixed R and Python chunks have some remaining problems but are mostly functional because they go through knitr and reticulate. Definitely could be improved!
My brain would be happiest with:
- a consistent shortcut for line execution in any context in any language, .R, .py, a chunk in .Rmd, and a chunk in .qmd. This is what I think of when I think of cmd+enter, and this would include running more than a single line if the cursor is in a function definition or a dplyr pipe.
- another consistent shortcut for cell execution in any file that has cells
The thing I am trying to avoid is using cmd+enter for line execution in my .py file while defining helper functions, then going to my python .qmd file in the same project and suddenly cmd+enter runs a whole cell.
+1 to what Jeremy said about Cmd+Enter in Python. For executing an entire cell, I would want Shift+Enter to do that, and move to the next cell, as it does in Jupyter UIs.
Hello, what if we use ALT + Enter to execute a block of code for qmd, ipynb, R and py files? If we don't want to change the default behavior of Ctrl + Enter in jupyter, perhaps using ALT + Enter could work.
I asked similar question here: https://github.com/posit-dev/positron/issues/2778#issuecomment-2223316213
For the most part, ALT + Enter does what I want, i.e., it executes the current block of the code and move to the next block. I also don't need to highlight the code I want to run, it just knows to execute the entire statement.
There are some caveats though, if the language is python, pressing Alt + Enter "sometimes" move the cursor to the incorrect next block of code. If the language is R in qmd, pressing Alt + Enter or Ctrl + Enter executes the code block then move to the next code block correctly. Pressing Alt + Enter does nothing in R script.
After several hours digging on how to best work with R and Python in .qmd file, it seems the user experience still separately.
First, I like the way Positron can replicate my experience of running R code for Python code in .py script, so press Ctrl + Enter always reliable, executing exactly things I want to do (line-by-line, a code block like pipe or function definition, run selection).
However, with Quarto document it is very disappointed. I cannot wrap my head on how complex the situations are:
- Quarto document running R code only:
Ctrl + Enter: Works as expected.- Click on
Run cellworks as expected
- Quarto document running Python code only:
Ctrl + Enter: Doesn't works as expected (run the whole cell).- If highlighting part of the code to run,
Ctrl + Enterworks as expected. Alt + Enter: Run a single line but jump randomly to any next lines,Shift + Enter: Run the whole cell (in Python console).- Click on
Run cellworks as expected
- Quarto document running R+Python code together:
- R code chunks run normally
- Python code:
Ctrl + EnterandAlt + Enter: Doesn't works as expected (Run a single line but jump randomly to any next lines). Shift + Enter: Run the whole cell (in R console, reticulate)- Click on `Run cell will run (in R console, reticulate)
I agree with Jeremy that the experience must be the same for every file type. Or at least with me, I only need Ctrl + Enter for run line-by-line and run highlighted selection, and click on Run cell for run the whole code chunk @juliasilge @seeM
Thanks for sharing this feedback @ntluong95! You are describing the remaining work as outlined in https://github.com/posit-dev/positron/issues/1646#issuecomment-2135993785
- Python Quarto files are going through the Jupyter executors from the Quarto extension (since they have Jupyter kernels) still and the behavior is somewhat different than if we were using the Positron statement range provider.
- Quarto files with mixed R and Python chunks go through knitr and reticulate and we can improve the experience here (i.e. use the Python console directly).
I have a new version of the Quarto extension ready to go to, for improved behavior here. You can currently get this as an artifact here (download the artifact, unzip, install the .vsix manually).
QA Notes
We can test out the new, improved behavior with two .qmd files, one for R and one for Python. In both, you can now use Cmd/Ctrl+Enter to run the current statement in the Positron console. No longer will we run the whole cell/chunk for Python.
The other item we were tracking in this issue was improvements to reticulate (R + Python) .qmd files. This is currently not terrible (quarto.runCurrent will choose the right console) but there are further improvements needed that we can track in #3865 and other new issues.
I have a new version of the Quarto extension ready to go to, for improved behavior here. You can currently get this as an artifact here (download the artifact, unzip, install the
.vsixmanually).QA Notes
We can test out the new, improved behavior with two
.qmdfiles, one for R and one for Python. In both, you can now use Cmd/Ctrl+Enter to run the current statement in the Positron console. No longer will we run the whole cell/chunk for Python.The other item we were tracking in this issue was improvements to reticulate (R + Python)
.qmdfiles. This is currently not terrible (quarto.runCurrentwill choose the right console) but there are further improvements needed that we can track in #3855 and other new issues.
I tested this one. It solved the problem but a new issue arising. This is when you press Ctrl + Shift + I, it no longer allowing you to select the language for the code chunk, it always insert the code chunk for the language used at the latest
That's not an issue, that's a new feature, because 99% of the time you use the language of the previous chunk. Many of us felt it was quite a pain to have to reselect the language over and over. This is also the way it works in RStudio
That's not an issue, that's a new feature, because 99% of the time you use the language of the previous chunk. Many of us felt it was quite a pain to have to reselect the language over and over. This is also the way it works in RStudio
Depend on the workflow I think. May be have an option to toggle the feature on and off
I found another problem. It is when you click on Run Cell, it will send code to R console instead of Python console. Press Ctrl + Enter works normally
@ntluong95 could you please open a new issue as you find new problems? And possibly over on https://github.com/quarto-dev/quarto if you feel it is quarto specific? Please include videos and as much detail as possible.
Can you tell how to record video, I would love to do that
I did just check on this, and I do not observe this problem so I think it is likely not related to this change.
Verified Fixed
Positron Version(s) : 2024.11.0-129
OS Version : OSX
Test scenario(s)
Able to "step through" QMD cells in R and python properly after some local system clean up.
Link(s) to TestRail test cases run or created: