glacambre / firenvim

Embed Neovim in Chrome, Firefox & others.
GNU General Public License v3.0
4.57k stars 144 forks source link

Expand textarea as more lines are typed #1619

Closed girishji closed 1 month ago

girishji commented 1 month ago

When I type inside the code box of a notebook, such as on colab.research.google.com, the height of the text area dynamically adjusts, expanding as more lines are entered. This is quite convenient, as the box starts small (just one line) and grows to accommodate additional lines.

However, when I start using Firenvim within the box, it maintains a fixed height regardless of the number of new lines added. This requires manual resizing by dragging the box with the mouse. It would be great if Firenvim could automatically adjust the height of the HTML element as more text is entered, similar to the default behavior of the notebook.

I'm not sure if this is technically feasible, but I wanted to suggest it as a potential improvement.

glacambre commented 1 month ago

Hi, does this do what you want?

local max_height = 20
local id = vim.api.nvim_create_augroup("ExpandLinesOnTextChanged", { clear = true })
vim.api.nvim_create_autocmd({"TextChanged", "TextChangedI"}, {
    group = id,
    callback = function(ev)
      local height = vim.api.nvim_win_text_height(0, {}).all
      if height > vim.o.lines and height < max_height then
        vim.o.lines = height
      end
    end
  })
glacambre commented 1 month ago

Huh, that's actually super nice to use, I wonder if this should be the default...

girishji commented 1 month ago

Thanks for quick response!

It almost works. This is on Colab (colab.google). The viewport shifts up by one line when the cursor is on lines 2, 4, 8, 16, etc. I fixed it by moving the last line.

        callback = function(ev)
            local height = vim.api.nvim_win_text_height(0, {}).all
            if height > vim.o.lines and height < max_height then
                vim.o.lines = height
                vim.cmd("norm! zb")
            end
        end

The width does not match the width of text box though. neovim box is too wide.

(edited)

girishji commented 1 month ago
Screenshot 2024-06-08 at 2 11 14 PM
glacambre commented 1 month ago

I don't think I'll be able to do anything about that, I'd recommend reducing the size of the frame with :set width=60 if it's a problem :)

girishji commented 1 month ago

No problem, it is good enough. There is no Vim option width though. textwidth has no effect on window itself.

Thanks :)

glacambre commented 1 month ago

Ah, yeah, sorry, that's :set columns=60 :)

girishji commented 1 month ago

Yep, that works!

girishji commented 1 month ago

Leaving this info for anyone interested:

Setting columns from BufRead does not work. Something else resets it back later. You can defer the execution using vim.defer_fn() with say 1 millisec delay to let Vim's typehead do other things. Or, use WinScrolled event to set columns=120.