Vscode-jupyter: Auto-detect code blocks around the cursor location. Shift-enter executes the entire block

Created on 28 Mar 2019  路  9Comments  路  Source: microsoft/vscode-jupyter

  1. Shift-enter moves to the next line after executing the current line
  2. Auto-detect code blocks around the cursor location. Shift-enter executes the entire block
enhancement needs spec needs split

Most helpful comment

This PR for the PyCharm editor shows a video of the sort of features that No. 2 could provide (automatically sending whole class and function definitions): https://github.com/JetBrains/intellij-community/pull/711

It would be nice if the following blocks were supported:

  1. Compound statement blocks (class, def, with, async, for/else, while/else, if/elif/else, try/except/finally), if the cursor is on the first line with the colon (suggested here https://github.com/microsoft/vscode-python/issues/480#issuecomment-479140136)
  2. Long lines split with \
  3. Multi-line bracketed blocks, as is common when using pandas, e.g.:
(pd.DataFrame({'a':[1,1,2,2], 'b':[1,2,3,4]})
 .groupby('a')
 .agg(sum))
  1. Function calls:
print('hello ' +
    'world')
  1. Multi-line lists and dictionaries:
x =[1,
    2,
    3]

d = {'a':1,
    'b':2,
    'c':3}

EDIT: The cursor would ideally go to the next code block after running the current code block.

All 9 comments

Just to say. This might be an improvement and we should totally consider it. But right now we had shift-enter match what the python extension does when sending code to the terminal. So if we have shift-enter move and advance we won't match the main python extension any more. Though maybe we can consider having both advance on shift-enter.

Do microsoft/vscode-python#1 for sure. Enter a new issue for microsoft/vscode-python#2 if we don't find an easy solution.

No. 2 is on the top of my list of things that would improve VS-py for me, cf. microsoft/vscode-python#480

So if we have shift-enter move and advance we won't match the main python extension any more. Though maybe we can consider having both advance on shift-enter.

@IanMatthewHuff You would match Jupyter Notebook/Lab behaviour though if Shift-Enter advanced after execution and Ctrl-Enter executed in place.

@aldanor Thanks for the heads up on Ctrl-Enter. I'll look into that, at a minimum I could maybe just add a new Execution in place command and leave it unbound so that users could add it to the keybinding of their choice.

@IanMatthewHuff That would be great, yea - at the very least to be able to remap it to user's liking without having to create macros manually.

This PR for the PyCharm editor shows a video of the sort of features that No. 2 could provide (automatically sending whole class and function definitions): https://github.com/JetBrains/intellij-community/pull/711

It would be nice if the following blocks were supported:

  1. Compound statement blocks (class, def, with, async, for/else, while/else, if/elif/else, try/except/finally), if the cursor is on the first line with the colon (suggested here https://github.com/microsoft/vscode-python/issues/480#issuecomment-479140136)
  2. Long lines split with \
  3. Multi-line bracketed blocks, as is common when using pandas, e.g.:
(pd.DataFrame({'a':[1,1,2,2], 'b':[1,2,3,4]})
 .groupby('a')
 .agg(sum))
  1. Function calls:
print('hello ' +
    'world')
  1. Multi-line lists and dictionaries:
x =[1,
    2,
    3]

d = {'a':1,
    'b':2,
    'c':3}

EDIT: The cursor would ideally go to the next code block after running the current code block.

This relates to microsoft/vscode-python#13495. Would make a lot of sense to implement them together.

Auto-detection of code blocks is still the feature that would make the biggest difference to my VS Code Python experience. We added something like this to one of the VS Code R extensions. I was thinking of putting my hand up to do an external contributor PR for the case of detecting blocks like def, if/else etc., but realised that to 'only' handle them you still need to consider some complicated cases.

  1. Simple case example:
if True: # Cursor on this line
    print("hello world")
  1. Complicated case example:
# Cursor on any line
if all([
    True,
    'a' in {
        'a',
        'b',
    },
    len(")") > 0,
]):
    print("hello world")

If the extension is already parsing the file and maintaining an AST with line numbers, these two cases can be handled just as easily. If the extension isn't doing that, then handling the complicated case is a fair amount of work. This file is how we handle it for R: https://github.com/Ikuyadeu/vscode-R/blob/master/src/selection.ts Most of the code is dedicated to handling nested parentheses/brackets/braces over multiple lines.

As a simpler first step that would probably handle most real-world cases, you could use this heuristic:

Proposed heuristic

If the cursor is on a line ending in a colon :, check whether all its parentheses/brackets/braces are matched WITHIN THAT LINE (ignoring parentheses/brackets/braces in strings). If so, use indendation to detect the rest of the block, and send the whole block. If not, show an error message like 'Could not identify block'. If the cursor is on a line that does NOT end in a colon, send just that line.

Would you be interested in an external contributor PR for this proposed heuristic method?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

allefeld picture allefeld  路  4Comments

n-a-sz picture n-a-sz  路  3Comments

dimitry-ishenko picture dimitry-ishenko  路  4Comments

TheOrangeJuice picture TheOrangeJuice  路  4Comments

AlbertDeFusco picture AlbertDeFusco  路  4Comments