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

Run Selection/Line in xxx be more smart? #18105

Closed
gonghenghai opened this issue Dec 2, 2021 · 12 comments
Closed

Run Selection/Line in xxx be more smart? #18105

gonghenghai opened this issue Dec 2, 2021 · 12 comments
Labels
area-repl area-terminal community ask Feature request that the community expressed interest in feature-request Request for new features or functionality

Comments

@gonghenghai
Copy link

When we execute the command of Run Selection/Line in xxx, it only executes either the current line if nothing is selected or the selection. For example:

dtypes = {
    'date' : 'str',
    'item_code' : 'category',
    'branch_code' : 'category',
    'sale_qty' : 'float',
    'sale_val' : 'float',
    'return_qty' : 'float',
    'return_val' : 'float'
}

When I put my cursor at the first line, it would attempt to run dtypes = { only. But in the other IDEs such as RStudio, it's a little smart, it intelligently knows that the current line is part of a multiline statement and runs the whole statement.

Thanks.

@gonghenghai gonghenghai added triage-needed Needs assignment to the proper sub-team feature-request Request for new features or functionality labels Dec 2, 2021
@karthiknadig karthiknadig added area-terminal needs community feedback Awaiting community feedback and removed triage-needed Needs assignment to the proper sub-team labels Dec 2, 2021
@karthiknadig
Copy link
Member

Thanks for the feature request! We are going to give the community 60 days from when this issue was created to provide 7 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

@palindali
Copy link

This feature request is based on this question on Stack Overflow, for reference.

@brettcannon
Copy link
Member

Thank you to everyone who upvoted this issue! Since the community showed interest in this feature request we will leave this issue open as something to consider implementing at some point in the future.

We do encourage people to continue 👍 the first/opening comment as it helps us prioritize our work based on what the community seems to want the most.

@brettcannon brettcannon added needs proposal Need to make some design decisions community ask Feature request that the community expressed interest in and removed needs community feedback Awaiting community feedback labels Feb 3, 2022
@cbrnr
Copy link

cbrnr commented Apr 5, 2022

This would be a great feature. FWIW, maybe looking at how other extensions do it might help. For example, the Julia extension uses Shift+Enter, and the R extension uses cmd+Enter to run multiline statements without any selection.

@andycraig
Copy link

Equivalent feature request for Jupyter extension: microsoft/vscode-jupyter#1471

@GitHunter0
Copy link

GitHunter0 commented Sep 3, 2022

RStudio related issue rstudio/rstudio#9014

@andycraig
Copy link

I'd love this block-detection functionality so I'm going to have a try at creating an extension to provide it. My plan is to use the Python Extension Template and make it compatible with both the VS Code Jupyter and VS Code Python extensions.

In the meantime, if there's any move towards providing this functionality within the VS Code Jupyter or VS Code Python extensions themselves, please let me know as I'd very happily leave it to the pros!

@brettcannon
Copy link
Member

There's currently no movement on this from our side, so if you do create an extension please let us know!

@kylebarron
Copy link

kylebarron commented Dec 1, 2022

(This is mostly a copy of my comment in the vscode-jupyter thread here)

I used this functionality in Atom + Hydrogen every day, and now that Atom is dying in more places, I got so frustrated without this functionality in vscode that I made my own vscode extension for this. In this screencast, the only keyboard shortcut used is Shift+Enter, which runs a block and moves down to the start of the next block (Ctrl+Enter/Cmd+Enter are currently mapped to "run the current block but don't move the cursor)

Screen.Recording.2022-11-28.at.10.27.28.PM.mov

Implementation

  • The github repo for this is here: https://github.com/kylebarron/vscode-jupyter-python

  • This is based off of https://github.com/nteract/hydrogen and https://github.com/nikitakit/hydrogen-python, specifically the code here. The idea is pretty simple:

    1. Include the current line plus any additional lines that have a greater initial indent

    2. Define a (user-editable) list of keywords that should be marked as "include this line even if it's at the same indent as the start line". So for example the default list includes elif and else, so that if your cursor is at the initial if line, the if plus elif plus else blocks all get evaluated at the same time.

      The default setting for this is: else, elif, except, finally, }, ], ).

    This heuristic generally works on most "clean" code; I assume it's possible to construct a case that would break the current implementation.

  • Using the block of text inferred by the above heuristic, call the jupyter.execSelectionInteractive command in the jupyter package

Caveats

  • I personally really love IPython and find the rich outputs (like dataframe rendering) useful, so the extension I wrote only supports the jupyter extension at this time and not the bare python extension.

  • I wrote this extension super fast, so it probably has bugs and I don't want to make any promises about my maintenance of it going forward.

@KatlehoJordan
Copy link

Any plans by the VSCode-python team to pick this up? It's been >18 months, and if I am to believe the screencast by @kylebarron, then there's already a starting point to build upon...

This would be an excellent feature, 🤞

@brettcannon
Copy link
Member

We will post when we have anything to specifically share. Unfortunately this issue doesn't even make it to the first page of upvoted issues. But when we take on redesigning any REPL experience we will make sure this is taken into consideration.

@anthonykim1 anthonykim1 mentioned this issue Jul 28, 2023
24 tasks
anthonykim1 added a commit that referenced this issue Oct 10, 2023
There are two Feature Requests from: #18105 #21838 
They are grouped together to provide the smoothest experience: when user
wants to press shift+enter and smoothly move between each executable
Python code block without having to manually move their cursor.

#19955 (For Execute line/selection and advance to next line, referred to
as dynamic smart cursor hereby)
Open Issue: #21778 #21838

Steps in implementing REPL Smart Send (smart shift+enter to the REPL)
and dynamic cursor move aka. Move to Next Line (next executable line of
code to be more precise):

1. Figure out the workflow of where things start and run when user
clicks on run selection/line
2. Send the content of selection & document to the Python Side from
Typescript side.
3. Respect and follow previous logic/code for EXPLICIT selection (user
has highlighting particular text they want to send to REPL), but
otherwise, use newly created smart send code.
4. Receive content of document & selection in Python Side
5. Use AST (From Python standard library) to figure out if selection if
selection is part of, for example, dictionary, but look for nodes and
how each relates to the top level. If some selection is, for example
part of a dictionary, we should run the whole dictionary. Look at how to
do this for all top level, so that we run the Minimum Viable Block
possible. (For example, if user selects part of a dictionary to run in
REPL, it will select and send only the dictionary not the whole class or
file, etc)
6. Receive the commands to run in typescript side and send it to the
REPL
7. After the user has ran shift+enter(non highlight, meaning there was
no explicit highlight of text), thus the incurring of smart send, and we
have processed the smart selection, figure out the "next" executable
line of code in the currently opened Python file.
8. After figuring out the "next" line number, we will move user's cursor
to that line number.

- [x] Additional scope for telemetry EventName.EXECUTION_CODE with the
scope of 'line' in addition to differentiate the explicit selection
usage compared to line or executable block.
- [x] Drop 3.7 support before merging since end_line attribute of the
AST module is only supported for Python version 3.8 and above.
-  [x] Python tests for both smart selection, dynamic cursor move.
-  [x] TypeScript tests for smart selection, dynamic cursor move.

Notes: 
* To be shipped after dropping Python3.7 support, since end_lineno,
which is critical in smart shift+enter logic, is only for Python version
GREATER than 3.7 Update (9/14/23: Python 3.7 support is dropped from the
VS Code Python extension: #21962)
* Code in regards to this feature(s) should be wrapped in standard
experiment (not setting based experiment)
* Respective Telemetry should also be attached
* EXPLICIT (highlight) selection of the text, and shift+enter/run
selection should respect user's selection and send AS IT IS. (When the
user selects/highlight specifically what they want to send, we should
respect user's selection and send the selection as they are selected)
* Smart Shift+Enter should be shipped together with dynamic smart cursor
movement for smoothest experience.
This way user could shift+enter line by line (or more accurately top
block after another top block) as they shift+enter their code.
* Be careful with line_no usage between vscode and python as vscode
counts line number starting from 0 and python ast start as normal
(starts from line 1)) So vscode_lineno + 1 = python_ast_lineno

---------

Co-authored-by: Karthik Nadig <kanadig@microsoft.com>
@anthonykim1
Copy link

anthonykim1 commented Dec 14, 2023

Hi everyone! This is implemented in #21779 Feel free to check out the feature and also note there is setting to toggle Smart Send on/off with a warning that lets you know when feature usage is not possible due to invalid or deprecated Python code. Simply search for repl in extension setting.

@github-actions github-actions bot removed the needs proposal Need to make some design decisions label Dec 14, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-repl area-terminal community ask Feature request that the community expressed interest in feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests