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

Visual selection sent and evaluated line-by-line rather than as a chunk #378

Open
marbris opened this issue May 31, 2024 · 10 comments
Open

Comments

@marbris
Copy link

marbris commented May 31, 2024

When i select a chunk of python code and send it to repl using iron.core.visual_send() it sends each line to the repl and evaluates it before sending the next line. It will send the next line even if an error is thrown along the way and it makes debugging difficult. A few weeks ago it would send and evaluate the selection as a whole.

Edit: I'm using LazyVim on Ubuntu 22.04. Iron.nvim is configured for python with

          repl_definition = {
            python = require("iron.fts.python").ipython,
          }

This changed over the past few weeks. it's probably one of the following updates but i can't tell which one it would be:

    c993d01 refactor: common.bracketed_paste_python is now more consistent across OS (#375) (2 weeks ago)
    0525058 bug fix: unix ipython has issues with lines that only had white spaces in them. (#372) (2 weeks ago)
    7fc2422 send_file and send_until_cursor bug fix for python (#371) (3 weeks ago)
    81733bf fixed python bug were standalone if/else and try/except statements would throw as indentation error (#370) (3 weeks ago)

EDIT:

the issue is that format = iron.fts.common.bracketed_paste_python doesn't work well with ipython. the default was changed from bracketed_paste to bracketed_paste_python in commit #367 . the workaround is to explicitly set the bracketed_paste, shown below. I don't know lua well enough to figure out why it doesn't work.

python = {
    format = require("iron.fts.common").bracketed_paste,
    command = { "ipython", "--no-autoindent" },
},
@rwijtvliet
Copy link

rwijtvliet commented Jun 5, 2024

I don't mean to hijack this thread, but it's probably related:

I also have an issue with visual_send in python. In my case, it's sent in one piece, but the indentation gets messed up. My source code:

for i in range(2):
    print (i)
    print (i+1)

After selecting and visual_send:

In [286]: for i in range(2):
     ...:         print (i)
     ...:             print (i+1)
  Cell In[286], line 3
    print (i+1)
    ^
IndentationError: unexpected indent

If I manually yank and paste these lines into the REPL, there's no issue.

For me, the config is

    repl_definition = {
            python = { command = 'ipython --nosep' },
    }

(Don't hesitate to ask me to open a new issue if it's bothering here)

@marbris
Copy link
Author

marbris commented Jun 6, 2024

@rwijtvliet . I use the command 'ipython --no-autoindent'. its the ipython default in iron. so my config looks like this

repl_definition = {
        python = require("iron.fts.python").ipython,
}

i don't know the difference between --nosep and --no-autoindent but maybe thats your issue.

@rwijtvliet
Copy link

Ah, thank you! That actually cleared up my issue 🙏 FYI, the --nosep option removes the blank lines between subsequent inputs, to save on space.

So the issue you're describing, that visual_send() sends each line separately, I cannot confirm. What I do see is that sending a chunk of code, such as send_until_cursor, does not stop at an error, which is making debugging equally hard.

I'll sit this one out and not pollute the thread again.

@marbris
Copy link
Author

marbris commented Jun 12, 2024

The issue was solved when i reverted back before those updates i listed. Today when i brought it all up to date again, along with other packages, the issue was gone. iron hasn't been updated since then, so the issue must have come from somewhere else. I'm closing the issue.

@marbris marbris closed this as completed Jun 12, 2024
@marbris
Copy link
Author

marbris commented Jun 13, 2024

Actually, my bad. The issue is not closed and i've found what the issue is. iron.fts.common.bracketed_paste_python doesn't work well for ipython. so in commit (#367) when the default format for ipython was changed from bracketed_paste to bracketed_paste_python, something stopped working.

my workaround is to use the following configuration:

python = {
    format = require("iron.fts.common").bracketed_paste,
    command = { "ipython", "--no-autoindent" },
},

i dont know lua well enough to figure out why bracketed_paste_python doesn't work for ipython on linux. So im reopening the issue in case someone want's to figure this out.

@marbris marbris reopened this Jun 13, 2024
@nickeisenberg
Copy link
Contributor

@marbris Hey I am using bracked_paste_python on linux with ipython and have not been having errors. This is my setup

    local repl_definition = {
      python = {
        command = { "ipython", "--no-autoindent" },
        format = python_format
      }
    }

    iron.setup {
      config = {
        scratch_repl = true,
        repl_open_cmd = view.split.vertical.rightbelow("%40"),
        repl_definition = repl_definition
      },
      keymaps = {
        send_line = "<space>sl",
        visual_send = "<space>sp",
        send_paragraph = "<space>sp",
        send_until_cursor = "<space>su",
        send_file = "<space>sf",
        exit = "<space>rq",
        clear = "<space>rc",
        interrupt = "<space><c-c>",
        cr = "<space><cr>",
      },
      highlight = {
        italic = true
      },
      ignore_blank_lines = true,
    }

Can you show me a snippet of code that you are having issues with?

@Jynxzzz
Copy link

Jynxzzz commented Aug 23, 2024

class LSTMForVariableInput(nn.Module):
def init(self, input_size, output_size, hidden_size, num_layers=1):
super().init()

    self.lstm = nn.LSTM(
        input_size=input_size,
        hidden_size=hidden_size,
        num_layers=num_layers,
        batch_first=True,
    )
    self.fc = nn.Linear(in_features=hidden_size, out_features=output_size)

def forward(self, x, seq_length):
    packed_sequence = nn.utils.rnn.pack_padded_sequence(
        x, seq_length, batch_first=True, enforce_sorted=False
    )
    packed_output, (h_n, c_n) = self.lstm(packed_sequence)

    lstm_out, _ = nn.utils.rnn.pad_packed_sequence(packed_output, batch_first=True)
    # only need one last step output
    output = self.fc(lstm_out[:, -1, :])

    return output. 

In Iron.vim
In [72]:

In [72]: class LSTMForVariableInput(nn.Module):
...: def init(self, input_size, output_size, hidden_size, num_layers=1):
...: super().init()
...:

In [73]: self.lstm = nn.LSTM(
...: input_size=input_size,
...: hidden_size=hidden_size,
...: num_layers=num_layers,
...: batch_first=True,
...: )

NameError Traceback (most recent call last)
Cell In[73], line 1
----> 1 self.lstm = nn.LSTM(
2 input_size=input_size,
3 hidden_size=hidden_size,
4 num_layers=num_layers,
5 batch_first=True,
6 )

NameError: name 'self' is not defined

In [74]: self.fc = nn.Linear(in_features=hidden_size, out_features=output_size)

NameError Traceback (most recent call last)
Cell In[74], line 1
----> 1 self.fc = nn.Linear(in_features=hidden_size, out_features=output_size)

NameError: name 'self' is not defined

In [75]:

In [75]: def forward(self, x, seq_length):
...: packed_sequence = nn.utils.rnn.pack_padded_sequence(
...: x, seq_length, batch_first=True, enforce_sorted=False
...: )
...: packed_output, (h_n, c_n) = self.lstm(packed_sequence)
...:

In [76]: lstm_out, _ = nn.utils.rnn.pad_packed_sequence(packed_output, batch_first=True)

NameError Traceback (most recent call last)
Cell In[76], line 1
----> 1 lstm_out, _ = nn.utils.rnn.pad_packed_sequence(packed_output, batch_first=True)

NameError: name 'packed_output' is not defined


my Lua setting :
{
'hkupty/iron.nvim',
config = function()
require('iron.core').setup {
config = {
repl_definition = {
python = {
command = { 'ipython', '--no-autoindent' },
format = python_format,
},
},
repl_open_cmd = require('iron.view').split.horizontal.botright(15),
},
keymaps = {
-- send_motion = 'cc',
visual_send = 'cc',
interrupt = 'cp>',
exit = 'cq',
clear = 'cl',
send_line = 'cc',
send_file = 'cf',
},
highlight = {
italic = true,
},
ignore_blank_lines = true,
}
end,
},
please help. Iron cannot evaluate by class

@nickeisenberg
Copy link
Contributor

@Jynxzzz Sorry I made a mistake in the configuration that I asked you to try. python_format was never defined. Try this

{
  'hkupty/iron.nvim',
  config = function()
    local python_format = require("iron.fts.common").bracketed_paste_python
    require('iron.core').setup {
      config = {
        repl_definition = {
          python = {
            command = { 'ipython', '--no-autoindent' },
            format = python_format,
          },
        },
        repl_open_cmd = require('iron.view').split.horizontal.botright(15),
      },
      keymaps = {
        -- send_motion = 'cc',
        visual_send = 'cc',
        interrupt = 'cp>',
        exit = 'cq',
        clear = 'cl',
        send_line = 'cc',
        send_file = 'cf',
      },
      highlight = {
        italic = true,
      },
      ignore_blank_lines = true,
    }
  end,
}

I just tested this on linux an it worked.

@Jynxzzz
Copy link

Jynxzzz commented Aug 23, 2024

@Jynxzzz Sorry I made a mistake in the configuration that I asked you to try. python_format was never defined. Try this

{
  'hkupty/iron.nvim',
  config = function()
    local python_format = require("iron.fts.common").bracketed_paste_python
    require('iron.core').setup {
      config = {
        repl_definition = {
          python = {
            command = { 'ipython', '--no-autoindent' },
            format = python_format,
          },
        },
        repl_open_cmd = require('iron.view').split.horizontal.botright(15),
      },
      keymaps = {
        -- send_motion = 'cc',
        visual_send = 'cc',
        interrupt = 'cp>',
        exit = 'cq',
        clear = 'cl',
        send_line = 'cc',
        send_file = 'cf',
      },
      highlight = {
        italic = true,
      },
      ignore_blank_lines = true,
    }
  end,
}

I just tested this on linux an it worked.

Man it worked like a magic. Thank you for your insane quick reply and huge help! !

@nickeisenberg
Copy link
Contributor

@Jynxzzz youre welcome!

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

4 participants