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

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

Open
yelled1 opened this issue Jun 23, 2018 · 6 comments
Open

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

yelled1 opened this issue Jun 23, 2018 · 6 comments

Comments

@yelled1
Copy link

yelled1 commented Jun 23, 2018

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
Copy link
Author

yelled1 commented Jun 24, 2018

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,

@hkupty
Copy link
Collaborator

hkupty commented Jun 25, 2018

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

@yelled1
Copy link
Author

yelled1 commented Jun 25, 2018

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 or 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 `>ajGpj0 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
Copy link
Author

yelled1 commented Jun 27, 2018

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/

@hkupty hkupty self-assigned this Dec 1, 2018
@hkupty hkupty added the up-next label May 18, 2022
@hkupty hkupty added this to the 3.2 - Features milestone May 18, 2022
@timoleistner
Copy link

timoleistner commented May 25, 2022

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 🤔

@patnr
Copy link

patnr commented Dec 19, 2024

Here's a solution that strips the indent:

repl_definition = {
    -- python = require("iron.fts.python").ipython,
    python = {
        -- Remove indent, ref Vigemus/iron.nvim/issues/77
        format = function(lines, extras)
            -- Find common (i.e. minimal, non-zero) indent width
            local nIndent = 999
            for _, line in ipairs(lines) do
                local indent = string.match(line, "^%s*")
                if indent then
                    nIndent = math.min(nIndent, #indent)
                end
            end
            -- Strip indent
            for i, line in ipairs(lines) do
                if not string.match(line, "^%s*$") then
                    lines[i] = string.sub(line, nIndent + 1)
                end
            end
            -- Apply default `format` (rm empty lines, add CR, make windows-compatible)
            lines = require("iron.fts.common").bracketed_paste(lines)
            -- lines = require("iron.fts.common").bracketed_paste_python(lines) -- does not work for me
            return lines
        end,
        -- Prefer ipython, fallback to python:
        command = function(_meta)
            -- meta.current_bufnr
            if vim.fn.executable("ipython") == 1 then
                return require("iron.fts.python").ipython.command
            else
                return require("iron.fts.python").python.command
            end
        end,
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants