Make chat windows read only

This commit is contained in:
Jonas Widen 2025-03-16 20:53:50 +01:00
parent e7b9bc1e2c
commit f8afeef891

View File

@ -133,7 +133,10 @@ function M.create_window()
end
function M.update_content(content, is_new_chat, is_thinking)
-- Temporarily disable readonly and enable modifiable
vim.api.nvim_buf_set_option(state.bufnr, 'readonly', false)
vim.api.nvim_buf_set_option(state.bufnr, 'modifiable', true)
local lines = vim.split(content, "\n")
if is_thinking then
@ -146,14 +149,12 @@ function M.update_content(content, is_new_chat, is_thinking)
-- Add all messages from history
for i, msg in ipairs(state.conversation_history) do
if i > 1 then -- Add separator before messages (except first)
if i > 1 then
table.insert(display_lines, "━━━━━━━━━━━━━━━━━━━━━━━━━━")
end
-- Keep track of where the current query starts
if i == #state.conversation_history - 1 then -- User's query is second-to-last message
if i == #state.conversation_history - 1 then
current_query_line = #display_lines + 1
end
-- Add prefix based on role
local prefix = msg.role == "user" and "User: " or "Assistant: "
local first_line = true
local msg_lines = vim.split(msg.content, "\n")
@ -167,16 +168,16 @@ function M.update_content(content, is_new_chat, is_thinking)
end
end
-- Set the buffer content
if #display_lines > 0 then
vim.api.nvim_buf_set_lines(state.bufnr, 0, -1, false, display_lines)
-- Scroll to the current query
vim.api.nvim_win_set_cursor(state.winnr, {current_query_line, 0})
vim.cmd('normal! zt')
end
end
-- Re-enable readonly and disable modifiable
vim.api.nvim_buf_set_option(state.bufnr, 'modifiable', false)
vim.api.nvim_buf_set_option(state.bufnr, 'readonly', true)
vim.cmd('wincmd p')
end