Skip to content

Commit e79d26c

Browse files
committed
add scrolling to the latest user message
1 parent 3bff0d9 commit e79d26c

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

lua/magenta/anthropic.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ end
3838
---Get the API key from config or environment
3939
---@return string|nil, string? error
4040
function Client:get_api_key()
41-
local log = require("magenta.log")
41+
local log = require("magenta.log").log
4242
log.debug("Getting API key")
4343
if self.config.api_key then
4444
log.debug("Using API key from config")
@@ -175,7 +175,7 @@ function Client:request(payload, actions, opts)
175175

176176
local job = self.opts.post(request_opts)
177177

178-
local log = require("magenta.log")
178+
local log = require("magenta.log").log
179179
if job and job.args then
180180
log.debug("Request command: %s", vim.inspect(job.args))
181181
end

lua/magenta/init.lua

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local log = require('magenta.log')
1+
local log = require('magenta.log').log
22

33
local M = {}
44

@@ -92,17 +92,22 @@ function M.setup(opts)
9292
-- end, {})
9393
end
9494

95-
-- Function to append text to the main area
96-
---@param text string Text to append
97-
local function append_to_main(text)
95+
---@param opts {text: string, scrolltop: boolean?} Options for appending text
96+
local function append_to_main(opts)
9897
if not main_area or not main_area.bufnr then
9998
log.error("Cannot append to main area - not initialized")
10099
return
101100
end
102101

103-
local lines = vim.split(text, '\n', {})
102+
local lines = vim.split(opts.text, '\n', {})
104103
if #lines == 0 then return end
105104

105+
local top_line = vim.api.nvim_buf_line_count(main_area.bufnr) + 1;
106+
107+
if opts.scrolltop then
108+
require('magenta.util.scroll_buffer').scroll_buffer(main_area.bufnr, top_line)
109+
end
110+
106111
local last_line = vim.api.nvim_buf_get_lines(main_area.bufnr, -2, -1, false)[1] or ""
107112

108113
vim.api.nvim_buf_set_option(main_area.bufnr, 'modifiable', true)
@@ -137,7 +142,7 @@ function M.send_message()
137142
vim.api.nvim_buf_set_lines(input_area.bufnr, 0, -1, false, { "" })
138143

139144
-- Add user message to main area
140-
append_to_main("\nUser: " .. message .. "\n\nAssistant: ")
145+
append_to_main { text = "\nUser: " .. message .. "\n\nAssistant: ", scrolltop = true }
141146

142147
-- Send to Anthropic with streaming
143148
anthropic_client:request({
@@ -147,12 +152,12 @@ function M.send_message()
147152
callback = function(err, text)
148153
if err then
149154
log.error("Anthropic API error:", err)
150-
append_to_main("\nError: " .. err .. "\n")
155+
append_to_main { text = "\nError: " .. err .. "\n" }
151156
return
152157
end
153158

154159
log.debug("Received stream text:", text)
155-
append_to_main(text)
160+
append_to_main({ text = text })
156161
end,
157162
done = function()
158163
log.debug("Request completed")
@@ -181,8 +186,9 @@ function M.show_sidebar()
181186
relative = "editor",
182187
size = M.config.sidebar_width,
183188
win_options = {
184-
wrap = true, -- Changed to true for better text display
189+
wrap = true,
185190
number = false,
191+
relativenumber = false,
186192
cursorline = true,
187193
},
188194
})
@@ -193,6 +199,7 @@ function M.show_sidebar()
193199
win_options = {
194200
wrap = true,
195201
number = false,
202+
relativenumber = false,
196203
},
197204
})
198205

lua/magenta/log.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
local log = require('plenary.log').new({
1+
local M = {}
2+
3+
M.log = require('plenary.log').new({
24
plugin = 'magenta',
35
level = "trace",
46
})
57

6-
return log
8+
return M

lua/magenta/util/scroll_buffer.lua

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local M = {}
2+
3+
---@param bufnr integer Buffer number
4+
---@param line integer Line number to scroll to (1-based)
5+
function M.scroll_buffer(bufnr, line)
6+
-- Save current window
7+
local current_win = vim.api.nvim_get_current_win()
8+
9+
-- Find window containing our buffer
10+
local target_win = nil
11+
for _, win in ipairs(vim.api.nvim_list_wins()) do
12+
if vim.api.nvim_win_get_buf(win) == bufnr then
13+
target_win = win
14+
break
15+
end
16+
end
17+
18+
if target_win then
19+
-- Switch to window
20+
vim.api.nvim_set_current_win(target_win)
21+
-- Move cursor and scroll
22+
vim.api.nvim_win_set_cursor(target_win, { line, 0 })
23+
vim.cmd('normal! zt')
24+
-- Switch back
25+
vim.api.nvim_set_current_win(current_win)
26+
end
27+
end
28+
29+
return M

0 commit comments

Comments
 (0)