-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Make "Run line" to work on partial statements (e.g. code blocks, loops, etc.) #4431
Comments
@GershTri this is a good feature to have, @ccordoba12, what do you think? |
This will require restructuring the code we use now to execute a line, which is very simple:
So step 1. won't be needed anymore and step 2. needs to be changed to "press Enter in the console" or something like that. I know RStudio users are fans of this feature, but it's not easy to implement it correctly in Spyder (at least not as easy as what we have now). So I'm marking this for Spyder 4, but I can't make promises that this will be available in the final release. |
My idea would be just to run the whole indented level of code when you are at the start. For example: def foo(bars):
print(bars)
for bar in bars:
if bar == 2:
print(bar)
else:
print(bar+1)
bars2 = [23,
24,
25] Cursor on line 1 would run the whole def. Is it too much to assume that the user would want to run that much code if they are on these lines? Some problems:
Are these safe assumptions? How many more corner cases would there be? @CAM-Gerlach Do you know what RStudio does? @ccordoba12 said:
Are you saying we use IPython to filter non-runnable code as a solution or to use this on corner cases? |
Rstudio runs anything that's a continuation of the current line, forward or backward. However, in R, you explicitly need braces ( So, for example, putting the cursor on the first or last line of the following would run the full if statement, while putting it on the center line would just run the if (TRUE) {
print("test")
} However, if we had the following, putting the cursor on the if (TRUE)
{
print("test")
} With if/else, it will run both if you put your cursor on any line with a brace (and you must put the if (FALSE) {
print("True")
} else {
print("False")
}
This is the key motivating use case for this feature both IMO and as presented in Rstudio when it was introduced; the ability to run indented levels is decidedly secondary. The most common use case where users need something like this is after entering or modifying a multi-line statement, in which case they want to run the whole thing. If we did it by indent level, running a continuation would only work if the cursor is on the first line, which is a decided minority of cases.
This is for the Editor, not the Console, so I'm not sure what you talking about with "add a continuation prompt". Furthermore, it is not necessarily a robust solution to finding the boundaries of a continuation statement, if that's what you're suggesting. Consider: spam = 42 * (
2 + 2
)
|
I guess one simple thing would be to look at bracket matching. If the line you are on has an unmatched bracket move down until you find the match. I guess this is what the python parser does. This isn't that general but it is really easy to do and would capture most use cases. |
Have we thought about using the standard library |
I'm saying that we should use what IPython already has to decide when a line is a complete Python statement or not. Specifically, IPython has the following machinery: https://ipython.readthedocs.io/en/stable/api/generated/IPython.core.inputsplitter.html Although that's deprecated, it's still compatible with Python 2 and 3. So we can use this method: before trying to execute a a line with I guess there would arise some complications along the way, but that's the main idea. |
But what if IPython 8 removes it, breaking Spyder? If we're going to do this, at the very least, we should check for
Which it will in cases like the above where the line itself happens to be a complete statement, but that is part of continuations. Ultimately, its issues here stem from being designed to parse line-by-line, sequential Python input, rather than pre-written Python code in a file, where the originally selected line may be at any point in the statement the user wants to evaluate.,
This is not really a useful solution to the problem presented here, either as framed by the original reporter, @bcolsen , myself or as in Rstudio. The only scenario where it is of any help is if the user happens to be on the first line of a continuation (which, as I describe above, is a minority of instances) and then sequentially presses the Instead, what the requested feature here is when Other than writing our own logic to parse what is part of a continuation line, unless we already have something suitable, we could do something like the following (which should properly handle all of the cases discussed):
Notes:
|
Feature request
In RStudio, given a function with arguments spread out over multiple lines, e.g.:
The use of "run current line" (using shortcut or menu) is less literal in RStudio than in Spyder, as the entire function is run rather than just isolated lines, returning the desired data frame. This works in a number of cases (loops, functions, dplyr/tidyr pipelines, etc.) and can be quite a time-saver, since you don't have to worry about highlighting longish blocks of code to run them.
Similar functionality is possible in Spyder using cells, but I think cells are a better tool for segmenting code according to logical steps in the coding/analysis process, and not so much for isolating individual functions and loops. Would it be possible to implement this feature in some future Spyder version?
The text was updated successfully, but these errors were encountered: