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

commented out code in python can cause an IndentationError #447

Open
andreyrogojin opened this issue Oct 11, 2024 · 3 comments
Open

commented out code in python can cause an IndentationError #447

andreyrogojin opened this issue Oct 11, 2024 · 3 comments

Comments

@andreyrogojin
Copy link

Example:

for i in range(10):
    print(i, end=' ')
#     print(i * 2, end=' ')
    print(i * i)

Maybe it's better to consider comments as empty lines?
To do this, in ftplugin/python/slime.vim: _EscapeText_python() can replace

let empty_lines_pat = '\(^\|\n\)\zs\(\s*\n\+\)\+'

with

let empty_lines_pat = '\(^\|\n\)\zs\(\s*\(#[^\n]*\)*\n\+\)\+'
@jpalardy
Copy link
Owner

Hi @andreyrogojin

Does the indent on the commented line matter? (would it work if it were indented like the rest of the body?)

Also, there's an "escape hatch" for custom escapes: https://github.com/jpalardy/vim-slime/blob/main/assets/doc/advanced.md#how-to-override-language-transformations

Let me know

@andreyrogojin
Copy link
Author

I didn't quite understand the question. If you meant, does an IndentationError occur if the code is commented out like this:

for i in range(10):
    print(i, end=' ')
    # print(i * 2, end=' ')
    print(i * i)

Then no, it does not arise.
IndentationError appears due to the addition of a newline in

function! _EscapeText_python(text)
    ...
    return substitute(dedented_lines, add_eol_pat, "\n", "g")

The newline is added focusing on the indentation, not paying attention to whether it is a code or a comment. If the indentation of the comment does not match the rest of the text, a line feed will be added, which will cause an IndentationError when sending further text.
But comments with '#' in the first column are often found during debugging, it's more convenient this way.

If your question is whether the proposed change to the empty_lines_pat template will work with different indents, then yes, such a template will delete all lines containing only a comment, regardless of the indentation. I think there is no point in sending comments to the REPL. The interpreter ignores them anyway, and the person reads them in the editor.

As for the SlimeOverride_Escape Text_#{language} function, I think this path is intended to be customized to the user's "taste". Tastes can be individual. But, I suppose, no one wants the results of calling a program to differ depending on whether it was executed in the interpreter or sent via vim-slime

@jpalardy
Copy link
Owner

Hi @andreyrogojin

Since I'm not a Python user myself, I'm hesitant to change how its substitutions are made. Especially given how long this code has been around (which hints that it works for most people 🤔 )

Screen Shot 2024-10-14 at 11 38 27

In vim, I've been using nerdcommenter which toggles comments at the proper indent level. In neovim, the gcc mapping (built-in) will comment the current line at the proper indent too. I understand — these are workarounds rather than fixes.

If you want to take this on, I would recommend you @ some of the original authors to iterate over solutions. (although it's possible that many people have moved on)

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

No branches or pull requests

2 participants