Skip to content

Commit a4a6a6a

Browse files
committed
fix(context): keep context state and send behavior in sync
1 parent fbad9da commit a4a6a6a

5 files changed

Lines changed: 33 additions & 6 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,13 @@ You can quiclkly see the current context items in the context bar at the top of
728728
<img src="https://i.imgur.com/vGgu6br.png" alt="Opencode.nvim context bar" width="90%" />
729729
</div>
730730

731+
For `Current file`, the color indicates whether it will be sent with the next prompt:
732+
733+
- Regular highlight: file is pending and will be included.
734+
- Dimmed/gray highlight: file was already sent and has not changed, so it will be skipped (delta behavior).
735+
736+
If the file content changes, it becomes pending again and will be sent on the next prompt.
737+
731738
### Context Items Completion
732739

733740
You can quickly reference available context items by typing `#` in the input window. This will show a completion menu with all available context items:

lua/opencode/context.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ function M.clear_subagents()
161161
end
162162

163163
function M.unload_attachments()
164-
ChatContext.clear_files()
165-
ChatContext.clear_selections()
164+
ChatContext.unload_attachments()
166165
end
167166

168167
function M.load()

lua/opencode/context/chat_context.lua

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ end
195195

196196
function M.clear_selections()
197197
M.context.selections = {}
198+
state.context_updated_at = vim.uv.now()
198199
end
199200

200201
function M.add_file(file)
@@ -230,6 +231,7 @@ end
230231

231232
function M.clear_files()
232233
M.context.mentioned_files = {}
234+
state.context_updated_at = vim.uv.now()
233235
end
234236

235237
function M.add_subagent(subagent)
@@ -261,11 +263,13 @@ end
261263

262264
function M.clear_subagents()
263265
M.context.mentioned_subagents = {}
266+
state.context_updated_at = vim.uv.now()
264267
end
265268

266269
function M.unload_attachments()
267270
M.context.mentioned_files = {}
268271
M.context.selections = {}
272+
state.context_updated_at = vim.uv.now()
269273
end
270274

271275
function M.get_mentioned_files()
@@ -336,7 +340,7 @@ function M.should_update_current_file(current_file)
336340
end
337341

338342
if not current_file then
339-
return false, false
343+
return true, false
340344
end
341345

342346
-- Different file name means update needed
@@ -373,6 +377,10 @@ function M.load()
373377
return
374378
end
375379

380+
local prev_current_file = vim.deepcopy(M.context.current_file)
381+
local prev_cursor_data = vim.deepcopy(M.context.cursor_data)
382+
local prev_linter_errors = vim.deepcopy(M.context.linter_errors)
383+
376384
local current_file = base_context.get_current_file(buf)
377385
local cursor_data = base_context.get_current_cursor_data(buf, win)
378386

@@ -389,6 +397,14 @@ function M.load()
389397
M.context.cursor_data = cursor_data
390398
M.context.linter_errors = M.get_diagnostics(buf, nil, nil)
391399

400+
if
401+
not vim.deep_equal(prev_current_file, M.context.current_file)
402+
or not vim.deep_equal(prev_cursor_data, M.context.cursor_data)
403+
or not vim.deep_equal(prev_linter_errors, M.context.linter_errors)
404+
then
405+
state.context_updated_at = vim.uv.now()
406+
end
407+
392408
-- Handle current selection
393409
if base_context.is_context_enabled('selection') then
394410
local current_selection = base_context.get_current_selection()
@@ -484,7 +500,11 @@ M.format_message = Promise.async(function(prompt, opts)
484500
return { parts = parts }
485501
end
486502

487-
if M.context.current_file and not M.context.current_file.sent_at then
503+
if
504+
base_context.is_context_enabled('current_file', context_config)
505+
and M.context.current_file
506+
and not M.context.current_file.sent_at
507+
then
488508
table.insert(parts, format_file_part(M.context.current_file.path))
489509
set_file_sent_timestamps(M.context.current_file)
490510
end

lua/opencode/ui/context_bar.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ end
171171

172172
function M.setup()
173173
state.subscribe(
174-
{ 'current_context_config', 'current_code_buf', 'opencode_focused', 'context_updated_at', 'user_message_count' },
174+
{ 'current_context_config', 'current_code_buf', 'is_opencode_focused', 'context_updated_at', 'user_message_count' },
175175
function()
176176
M.render()
177177
end

tests/unit/context_bar_spec.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,8 @@ describe('opencode.ui.context_bar', function()
299299
assert.is_true(subscription_called)
300300
assert.is_table(captured_keys)
301301

302-
local expected_keys = { 'current_context_config', 'current_code_buf', 'opencode_focused', 'context_updated_at' }
302+
local expected_keys =
303+
{ 'current_context_config', 'current_code_buf', 'is_opencode_focused', 'context_updated_at' }
303304
for _, expected_key in ipairs(expected_keys) do
304305
local found = false
305306
for _, key in ipairs(captured_keys) do

0 commit comments

Comments
 (0)