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

Quarto: 'Execute Current Statement' feature in Quarto chunks #1646

Closed
Tracked by #4858
jmcphers opened this issue Oct 19, 2023 · 19 comments
Closed
Tracked by #4858

Quarto: 'Execute Current Statement' feature in Quarto chunks #1646

jmcphers opened this issue Oct 19, 2023 · 19 comments
Assignees
Labels
area: quarto Issues related to Quarto category.

Comments

@jmcphers
Copy link
Collaborator

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
Copy link
Collaborator Author

Related to #1518.

@DavisVaughan
Copy link
Contributor

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

@jmcphers
Copy link
Collaborator Author

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 jmcphers modified the milestones: Future, Release Candidate Feb 20, 2024
@wesm wesm added the area: quarto Issues related to Quarto category. label Feb 29, 2024
@jmcphers
Copy link
Collaborator Author

jmcphers commented Apr 4, 2024

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

@juliasilge juliasilge self-assigned this May 28, 2024
@juliasilge
Copy link
Contributor

We did most of this in #3107 and quarto-dev/quarto#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!

@jeremy-allen
Copy link

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.

@seeM
Copy link
Contributor

seeM commented May 31, 2024

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

@EKtheSage
Copy link

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: #2778 (comment)

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.

@ntluong95
Copy link

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

@juliasilge
Copy link
Contributor

Thanks for sharing this feedback @ntluong95! You are describing the remaining work as outlined in #1646 (comment)

  • 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
Copy link
Contributor

juliasilge commented Oct 28, 2024

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.

@ntluong95

This comment has been minimized.

@DavisVaughan

This comment has been minimized.

@ntluong95

This comment has been minimized.

@ntluong95

This comment has been minimized.

@DavisVaughan

This comment has been minimized.

@ntluong95

This comment has been minimized.

@juliasilge

This comment has been minimized.

@testlabauto
Copy link
Contributor

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:

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: quarto Issues related to Quarto category.
Projects
None yet
Development

No branches or pull requests

9 participants