vim-sendtowindow icon indicating copy to clipboard operation
vim-sendtowindow copied to clipboard

Python & Whitespace

Open marshallm94 opened this issue 4 years ago • 3 comments

Have you tried this plugin with Python?

I am able to send function/class definitions to the Python interpreter provided there isn't any whitespace in the function/class definition, however if there is an empty line, the interpreter throws an IndentationError.

For example, sending the following works (the two lines of whitespace after the function are needed to signal the end of the function):

# works
def add(x, y):
    return x + y


However, sending this does not work:

# doesn't work
def add(x, y):
    
    return x + y


Comments are fine:

# works
def add(x, y):
    # this isn't a problem
    return x + y


The Question

I'm guessing this is a nuance of the Python interpreter and not your plugin, however do you think there is a way to work around this with your plugin/some vim script?

I really like the simplicity of your plugin and can "make do" by changing the way I write code, but I sometimes incorporate whitespace as an "idea separator" within function/classes, and would like to not change that if possible.

Any thoughts/ideas appreciated.

marshallm94 avatar Dec 22 '20 12:12 marshallm94

I have used this script with python a few times but don't remember running into this issue.

Suggestions

Few currently working suggestions:

  1. if you would indent the empty line (by adding 4 spaces) then it seems to work (note the spaces before empty line):
# works this way
def add(x, y):
    
    return x + y
  1. you can use ip for sending the text, and skip the line manually. For example this would work, using default maps:
<space>jip
j
.
  1. craft a custom quick-map that works for most of your situations and use fine-grained commands only when needed:
# Make Q send "inner paragraph" to the repl, and adjust the cursor by moving it one line down
nnoremap Q :execute "normal vip \<Plug>SendDownV"<cr>j

But you would have to think what makes more sense in your situation.

Possible additions to the plugin

As for possible additions/changes that came to mind:

  1. Make the cursor positioning ignore new lines. In such a case you would be able to send text with (i.e.) <space>jip - it will send "inner paragraph" without appended empty line, and the cursor will skip the empty line and go straight to the next occurrence of text.

  2. Add a setting like set g:sendtowindow_consume_emptylines that would remove any empty lines from being sent.

But these would make other examples fail. As you will not be able to send empty lines, and python seems to need them for marking the end of those def statements... I am not in love with these solutions, would have to think more about them if we decide going that way.

Behaviour of other plugins?

Maybe you have experience with other, more elaborate, REPL plugins and how they are dealing with this?

karoliskoncevicius avatar Dec 23 '20 15:12 karoliskoncevicius

I don't have experience with other plugins, no - I like the simplicity of yours so when I was searching for one, I just stuck with yours. I'm by no means a vim settings expert (barely fluent), but do you think there is some combination of tabstop,expandtab, shiftwidth that might help?

marshallm94 avatar Jan 07 '21 12:01 marshallm94

upvote

Awannaphasch2016 avatar Feb 11 '21 07:02 Awannaphasch2016