Expand textarea as more lines are typed
Closed this issue · 9 comments
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.
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
})
Huh, that's actually super nice to use, I wonder if this should be the default...
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)
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 :)
No problem, it is good enough. There is no Vim option width
though. textwidth
has no effect on window itself.
Thanks :)
Ah, yeah, sorry, that's :set columns=60
:)
Yep, that works!
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
.