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

Different REPL per file

Open guymorlan opened this issue 3 years ago • 3 comments

I often have several files open with the same filetype. Currently they share the same REPL. Can iron be configured so that a different REPL is launched per file?

Thanks

guymorlan avatar Jun 06 '22 06:06 guymorlan

Hi, @guymorlan

Iron doesn't allow it "out-of-the-box" but you could extend it to add support if you want to. That's probably not intuitive and there should probably be an "Advanced Config" section, but that's probably for later..

In a TL;DR version, iron has an extensible scope management module. One needs to inform iron how to get and how to set values in a central REPL-reference in-memory db. So the implemented ones are tab_based, path_based and singleton.

If you want to implement your own version, it would be something like this:

local iron = require("iron.core")

local my_scope = {
  set = function(db, filetype, repl_data)
    local key = vim.fn.expand("%")
    db[filetype] = db[filetype] or {} -- Needed for the first time that filetype is used, might be abstracted away in the future
    db[filetype][key] = repl_data
    
    return repl_data -- important to always return the repl data here.
  end,
  get = function(db, filetype)
    local key = vim.fn.expand("%")
    db[filetype] = db[filetype] or {} -- Needed for the first time that filetype is used, might be abstracted away in the future
    return db[filetype][key]
  end
}

iron.setup{
  config = {
    scope = my_scope
  }
}

Note that this is untested.

I plan to revamp this section and I might eventually make it easier to add new scopes - and I would welcome a PR for that if it works - it is just not in my plans to touch this part of iron at the moment.

Hope this helps, let me know how it goes.

Best regards, Henry

hkupty avatar Jun 07 '22 09:06 hkupty

Hi, unfortunately this snippet doesn't work for me:

E5108: Error executing lua: ...local/share/nvim/plugged/iron.nvim/lua/iron/lowlevel.lua:148: attempt to index local 'meta' (a nil value)
stack traceback:
    ...local/share/nvim/plugged/iron.nvim/lua/iron/lowlevel.lua:148: in function 'send_to_repl'
    ...rz/.local/share/nvim/plugged/iron.nvim/lua/iron/core.lua:140: in function <...rz/.local/share/nvim/plugged/iron.nvim/lua/iron/core.lua:136>

It works fine (same as singleton scope) if I don't create the key dynamically:

local my_scope = {
  set = function(db, filetype, repl_data)
    db[filetype] = db[filetype] or {} -- Needed for the first time that filetype is used, might be abstracted away in the future
    db[filetype]['pomidor'] = repl_data
    
    return repl_data -- important to always return the repl data here.
  end,
  get = function(db, filetype)
    db[filetype] = db[filetype] or {} -- Needed for the first time that filetype is used, might be abstracted away in the future
    return db[filetype]['pomidor']
  end
}

I added print statements to get and set functions and it seems that the current buffer is changed to repl buffer prior to calling the set function. Specifically, new_repl.create in core.lua calls ll.create_repl_on_current_window, which calls vim.api.nvim_win_set_buf(0, bufnr). ll.set is called after this, so it sees the repl buffer/file and not the code buffer/file.

I read the code some more but I don't see an easy way ensure that the set function is called in proper context.

gkowzan avatar Oct 22 '22 04:10 gkowzan

I added an ugly solution to my file_scope branch. The current window is set to code window just before calling ll.set, and then it's immediately changed back.

gkowzan avatar Oct 22 '22 15:10 gkowzan