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 or inferred code block #2

Closed
andycraig opened this issue Dec 3, 2022 · 8 comments · Fixed by #5
Closed

Run selection or inferred code block #2

andycraig opened this issue Dec 3, 2022 · 8 comments · Fixed by #5

Comments

@andycraig
Copy link

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
Copy link
Owner

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:

const expandedCodeRange = _runInferredCodeBlock(textEditor);

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
Copy link
Owner

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

@andycraig
Copy link
Author

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
Copy link
Owner

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

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

@kylebarron
Copy link
Owner

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

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

@andycraig
Copy link
Author

Works nicely! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants