kylebarron / vscode-jupyter-python

Run automatically-inferred Python code blocks in the VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=kylebarron.vscode-jupyter-python
MIT License
37 stars 3 forks source link

Run selection or inferred code block #2

Closed andycraig closed 1 year ago

andycraig commented 1 year ago

How about a command that runs the selected code if there is a selection, or the inferred code block if there is no selection? This could be incorporated into the existing Jupyter Python: Run inferred code block, or added as a separate command (e.g., Jupyter Python: Run selection/inferred code block).

As a workaround I have bound Jupyter Python: Run inferred code block to one keyboard shortcut which I use when there is no selection and Jupyter: Run Selection/Line in Interactive Window to another shortcut which I use when there is a selection, but it would be nice to be able to use a single shortcut.

I would be happy to do a PR for this if you'd be interested in one.

kylebarron commented 1 year ago

That would be a good feature. It also matches what I'm used to in Atom. If there's an active selection use that; otherwise infer a block.

I think it's as simple as here: https://github.com/kylebarron/vscode-jupyter-python/blob/4da46bd92458b2686c15cd08668e602331e491d6/src/run-code.ts#L30 checking if the start and end of textEditor.selection are the same. If so, infer a block, otherwise run the selection.

We might need a refactor to handle shift+enter when running a selection if we still want the cursor to jump to the next code start

kylebarron commented 1 year ago

I'd also really love to figure out some basic testing on ci so we can make changes more easily.

andycraig commented 1 year ago

Great! That sounds right to me for the implementation too.

We might need a refactor to handle shift+enter when running a selection if we still want the cursor to jump to the next code start

When running a selection I normally want to retain the selection (i.e., not have the cursor move), but that may just be me.

If you'd like me to have a go at a PR let me know!

I'd also really love to figure out some basic testing on ci so we can make changes more easily.

In case it's helpful, here's how I went about unit testing similar functionality for R: https://github.com/REditorSupport/vscode-R/blob/master/src/test/suite/extendSelection.test.ts

I'm pretty sure this is the bit that runs them in GitHub Actions: https://github.com/REditorSupport/vscode-R/blob/4f6dc498541e8d3a3e17cad5fb9633a499e3d856/.github/workflows/main.yml#L6-L21

The vscode-jupyter extension does integration tests that actually create and manipulate documents (although I've never set something like that up myself): https://github.com/microsoft/vscode-jupyter/blob/5ee34a00fbbfde4b38a44ed2510860eb5d691e61/src/test/datascience/editor-integration/codewatcher.unit.test.ts#L905-L948

kylebarron commented 1 year ago

When running a selection I normally want to retain the selection (i.e., not have the cursor move), but that may just be me.

Yeah me too. I was thinking Ctrl+Enter wouldn't move the cursor (just like when you haven't selected a block) while Shift+Enter would move the cursor to the next block regardless of whether you start with a selection or not.

andycraig commented 1 year ago

That should work. How does Atom do it as a matter of interest?

kylebarron commented 1 year ago

in terms of implementation or behavior? behavior should be described above, where ctrl doesn't move cursor; shift does. if a selection exists, that takes precedence over an inferred block

andycraig commented 1 year ago

I was wondering about behaviour. Got it, so it would match Atom's behaviour.

andycraig commented 1 year ago

Works nicely! Thank you!