Vigemus / iron.nvim

Interactive Repl Over Neovim
BSD 3-Clause "New" or "Revised" License
975 stars 81 forks source link

How do I use alternate definitions for a ft? #177

Open cmcaine opened 3 years ago

cmcaine commented 3 years ago

Say I've configured iron with two repl definitions for a ft, how do I start a REPL that uses the non-preferred definition?

Example config:

local iron = require('iron')

iron.core.add_repl_definitions {
  julia = {
    julia = {
      command = {"julia", "--project", "-t", "auto"}
    },
    julia17 = {
      command = {"julia-17", "--project", "-t", "auto"}
    }
  },
}

iron.core.set_config {
  preferred = {
    julia = "julia",
  }
}
hkupty commented 3 years ago

Hi @cmcaine.

The idea of the definitions is that it'll try to get the first matching binary if there's no preference. There's a top-level function for starting a repl by its name, so you could: :lua require("iron").core.repl_by_name("julia17", "julia") for example.

Another approach would be to use something like impromptu.nvim and select the repl choice you want on the go:

local iron = require("iron")
local impromptu = require("impromptu")

_G.spawn_repl = function(ft)
  if ft == nil then
    local cb = vim.api.nvim_get_current_buf()
    ft = vim.api.nvim_buf_get_option(cb, 'filetype')
  end
  local opts = {}
  local defs = iron.core.list_definitions_for_ft(ft)

  for _, kv in ipairs(defs) do
    local descr
    if type(kv[2].command) == "table" then
      descr = table.concat(kv[2].command, " ")
    else
      descr = kv[2].command
    end

    opts[kv[1]] = {
      description = descr,
      name = kv[1]
    }
  end

  impromptu.ask{
    question = "Select repl definition for filetype " .. ft,
    options = opts,
    handler = function(session, opt)
      iron.core.repl_by_name(opt.name, ft)
      return true
    end
  }
end

I hope this helps.

When I get the time to invest in iron again, I'll make sure to merge #174 and, after that, make those things more obvious as well as improve documentation.

Best regards, Henry John Kupty

gwerbin commented 2 years ago

I currently have this in my config, which seems to work well:

function! IronRepl2(ft = '', repl_name = '') abort
  let l:ft = a:ft == '' ? nvim_buf_get_option('filetype') : a:ft
  if a:repl_name == ''
    call luaeval('require("iron").core.repl_for(_A)', l:ft)
  else
    call luaeval('require("iron").core.repl_by_name(_A[1], _A[2])', [a:repl_name, l:ft])
  endif
endfunction

command! -nargs=* IronRepl2 call IronRepl2(<f-args>)

That said, repl_by_name doesn't seem to be in the docs anywhere. Is that intentional? Or is that part of some intended future cleanup work?

hkupty commented 2 years ago

Hi @gwerbin

Yep, unfortunately I couldn't get to devote the amount of time iron needs for the past year or so... I have to make sure those functions are properly documented... I don't think I'll remove that one, but it is likely that some API is going to change.