positron icon indicating copy to clipboard operation
positron copied to clipboard

Quarto: 'Execute Current Statement' feature in Quarto chunks

Open jmcphers opened this issue 2 years ago • 10 comments

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.

jmcphers avatar Oct 19 '23 21:10 jmcphers

Related to https://github.com/posit-dev/positron/issues/1518.

jmcphers avatar Oct 19 '23 21:10 jmcphers

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

DavisVaughan avatar Oct 19 '23 22:10 DavisVaughan

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

jmcphers avatar Oct 19 '23 23:10 jmcphers

Came up in beta feedback: https://github.com/posit-dev/positron-beta/discussions/71

jmcphers avatar Apr 04 '24 21:04 jmcphers

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!

juliasilge avatar May 28 '24 19:05 juliasilge

My brain would be happiest with:

  1. 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.
  2. 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.

jeremy-allen avatar May 30 '24 17:05 jeremy-allen

+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.

seeM avatar May 31 '24 07:05 seeM

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.

EKtheSage avatar Jul 12 '24 21:07 EKtheSage

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 cell works 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 + Enter works 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 cell works as expected
  • Quarto document running R+Python code together:
    • R code chunks run normally
    • Python code: Ctrl + Enter and Alt + 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

ntluong95 avatar Jul 24 '24 21:07 ntluong95

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).

juliasilge avatar Jul 25 '24 17:07 juliasilge

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.

juliasilge avatar Oct 28 '24 17:10 juliasilge

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 #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

ntluong95 avatar Oct 29 '24 07:10 ntluong95

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

DavisVaughan avatar Oct 29 '24 11:10 DavisVaughan

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

ntluong95 avatar Oct 29 '24 11:10 ntluong95

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 avatar Oct 29 '24 22:10 ntluong95

@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.

DavisVaughan avatar Oct 30 '24 12:10 DavisVaughan

Can you tell how to record video, I would love to do that

ntluong95 avatar Oct 30 '24 12:10 ntluong95

I did just check on this, and I do not observe this problem so I think it is likely not related to this change.

juliasilge avatar Oct 30 '24 17:10 juliasilge

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:

testlabauto avatar Oct 30 '24 20:10 testlabauto