jpalardy / vim-slime

A vim plugin to give you some slime. (Emacs)
http://technotales.wordpress.com/2007/10/03/like-slime-for-vim/
MIT License
1.88k stars 227 forks source link

commented out code in python can cause an IndentationError #447

Open andreyrogojin opened 1 week ago

andreyrogojin commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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 SlimeOverrideEscape 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 commented 1 week ago

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)