Repository navigation
Expand file tree
/
Copy pathchat_context.lua
More file actions
854 lines (762 loc) · 26.1 KB
/
Copy pathchat_context.lua
File metadata and controls
854 lines (762 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
local base_context = require('opencode.context.base_context')
local util = require('opencode.util')
local state = require('opencode.state')
local Promise = require('opencode.promise')
local M = {}
---@type OpencodeContext
M.context = {
mentioned_files = {},
selections = {},
review_comments = {},
mentioned_subagents = {},
current_file = nil,
cursor_data = nil,
linter_errors = nil,
}
local cleared_selections = {}
local cleared_selections_context = nil
local set_file_sent_timestamps
local next_review_id = 0
---@param comment OpencodeContextReviewComment
function M.add_review_comment(comment)
next_review_id = next_review_id + 1
comment.id = next_review_id
local comments = M.context.review_comments or {}
for _, existing in ipairs(comments) do
if
existing.file == comment.file
and existing.side == comment.side
and existing.start_line == comment.start_line
and existing.end_line == comment.end_line
and existing.session_id == comment.session_id
and existing.from == comment.from
and existing.to == comment.to
then
existing.comment = comment.comment
state.context.set_context_updated_at(vim.uv.now())
return existing
end
end
comments[#comments + 1] = comment
M.context.review_comments = comments
state.context.set_context_updated_at(vim.uv.now())
-- Resolve file drift after the editor has returned to its event loop.
vim.defer_fn(function()
for _, stored in ipairs(M.get_review_comments()) do
if stored == comment then
comment.resolution = require('opencode.review_anchor').current(comment)
state.context.set_context_updated_at(vim.uv.now())
return
end
end
end, 25)
return comment
end
function M.get_review_comments(file)
local comments = M.context.review_comments or {}
if not file then
return comments
end
return vim.tbl_filter(function(comment)
return comment.file == file
end, comments)
end
---@param path string
---@return boolean
function M.has_review_comment_for_file(path)
local current_session_id = state.active_session and state.active_session.id
local current_path = vim.fn.resolve(vim.fn.fnamemodify(path, ':p'))
for _, comment in ipairs(M.get_review_comments()) do
if
(not current_session_id or comment.session_id == current_session_id)
and vim.fn.resolve(vim.fn.fnamemodify(comment.file, ':p')) == current_path
then
return true
end
end
return false
end
function M.update_review_comment(id, text)
for _, comment in ipairs(M.get_review_comments()) do
if comment.id == id then
comment.comment = text
state.context.set_context_updated_at(vim.uv.now())
return
end
end
end
function M.remove_review_comment(id)
for index, comment in ipairs(M.get_review_comments()) do
if comment.id == id then
table.remove(M.context.review_comments, index)
state.context.set_context_updated_at(vim.uv.now())
return
end
end
end
function M.clear_review_comments()
M.context.review_comments = {}
state.context.set_context_updated_at(vim.uv.now())
end
---@param comment OpencodeContextReviewComment
local function capture_review_comment(comment, hint)
local resolution = require('opencode.review_anchor').current(comment, hint)
comment.resolution = resolution
local current = resolution.status ~= 'exact'
and {
status = resolution.status,
lines = resolution.start_line and (resolution.start_line .. '-' .. resolution.end_line) or nil,
code = resolution.current_code,
}
or nil
return {
comment = comment.comment,
side = comment.side,
lines = comment.start_line .. '-' .. comment.end_line,
code = comment.code,
current = current,
}
end
---@param left OpencodeContextSelection|nil
---@param right OpencodeContextSelection|nil
---@return boolean
local function is_same_selection(left, right)
return (
left
and right
and left.file
and right.file
and left.file.path == right.file.path
and left.lines == right.lines
) == true
end
---@param path string
---@param prompt? string
---@return table
local function capture_file(path, prompt)
local rel_path = vim.fn.fnamemodify(path, ':~:.')
local filename = vim.fn.fnamemodify(path, ':t')
if filename:match('^pasted_image_') then
rel_path = filename
end
local mention = '@' .. rel_path
local pos = prompt and prompt:find(mention, 1, true)
local ext = vim.fn.fnamemodify(path, ':e'):lower()
local mime_type = 'text/plain'
if ext == 'png' then
mime_type = 'image/png'
elseif ext == 'jpg' or ext == 'jpeg' then
mime_type = 'image/jpeg'
elseif ext == 'gif' then
mime_type = 'image/gif'
elseif ext == 'webp' then
mime_type = 'image/webp'
end
local file = {
name = rel_path,
media_type = mime_type,
server_uri = vim.uri_from_fname(util.apply_path_map(path)),
}
if pos then
file.mention = { start_byte = pos - 1, end_byte = pos - 1 + #mention }
end
return file
end
---@param selection OpencodeContextSelection
---@return table
local function capture_selection(selection)
local lang = util.get_markdown_language(selection.file and selection.file.name or '')
return {
text = vim.json.encode({
context_type = 'selection',
file = selection.file,
content = string.format('`````%s\n%s\n`````', lang, selection.content),
lines = selection.lines,
}),
source = { kind = 'selection', file_name = selection.file and selection.file.name, range = selection.lines },
}
end
---@param diagnostics OpencodeDiagnostic[]
---@param range? { start_line: integer, end_line: integer }|nil
---@return table
local function capture_diagnostics(diagnostics, range)
local diag_list = {}
for _, diag in ipairs(diagnostics) do
if not range or (diag.lnum >= range.start_line and diag.lnum <= range.end_line) then
local short_msg = diag.message:gsub('%s+', ' '):gsub('^%s', ''):gsub('%s$', '')
table.insert(
diag_list,
{ msg = short_msg, severity = diag.severity, pos = 'l' .. diag.lnum + 1 .. ':c' .. diag.col + 1 }
)
end
end
return {
text = vim.json.encode({ context_type = 'diagnostics', content = diag_list }),
source = { kind = 'diagnostics' },
}
end
---@param cursor_data table
---@param get_current_buf fun(): integer|nil Function to get current buffer
---@return table
local function capture_cursor_data(cursor_data, get_current_buf)
local buf = (get_current_buf() or 0) --[[@as integer]]
local lang = util.get_markdown_language(vim.api.nvim_buf_get_name(buf))
return {
text = vim.json.encode({
context_type = 'cursor-data',
line = cursor_data.line,
column = cursor_data.column,
line_content = string.format('`````%s\n%s\n`````', lang, cursor_data.line_content),
lines_before = cursor_data.lines_before,
lines_after = cursor_data.lines_after,
}),
source = { kind = 'cursor' },
}
end
---@param agent string
---@param prompt string
---@return table
local function capture_agent(agent, prompt)
local mention = '@' .. agent
local pos = prompt:find(mention)
local result = { name = agent }
if pos then
result.mention = { start_byte = pos - 1, end_byte = pos - 1 + #mention }
end
return result
end
---@param buf integer
---@return table
local function capture_buffer(buf)
local file = vim.api.nvim_buf_get_name(buf)
local rel_path = vim.fn.fnamemodify(file, ':~:.')
return {
text = table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), '\n'),
source = { kind = 'buffer', file_name = rel_path },
}
end
---@param diff_text string
---@return table
local function capture_git_diff(diff_text)
return {
text = diff_text,
source = { kind = 'git_diff' },
}
end
-- Global context management functions
function M.add_selection(selection)
cleared_selections = {}
cleared_selections_context = nil
-- Ensure selections is always a table
if not M.context.selections then
M.context.selections = {}
end
-- prevent duplicate selection (same exact line range) from being added
for _, sel in ipairs(M.context.selections) do
if sel.file.path == selection.file.path and sel.lines == selection.lines then
return
end
end
table.insert(M.context.selections, selection)
state.context.set_context_updated_at(vim.uv.now())
end
function M.remove_selection(selection)
if not M.context.selections then
M.context.selections = {}
return
end
for i, sel in ipairs(M.context.selections) do
if sel.file.path == selection.file.path and sel.lines == selection.lines then
table.remove(M.context.selections, i)
break
end
end
state.context.set_context_updated_at(vim.uv.now())
end
function M.clear_selections()
M.context.selections = {}
M.context.review_comments = {}
state.context.set_context_updated_at(vim.uv.now())
end
function M.add_file(file)
file = vim.fn.fnamemodify(file, ':p')
if not M.context.mentioned_files then
M.context.mentioned_files = {}
end
if not vim.tbl_contains(M.context.mentioned_files, file) then
table.insert(M.context.mentioned_files, file)
end
state.context.set_context_updated_at(vim.uv.now())
end
function M.remove_file(file)
if not M.context.mentioned_files then
M.context.mentioned_files = {}
return
end
file = vim.fn.fnamemodify(file, ':p')
for i, f in ipairs(M.context.mentioned_files) do
if f == file then
table.remove(M.context.mentioned_files, i)
break
end
end
state.context.set_context_updated_at(vim.uv.now())
end
function M.clear_files()
M.context.mentioned_files = {}
state.context.set_context_updated_at(vim.uv.now())
end
function M.add_subagent(subagent)
-- Ensure mentioned_subagents is always a table
if not M.context.mentioned_subagents then
M.context.mentioned_subagents = {}
end
if not vim.tbl_contains(M.context.mentioned_subagents, subagent) then
table.insert(M.context.mentioned_subagents, subagent)
end
state.context.set_context_updated_at(vim.uv.now())
end
function M.remove_subagent(subagent)
if not M.context.mentioned_subagents then
M.context.mentioned_subagents = {}
return
end
for i, a in ipairs(M.context.mentioned_subagents) do
if a == subagent then
table.remove(M.context.mentioned_subagents, i)
break
end
end
state.context.set_context_updated_at(vim.uv.now())
end
function M.clear_subagents()
M.context.mentioned_subagents = {}
state.context.set_context_updated_at(vim.uv.now())
end
---@param selections? OpencodeContextSelection[]
function M.unload_attachments(selections)
cleared_selections = vim.deepcopy(M.context.selections or {})
for _, selection in ipairs(selections or {}) do
local already_cleared = false
for _, cleared_selection in ipairs(cleared_selections) do
if is_same_selection(selection, cleared_selection) then
already_cleared = true
break
end
end
if not already_cleared then
table.insert(cleared_selections, vim.deepcopy(selection))
end
end
cleared_selections_context = M.context
M.context.mentioned_files = {}
M.context.selections = {}
state.context.set_context_updated_at(vim.uv.now())
end
---@param sent OpencodeContext
---@param target? OpencodeContext
function M.consume_attachments(sent, target)
target = target or M.context
local function remove_values(current, consumed)
local result = {}
for _, value in ipairs(current or {}) do
if not vim.tbl_contains(consumed or {}, value) then
result[#result + 1] = value
end
end
return result
end
if target == M.context then
cleared_selections = vim.deepcopy(sent.selections or {})
cleared_selections_context = M.context
end
target.mentioned_files = remove_values(target.mentioned_files, sent.mentioned_files)
target.mentioned_subagents = remove_values(target.mentioned_subagents, sent.mentioned_subagents)
local remaining = {}
for _, selection in ipairs(target.selections or {}) do
local consumed = false
for _, sent_selection in ipairs(sent.selections or {}) do
consumed = consumed or is_same_selection(selection, sent_selection)
end
if not consumed then
remaining[#remaining + 1] = selection
end
end
target.selections = remaining
target.review_comments = vim.tbl_filter(function(comment)
for _, sent_comment in ipairs(sent.review_comments or {}) do
if comment.id == sent_comment.id then
return false
end
end
return true
end, target.review_comments or {})
if is_same_selection({ file = target.current_file, lines = '' }, { file = sent.current_file, lines = '' }) then
set_file_sent_timestamps(target.current_file)
end
if target == M.context then
state.context.set_context_updated_at(vim.uv.now())
end
end
function M.get_mentioned_files()
return M.context.mentioned_files or {}
end
function M.get_selections()
return M.context.selections or {}
end
function M.get_mentioned_subagents()
return M.context.mentioned_subagents or {}
end
-- Chat-context-aware get_diagnostics that considers stored selections
---@param buf integer
---@param context_config? OpencodeContextConfig
---@param range? { start_line: integer, end_line: integer }
---@return OpencodeDiagnostic[]|nil
function M.get_diagnostics(buf, context_config, range)
-- Use explicit range if provided
if range then
return base_context.get_diagnostics(buf, context_config, range)
end
if M.context.selections and #M.context.selections > 0 then
local selection_ranges = {}
for _, sel in ipairs(M.context.selections) do
if sel.lines then
-- Handle both formats: "1, 5" and "1-5"
local start_line, end_line = sel.lines:match('(%d+)[,%-]%s*(%d+)')
if not start_line then
-- Single line case like "5, 5" or just "5"
start_line = sel.lines:match('(%d+)')
end_line = start_line
end
if start_line then
local start_num = tonumber(start_line)
local end_num = tonumber(end_line)
if start_num and end_num then
-- Convert to 0-based
local selection_range = {
start_line = start_num - 1,
end_line = end_num - 1,
}
table.insert(selection_ranges, selection_range)
end
end
end
end
if #selection_ranges > 0 then
return base_context.get_diagnostics(buf, context_config, selection_ranges)
end
end
return base_context.get_diagnostics(buf, context_config, nil)
end
---@param current_file table|nil
---@return boolean, boolean -- should_update, is_different_file
function M.should_update_current_file(current_file)
if not M.context.current_file then
return current_file ~= nil, false
end
if not current_file then
return true, false
end
-- Different file name means update needed
if M.context.current_file.name ~= current_file.name then
return true, true
end
-- Same file, check modification time
local file_path = current_file.path
if not file_path or vim.fn.filereadable(file_path) ~= 1 then
return false, false
end
local stat = vim.uv.fs_stat(file_path)
if not (stat and stat.mtime and stat.mtime.sec) then
return false, false
end
local file_mtime_sec = stat.mtime.sec --[[@as number]]
local last_sent_mtime = M.context.current_file.sent_at_mtime or 0
return file_mtime_sec > last_sent_mtime, false
end
-- Load function that populates the global context state
-- This is the core loading logic that was originally in the main context module
function M.load()
local changed = false
for _, comment in ipairs(M.get_review_comments()) do
local resolution = require('opencode.review_anchor').current(comment)
changed = changed or not vim.deep_equal(comment.resolution, resolution)
comment.resolution = resolution
end
if changed then
state.context.set_context_updated_at(vim.uv.now())
end
local selections_cleared_by_send = {}
if cleared_selections_context == M.context then
selections_cleared_by_send = cleared_selections
else
cleared_selections = {}
cleared_selections_context = nil
end
cleared_selections = {}
cleared_selections_context = nil
if not state.active_session and not state.is_opening then
return
end
local buf, win = base_context.get_current_buf()
if not buf or not win then
return
end
local prev_current_file = vim.deepcopy(M.context.current_file)
local prev_cursor_data = vim.deepcopy(M.context.cursor_data)
local prev_linter_errors = vim.deepcopy(M.context.linter_errors)
local current_file = base_context.get_current_file(buf)
local cursor_data = base_context.get_current_cursor_data(buf, win)
local should_update_file = M.should_update_current_file(current_file)
if should_update_file then
M.context.current_file = current_file
if M.context.current_file then
M.context.current_file.sent_at = nil
M.context.current_file.sent_at_mtime = nil
end
end
M.context.cursor_data = cursor_data
M.context.linter_errors = M.get_diagnostics(buf, nil, nil)
if
not vim.deep_equal(prev_current_file, M.context.current_file)
or not vim.deep_equal(prev_cursor_data, M.context.cursor_data)
or not vim.deep_equal(prev_linter_errors, M.context.linter_errors)
then
state.context.set_context_updated_at(vim.uv.now())
end
-- Handle current selection
if base_context.is_context_enabled('selection') then
local current_selection = base_context.get_current_selection()
if current_selection then
local selection_file = base_context.get_current_file_for_selection(buf)
if selection_file then
local selection = base_context.new_selection(selection_file, current_selection.text, current_selection.lines)
local was_cleared = false
for _, cleared_selection in ipairs(selections_cleared_by_send) do
if is_same_selection(selection, cleared_selection) then
was_cleared = true
break
end
end
if not was_cleared then
M.add_selection(selection)
end
end
end
end
end
---@param current_file table
set_file_sent_timestamps = function(current_file)
if not current_file then
return
end
current_file.sent_at = vim.uv.now()
local stat = vim.uv.fs_stat(current_file.path)
if stat and stat.mtime and stat.mtime.sec then
current_file.sent_at_mtime = stat.mtime.sec
end
end
---@class OpencodeAutomaticContextPayload
---@field key string
---@field part table
---@field present boolean
---@field cleared table
---Return automatic payload parts changed since the previous accepted submission.
---@param payloads OpencodeAutomaticContextPayload[]
---@param previous_context? OpencodeContext
---@param submission_context? OpencodeContext
---@return table[]
function M.delta_context(payloads, previous_context, submission_context)
local delta = {}
if not submission_context then
for _, payload in ipairs(payloads) do
if payload.present then
delta[#delta + 1] = payload.part
end
end
return delta
end
local previous = previous_context and previous_context.automatic_context or {}
submission_context.automatic_context = {}
for _, payload in ipairs(payloads) do
local fingerprint = vim.fn.sha256(payload.part.text)
local previous_fingerprint = previous[payload.key]
submission_context.automatic_context[payload.key] = fingerprint
if previous_fingerprint ~= fingerprint then
if payload.present then
delta[#delta + 1] = payload.part
elseif previous_fingerprint ~= nil then
delta[#delta + 1] = payload.cleared
end
end
end
return delta
end
--- Capture the protocol-independent input for one submission.
---@param prompt string The user's instruction/prompt
---@param opts? { range?: { start: integer, stop: integer }, context_config?: OpencodeContextConfig, previous_context?: OpencodeContext, submission_context?: OpencodeContext }
---@return table
M.format_message = Promise.async(function(prompt, opts)
opts = opts or {}
local context_config = opts.context_config
local previous_context = opts.previous_context
local submission_context = opts.submission_context
local buf, win = base_context.get_current_buf()
local range = opts.range
local captured = { text = prompt, context = {}, files = {}, agents = {} }
---@type OpencodeAutomaticContextPayload[]
local automatic_context = {}
for _, file_path in ipairs(M.context.mentioned_files or {}) do
captured.files[#captured.files + 1] = capture_file(file_path, prompt)
end
for _, agent in ipairs(M.context.mentioned_subagents or {}) do
captured.agents[#captured.agents + 1] = capture_agent(agent, prompt)
end
if base_context.is_context_enabled('review_comments', context_config) then
local by_file, order, diffs_by_range = {}, {}, {}
for _, comment in ipairs(M.get_review_comments()) do
if not state.active_session or comment.session_id == state.active_session.id then
local hint
local connection = state.opencode_server
local end_message = comment.to or comment.from
if
end_message
and connection
and connection.protocol == 'v2'
and state.active_session
and state.active_session.id == comment.session_id
then
if diffs_by_range[end_message] == nil then
local ok, files = pcall(function()
return connection.operations
.diff_session(connection, comment.session_id, end_message, nil, util.apply_reverse_path_map)
:await()
end)
diffs_by_range[end_message] = ok and files or false
end
for _, file in ipairs(diffs_by_range[end_message] or {}) do
if file.file == comment.file then
hint = require('opencode.review_anchor').translate(comment.anchor_side_line, file.patch)
break
end
end
end
local rel_path = vim.fn.fnamemodify(comment.file, ':~:.')
if not by_file[rel_path] then
by_file[rel_path] = {
context_type = 'review-comment',
file = rel_path,
comments = {},
note = 'Session diff snapshot: locate code by content, not line number. Check changed code before acting.',
}
order[#order + 1] = rel_path
end
local group = by_file[rel_path]
group.comments[#group.comments + 1] = capture_review_comment(comment, hint)
end
end
for _, file in ipairs(order) do
captured.context[#captured.context + 1] = {
text = vim.json.encode(by_file[file]),
source = { kind = 'review_comment', file_name = file },
}
end
end
if not buf then
return captured
end
local selections = {}
if base_context.is_context_enabled('selection', context_config) then
if range and range.start and range.stop then
local file = base_context.get_current_file_for_selection(buf)
if file then
local selection = base_context.new_selection(
file,
table.concat(
vim.api.nvim_buf_get_lines(buf, math.floor(range.start) - 1, math.floor(range.stop), false),
'\n'
),
string.format('%d-%d', math.floor(range.start), math.floor(range.stop))
)
table.insert(selections, selection)
end
end
local current_selection = base_context.get_current_selection(context_config)
if current_selection then
local file = base_context.get_current_file_for_selection(buf)
if file then
local selection = base_context.new_selection(file, current_selection.text, current_selection.lines)
table.insert(selections, selection)
end
end
for _, sel in ipairs(M.context.selections or {}) do
table.insert(selections, sel)
end
end
local current_file_selected = M.context.current_file
and base_context.is_context_enabled('review_comments', context_config)
and M.has_review_comment_for_file(M.context.current_file.path)
or false
for _, selection in ipairs(selections) do
if M.context.current_file and selection.file and selection.file.path == M.context.current_file.path then
current_file_selected = true
break
end
end
if
base_context.is_context_enabled('current_file', context_config)
and M.context.current_file
and not M.context.current_file.sent_at
and not current_file_selected
then
captured.files[#captured.files + 1] = capture_file(M.context.current_file.path)
end
for _, selection in ipairs(selections) do
captured.context[#captured.context + 1] = capture_selection(selection)
end
if base_context.is_context_enabled('buffer', context_config) then
local buffer = capture_buffer(buf)
automatic_context[#automatic_context + 1] = { key = 'buffer', part = buffer, present = true, cleared = buffer }
end
local diag_range = nil
if range then
diag_range = { start_line = math.floor(range.start) - 1, end_line = math.floor(range.stop) - 1 }
end
local diagnostics = M.get_diagnostics(buf, context_config, diag_range)
if diagnostics then
local diagnostic_context = capture_diagnostics(diagnostics, diag_range)
automatic_context[#automatic_context + 1] = {
key = 'diagnostics',
part = diagnostic_context,
present = #diagnostics > 0,
cleared = diagnostic_context,
}
end
if base_context.is_context_enabled('cursor_data', context_config) then
local cursor_data = base_context.get_current_cursor_data(buf, win, context_config)
if cursor_data then
local cursor_context = capture_cursor_data(cursor_data, function()
return buf
end)
automatic_context[#automatic_context + 1] = {
key = 'cursor_data',
part = cursor_context,
present = true,
cleared = cursor_context,
}
end
end
if base_context.is_context_enabled('git_diff', context_config) then
local diff_text = base_context.get_git_diff(context_config):await()
local git_diff = capture_git_diff(diff_text or '')
automatic_context[#automatic_context + 1] = {
key = 'git_diff',
part = git_diff,
present = diff_text ~= nil and diff_text ~= '',
cleared = capture_git_diff('No staged changes.'),
}
end
vim.list_extend(captured.context, M.delta_context(automatic_context, previous_context, submission_context))
return captured
end)
return M