Exafunction / codeium.nvim

A native neovim extension for Codeium
MIT License
639 stars 50 forks source link

[feature] Open Codeium Chat in floating nvim window #188

Open erichlf opened 1 month ago

erichlf commented 1 month ago

Instead of opening a new browser window to use codeium chat it would be nice to just open a floating window in nvim.

aliaksandr-trush commented 1 month ago

It was the answer from Codeium team: Hey, we gave this some thought and to be honest we don't think it would be super helpful to provide this interface—I know Neovim users want a native-chat interface to use, but at the pace that we're adding features to our existing browser-based solution, any native interface would struggle to maintain feature parity, and we don't want our users stuck without these new features

erichlf commented 1 month ago

The mentality they had there was that they had to create something rather than to use existing tools, so I can understand that answer in that case. I was more suggesting something like some of the answer provided in the following: https://vi.stackexchange.com/questions/848/how-to-render-html-file

For my use case I wouldn't have access to a full browser, since I am in a docker container.

fortenforge commented 1 month ago

We're definitely down to do something like what's shown in that stackexchange answer—but are those solutions any good? Rendering full-featured HTML as text seems like a very hard problem

erichlf commented 1 month ago

With your question I began to dive into the terminal based web browser hole and found brow.sh. So it seems like what could be done is you could use something like the following. First create a brosh.lua file like so

Terminal = require("toggleterm.terminal").Terminal

M = {}

-- keep track of buffer number for the toggle terminal
local _terminal = nil

-- when the created window detaches set things back to -1
local _on_detach = function()
  _terminal = nil
end

local _on_open = function(term)
  -- ensure that we are not in insert mode
  vim.cmd("stopinsert")
  vim.api.nvim_buf_set_keymap(
    term.bufnr,
    'n',
    '<esc>',
    '<CMD>lua vim.api.nvim_buf_delete(' .. term.bufnr .. ', { force = true } )<CR><CMD>close<CR>',
    { noremap = true, silent = true }
  )
  vim.api.nvim_buf_set_keymap(
    term.bufnr,
    'n',
    'q',
    '<CMD>lua vim.api.nvim_buf_delete(' .. term.bufnr .. ', { force = true } )<CR><CMD>close<CR>',
    { noremap = true, silent = true }
  )
  vim.api.nvim_buf_set_keymap(term.bufnr, 'n', 't', '<CMD>close<CR>', { noremap = true, silent = true })
end

function M.open(url)
  if _terminal ~= nil then
    M.toggle()
    return
  end
  cmd = "brow.sh %s"
  _terminal = Terminal:new {
    cmd = cmd.format(url),
    hidden = false,
    display_name = "codeium chat",
    direction = vim.F.if_nil(config.direction, "float"),
    size = size,
    close_on_exit = true,
    on_open = _on_open,
    auto_scroll = true,
    on_exit = function(_, _, code, _)
      _on_exit(code)
      _on_detach()
    end, -- callback for when process closes
  }

  -- start in insert mode
  _terminal:set_mode(mode.NORMAL)
  -- now execute the command
  _terminal:open()
end

function M.is_open()
  return _terminal ~= nil
end

function M.toggle()
  if _terminal == nil then
    vim.notify("No codeium chat window to toggle.", vim.log.levels.WARN)
    return
  end
  _terminal:toggle()
end

return M

and in api.lua around line 430 change it to the following

browsh = require("codeium.browsh")

-- cross-platform solution to open the web app
local os_info = io.get_system_info()
if vim.fn.executable('brow.sh') == 1 then
    browsh.open(url)
elseif os_info.os == "linux" then
    os.execute("xdg-open '" .. url .. "'")
elseif os_info.os == "macos" then
    os.execute("open '" .. url .. "'")
elseif os_info.os == "windows" then
    os.execute("start " .. url)
else
    notify.error("Unsupported operating system")
end