iron.nvim icon indicating copy to clipboard operation
iron.nvim copied to clipboard

Extra spaces in front processing? (not a bug but a feature add?)

Open yelled1 opened this issue 6 years ago • 5 comments

It's awesome but when compared to Emacs or Atom, I miss the ability to take code chunk with extra spaces in front. (visual selection)

import os
print("hello")
os.getenv('PATH')

can be sent directly to repl vs visual selection, but not:

    import os
    print("hello")
    os.getenv('PATH')

with extra spaces in front. Some simple processing to remove extra spaces in front would be great. Wishful but not a show stopper. Thanks u much for making ssh work so much easier!

yelled1 avatar Jun 23 '18 05:06 yelled1

Here's a dumb/simple solution. If 1st line contains whitespace as the 1st character then just add if True:\n to the visual selection. Doing this manually for now. Not sure how to implment. Thanks,

yelled1 avatar Jun 24 '18 16:06 yelled1

Thanks for the issue. I'm really glad this helps you out.

I need to invest some time in iron, which was not possible during the previous months. This is a known limitation and I'll prioritize this (as well as multi-line, which is still not perfect IMO) in the future.

I was hoping I could get the lua port done this year and I'll try to get this right from beginning in lua if possible. Otherwise, I'll just try to fix the python verison before migrating.

Cheers, Henry

hkupty avatar Jun 25 '18 11:06 hkupty

I hope you can! This is a really something I would gladly spend time with you to help any way I can. Here's my hacky way of dealing with it.

let g:iron_map_defaults=0 
" My issues with Iron
"' <1> Add if True:\n in visual selection execution?
"' <2> after visual exec scroll to end of selection
"' <3> Do scroll to the end of Repl after execution to Repl
" So typical of a Emacs person:
noremap <C-x>o <C-W>w
inoremap <C-x>o <C-O><C-W>w
tnoremap <C-x>o <c-\><c-n><c-w><c-w>

" Launching IronRepl w/ (custom) noremap <C-x>o <C-W>w as it goes into terminal mode & <ESC> do not work & needs <c-\><c-n><c-w><c-w>
map <silent> <leader>tt :IronRepl<CR><C-x>o
augroup ironmapping
    autocmd!
    " quick ,, will goto end of Repl & end of last visual block & move down
    autocmd FileType python map <silent> <leader>, <Esc>`>a<ESC><C-w>jG<C-w>pj0 
    autocmd Filetype python vmap <buffer> <localleader>\ $<Plug>(iron-send-motion)
    autocmd Filetype python nmap <localleader>\ :call IronSend(getline('.'))<CR><ESC><C-w>jG<C-w>pj0
    autocmd Filetype python imap <localleader>\ <Esc>:call IronSend(getline('.'))<CR>
augroup END

It's not pretty but works.

Issue is testing for white spaces <Tab> or <Space> presence & if True then,

  1. either adding if True\n to the top
  2. or cutting out the 1st lines white space & repeating the same cut to the entire block

and adding hacky <Esc>`>a<ESC><C-w>jG<C-w>pj0 as the post process Obviously, #2 is preferred.

Also nice add if possible (but not expecting): Also it would be nice to be able to hit <leader>\ to the top of the code block & dump the entire section into the Repl as visual block:

def xyz():
     print("hello")
     if (x > y): return True
     else: return False

or

     if (x > y): 
           return True
     else: 
           return False

etc...

I attempted to copy scala.py to python.py but did not go far. Let me know if u could use help. Not a good coder, but certainly not a horrible one. Unfortunately, I do not prog in Lua, only python. Thank u much,

yelled1 avatar Jun 25 '18 11:06 yelled1

Oh, this might make the whole job easier.

The secret sauce to this is textwrap.dedent. The textwrap module is in the standard library, so there’s really no excuse not to use it. What it does is it “removes any common leading whitespace from every line in text”. With dedent, we can indent the whole multiline string according to the current scope, so that it looks like a Pythonic code block.

https://amir.rachum.com/blog/2018/06/23/python-multiline-idioms/

yelled1 avatar Jun 27 '18 14:06 yelled1

I've also noticed, and I think this might be correlated, that after sending an indented line to the repl, somehow the following visual_send is shifted in the selection. Example in julia code:

a = "Hello"
    b = println(a, ", World!")

Sending these two lines to the repl works as expected. If I now visually select the b variable, b = p is send to the repl. It seems like the selection is shifted for the indentation. So if I have indent of 4 spaces, they are added to visual selection column count. image

PS: This only started to happen after the recent update so might even be worth an own issue :thinking:

timoleistner avatar May 25 '22 11:05 timoleistner