Repository navigation
Expand file tree
/
Copy pathcontext.lua
More file actions
195 lines (159 loc) · 5.05 KB
/
Copy pathcontext.lua
File metadata and controls
195 lines (159 loc) · 5.05 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
-- Gathers editor context
local template = require('opencode.template')
local util = require('opencode.util')
local config = require('opencode.config')
local M = {}
M.context = {
-- current file
current_file = nil,
cursor_data = nil,
-- attachments
mentioned_files = nil,
selections = nil,
linter_errors = nil,
}
function M.unload_attachments()
M.context.mentioned_files = nil
M.context.selections = nil
M.context.linter_errors = nil
end
function M.load()
if util.is_current_buf_a_file() then
local current_file = M.get_current_file()
local cursor_data = M.get_current_cursor_data()
M.context.current_file = current_file
M.context.cursor_data = cursor_data
M.context.linter_errors = M.check_linter_errors()
end
local current_selection = M.get_current_selection()
if current_selection then
local selection = M.new_selection(M.context.current_file, current_selection.text, current_selection.lines)
M.add_selection(selection)
end
end
function M.check_linter_errors()
local diagnostic_conf = config.get('context') and config.get('context').diagnostics
if not diagnostic_conf then
return nil
end
local severity_levels = {}
if diagnostic_conf.error then
table.insert(severity_levels, vim.diagnostic.severity.ERROR)
end
if diagnostic_conf.warning then
table.insert(severity_levels, vim.diagnostic.severity.WARN)
end
if diagnostic_conf.info then
table.insert(severity_levels, vim.diagnostic.severity.INFO)
end
local diagnostics = vim.diagnostic.get(0, { severity = severity_levels })
if #diagnostics == 0 then
return nil
end
local message = 'Found ' .. #diagnostics .. ' error' .. (#diagnostics > 1 and 's' or '') .. ':'
for i, diagnostic in ipairs(diagnostics) do
local line_number = diagnostic.lnum + 1 -- Convert to 1-based line numbers
local short_message = diagnostic.message:gsub('%s+', ' '):gsub('^%s', ''):gsub('%s$', '')
message = message .. '\n Line ' .. line_number .. ': ' .. short_message
end
return message
end
function M.new_selection(file, content, lines)
return {
file = file,
content = util.indent_code_block(content),
lines = lines,
}
end
function M.add_selection(selection)
if not M.context.selections then
M.context.selections = {}
end
table.insert(M.context.selections, selection)
end
function M.add_file(file)
if not M.context.mentioned_files then
M.context.mentioned_files = {}
end
if vim.fn.filereadable(file) ~= 1 then
vim.notify('File not added to context. Could not read.')
return
end
if not vim.tbl_contains(M.context.mentioned_files, file) then
table.insert(M.context.mentioned_files, file)
end
end
function M.delta_context()
local context = vim.deepcopy(M.context)
local last_context = require('opencode.state').last_sent_context
if not last_context then
return context
end
-- no need to send file context again
if context.current_file and context.current_file.name == last_context and last_context.current_file.name then
context.current_file = nil
end
return context
end
function M.get_current_file()
local file = vim.fn.expand('%:p')
if not file or file == '' or vim.fn.filereadable(file) ~= 1 then
return nil
end
return {
path = file,
name = vim.fn.fnamemodify(file, ':t'),
extension = vim.fn.fnamemodify(file, ':e'),
}
end
function M.get_current_cursor_data()
if not (config.get('context') and config.get('context').cursor_data) then
return nil
end
local cursor_pos = vim.fn.getcurpos()
local cursor_content = vim.trim(vim.api.nvim_get_current_line())
return { line = cursor_pos[2], col = cursor_pos[3], line_content = cursor_content }
end
function M.get_current_selection()
-- Return nil if not in a visual mode
if not vim.fn.mode():match('[vV\022]') then
return nil
end
-- Save current position and register state
local current_pos = vim.fn.getpos('.')
local old_reg = vim.fn.getreg('x')
local old_regtype = vim.fn.getregtype('x')
-- Capture selection text and position
vim.cmd('normal! "xy')
local text = vim.fn.getreg('x')
-- Get line numbers
vim.cmd('normal! `<')
local start_line = vim.fn.line('.')
vim.cmd('normal! `>')
local end_line = vim.fn.line('.')
-- Restore state
vim.fn.setreg('x', old_reg, old_regtype)
vim.cmd('normal! gv')
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('<Esc>', true, false, true), 'nx', true)
vim.fn.setpos('.', current_pos)
return {
text = text and text:match('[^%s]') and text or nil,
lines = start_line .. ', ' .. end_line,
}
end
function M.format_message(prompt)
local context = nil
context = M.delta_context()
context.prompt = prompt
return template.render_template(context)
end
function M.extract_from_message(text)
local current_file = template.extract_tag('current-file', text)
local context = {
prompt = template.extract_tag('user-query', text) or text,
selected_text = template.extract_tag('manually-added-selection', text),
current_file = current_file and current_file:match('Path: (.+)') or nil,
}
return context
end
return M