Repository navigation
Expand file tree
/
Copy pathnavigation.lua
More file actions
341 lines (290 loc) · 9.34 KB
/
Copy pathnavigation.lua
File metadata and controls
341 lines (290 loc) · 9.34 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
local M = {}
local state = require('opencode.state')
local config = require('opencode.config')
local renderer = require('opencode.ui.renderer')
local output_window = require('opencode.ui.output_window')
function M.goto_message_by_id(message_id)
require('opencode.ui.ui').focus_output()
local windows = state.windows or {}
local win = windows.output_win
local buf = windows.output_buf
if not win or not buf then
return
end
local rendered_msg = renderer.get_rendered_message(message_id)
if not rendered_msg or not rendered_msg.line_start then
return
end
vim.api.nvim_win_set_cursor(win, { rendered_msg.line_start + 1, 0 })
end
function M.goto_next_message()
require('opencode.ui.ui').focus_output()
local windows = state.windows or {}
local win = windows.output_win
local buf = windows.output_buf
if not win or not buf then
return
end
local current_line = vim.api.nvim_win_get_cursor(win)[1]
local next_message = renderer.get_next_rendered_message(current_line)
if next_message and next_message.line_start then
vim.api.nvim_win_set_cursor(win, { next_message.line_start + 1, 0 })
return
end
local line_count = vim.api.nvim_buf_line_count(buf)
vim.api.nvim_win_set_cursor(win, { line_count, 0 })
end
function M.goto_prev_message()
require('opencode.ui.ui').focus_output()
local windows = state.windows or {}
local win = windows.output_win
local buf = windows.output_buf
if not win or not buf then
return
end
local current_line = vim.api.nvim_win_get_cursor(win)[1]
local previous_message = renderer.get_prev_rendered_message(current_line)
if previous_message and previous_message.line_start then
vim.api.nvim_win_set_cursor(win, { previous_message.line_start + 1, 0 })
return
end
vim.api.nvim_win_set_cursor(win, { 1, 0 })
end
function M.goto_next_user_message()
require('opencode.ui.ui').focus_output()
local windows = state.windows or {}
local win = windows.output_win
local buf = windows.output_buf
if not win or not buf then
return
end
-- Mirror `gg` in output_window.setup_keymaps: under lazy render the target
-- message may not yet have a line_start, so force a full render first.
renderer.load_all_messages()
local current_line = vim.api.nvim_win_get_cursor(win)[1]
local next_message = renderer.get_next_user_message(current_line)
if next_message and next_message.line_start then
vim.api.nvim_win_set_cursor(win, { next_message.line_start + 1, 0 })
return
end
vim.notify('No next user message', vim.log.levels.INFO)
end
function M.goto_prev_user_message()
require('opencode.ui.ui').focus_output()
local windows = state.windows or {}
local win = windows.output_win
local buf = windows.output_buf
if not win or not buf then
return
end
renderer.load_all_messages()
local current_line = vim.api.nvim_win_get_cursor(win)[1]
local previous_message = renderer.get_prev_user_message(current_line)
if previous_message and previous_message.line_start then
vim.api.nvim_win_set_cursor(win, { previous_message.line_start + 1, 0 })
return
end
vim.notify('No previous user message', vim.log.levels.INFO)
end
---@param raw string
local function resolve_path(raw)
if vim.uv.fs_stat(raw) then
return raw
end
local absolute = vim.fn.fnamemodify(raw, ':p')
if vim.uv.fs_stat(absolute) then
return absolute
end
local found = vim.fn.findfile(raw, '.;')
if found ~= '' then
return found
end
end
local function parse_path_location(raw)
if raw:match('^%a[%w+.-]*://') and not raw:match('^file://') then
return nil
end
local path = raw:gsub('^file://', '')
local line, col
local p, l, c = path:match('^(.-):(%d+):(%d+)$')
if p then
path, line, col = p, tonumber(l), tonumber(c)
else
p, l = path:match('^(.-):(%d+)$')
if p then
path, line = p, tonumber(l)
end
end
if path == '' then
return nil
end
return { path = path, line = line, col = col }
end
local function contains_col(start_pos, end_pos, col)
return col >= start_pos - 1 and col <= end_pos - 1
end
local function add_file_candidate(candidates, line, pattern, path_capture_index)
path_capture_index = path_capture_index or 2
local captures = { line:match(pattern) }
while #captures > 0 do
local start_pos = captures[1]
local end_pos = captures[#captures] - 1
local raw = captures[path_capture_index]
table.insert(candidates, {
start_pos = start_pos,
end_pos = end_pos,
target = parse_path_location(raw),
})
local next_start = end_pos + 2
captures = { line:match(pattern, next_start) }
end
end
local function file_target_at_col(line, col)
local candidates = {}
add_file_candidate(candidates, line, '()%[%`([^`]+)%`%]%([^%)]+%)()')
add_file_candidate(candidates, line, '()%`([^`\n]+%.%w+:?%d*:?%d*)%`()')
add_file_candidate(candidates, line, '()file://([%S]+%.%w+:?%d*:?%d*)()')
add_file_candidate(candidates, line, '()%*%*.-%*%*%s+%`([^`]+)%`()')
add_file_candidate(candidates, line, '()([%w_./%-]+/[%w_./%-]*%.%w+:?%d*:?%d*)()')
add_file_candidate(candidates, line, '()([%w_%-]+%.%w+:?%d*:?%d*)()')
for _, candidate in ipairs(candidates) do
if candidate.target and contains_col(candidate.start_pos, candidate.end_pos, col) and resolve_path(candidate.target.path) then
return candidate.target
end
end
end
local function first_file_target(line)
local max_col = math.max(#line - 1, 0)
for col = 0, max_col do
local target = file_target_at_col(line, col)
if target then
return target
end
end
end
local function diff_line_number(buf, line_num)
local ns = output_window.namespace
local extmarks = vim.api.nvim_buf_get_extmarks(buf, ns, { line_num - 1, 0 }, { line_num - 1, -1 }, { details = true })
for _, extmark in ipairs(extmarks) do
local details = extmark[4]
local virt_text = details and details.virt_text
if virt_text then
local gutter = virt_text[1] and virt_text[1][1]
local sign = virt_text[2] and virt_text[2][1]
if sign == '-' then
return nil
end
if sign == '+' or sign == ' ' then
return tonumber(vim.trim(gutter or ''))
end
end
end
end
---Resolve file and line number at cursor position in the output buffer.
---@return { path: string, line: number? }?
function M.resolve_file_at_cursor()
local windows = state.windows or {}
local win = windows.output_win
local buf = windows.output_buf
if not win or not buf or not vim.api.nvim_win_is_valid(win) then
return nil
end
local cursor = vim.api.nvim_win_get_cursor(win)
local line_num = cursor[1]
local col = cursor[2]
local line = vim.api.nvim_buf_get_lines(buf, line_num - 1, line_num, false)[1]
if not line then
return nil
end
local target = file_target_at_col(line, col)
if target then
return target
end
local file_path = nil
for i = line_num, 1, -1 do
local l = vim.api.nvim_buf_get_lines(buf, i - 1, i, false)[1]
if l then
local found = first_file_target(l)
if found then
file_path = found.path
break
end
end
end
if not file_path then
return nil
end
local ln = diff_line_number(buf, line_num)
if not ln then
return nil
end
return { path = file_path, line = ln }
end
---Open a file in the current window without triggering BufRead/BufNew autocmds.
---Falls back to :edit if the file isn't loaded in any buffer yet.
---@param path string
local function open_silent(path)
local escaped = vim.fn.fnameescape(path)
if not pcall(vim.cmd, 'buffer ' .. escaped) then
pcall(vim.cmd, 'edit ' .. escaped)
end
end
local function open_at(win, path, line, col)
if not win or not vim.api.nvim_win_is_valid(win) then
return
end
vim.api.nvim_set_current_win(win)
open_silent(path)
if line then
local buf = vim.api.nvim_win_get_buf(win)
local line_count = vim.api.nvim_buf_line_count(buf)
local target_line = math.max(1, math.min(line, line_count))
local target_col = 0
if col then
local target_lines = vim.api.nvim_buf_get_lines(buf, target_line - 1, target_line, false)
local line_text = target_lines[1] or ''
target_col = math.max(0, math.min(col - 1, math.max(#line_text - 1, 0)))
end
pcall(vim.api.nvim_win_set_cursor, win, { target_line, target_col })
vim.cmd('normal! zz')
end
end
local function best_target_win()
local w = state.last_code_win_before_opencode
if w and vim.api.nvim_win_is_valid(w) then
return w
end
local alt = vim.fn.win_getid(vim.fn.winnr('#'))
if alt ~= 0 and vim.api.nvim_win_is_valid(alt) then
return alt
end
end
---@param path string
---@param line? number
---@param col? number
function M.navigate_to_location(path, line, col)
local resolved_path = resolve_path(path)
if not resolved_path then
return
end
local target_win = best_target_win()
local windows = state.windows
if config.ui.position == 'current' and windows and target_win == windows.output_win then
require('opencode.ui.ui').hide_visible_windows(windows)
end
open_at(target_win, resolved_path, line, col)
end
function M.resolve_target_at_cursor()
return M.resolve_file_at_cursor()
end
function M.jump_to_target_at_cursor()
local resolved = M.resolve_target_at_cursor()
if not resolved then
return
end
M.navigate_to_location(resolved.path, resolved.line, resolved.col)
end
function M.jump_to_file_at_cursor()
M.jump_to_target_at_cursor()
end
return M