-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathcontext_bar.lua
More file actions
195 lines (162 loc) · 5.47 KB
/
context_bar.lua
File metadata and controls
195 lines (162 loc) · 5.47 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
local M = {}
local context = require('opencode.context')
local icons = require('opencode.ui.icons')
local state = require('opencode.state')
local config = require('opencode.config')
local prompt_guard_indicator = require('opencode.ui.prompt_guard_indicator')
local function get_current_file_info(ctx)
local current_file = ctx.current_file
if not current_file then
return nil
end
return {
name = current_file.name,
path = current_file.path,
}
end
local function get_attached_files_count(ctx)
local mentioned_files = ctx.mentioned_files
return mentioned_files and #mentioned_files or 0
end
local function has_selection(ctx)
local selections = ctx.selections
return selections and #selections > 0
end
local function has_cursor_data(ctx)
local cursor_data = ctx.cursor_data
return cursor_data ~= nil
end
local function has_diagnostics(ctx)
local diagnostics = ctx.linter_errors
return diagnostics and #diagnostics > 0
end
local function format_cursor_data(ctx)
local cursor_data = ctx.cursor_data
if not cursor_data then
return ''
end
return string.format('L:%d ', cursor_data.line, cursor_data.col)
end
local function create_winbar_segments()
local ctx = context.get_context()
local segments = {}
local current_file = get_current_file_info(ctx)
if context.is_context_enabled('current_file') and current_file then
local highlight = 'OpencodeContextCurrentFile'
if ctx.current_file and ctx.current_file.sent_at then
highlight = 'OpencodeContextCurrentFileNotUpdated'
end
table.insert(segments, {
icon = icons.get('attached_file'),
text = current_file.name,
highlight = highlight,
})
end
local agents = ctx.mentioned_subagents or {}
if context.is_context_enabled('agents') and #agents > 0 then
table.insert(segments, {
icon = icons.get('agent'),
text = '(' .. #agents .. ')',
highlight = 'OpencodeContextAgent',
})
end
local attached_count = get_attached_files_count(ctx)
if context.is_context_enabled('files') and attached_count > 0 then
table.insert(segments, {
icon = icons.get('file'),
text = '(' .. attached_count .. ')',
highlight = 'OpencodeContextFile',
})
end
if context.is_context_enabled('selection') and has_selection(ctx) then
table.insert(segments, {
icon = icons.get('selection'),
text = '(' .. #ctx.selections .. ')',
highlight = 'OpencodeContextSelection',
})
end
if context.is_context_enabled('cursor_data') and has_cursor_data(ctx) then
table.insert(segments, {
icon = icons.get('cursor_data'),
text = format_cursor_data(ctx),
highlight = 'OpencodeContextSelection',
})
end
if context.is_context_enabled('diagnostics') and has_diagnostics(ctx) then
local counts = {}
local diagnostics = ctx.linter_errors
local severity_types = {
[vim.diagnostic.severity.ERROR] = 'error',
[vim.diagnostic.severity.WARN] = 'warning',
[vim.diagnostic.severity.INFO] = 'info',
}
for _, diag in ipairs(diagnostics or {}) do
local type_name = severity_types[diag.severity] or 'error'
counts[type_name] = (counts[type_name] or 0) + 1
end
local filter_icon = config.context.diagnostics.only_closest and icons.get('filter') or ''
for _, type_name in pairs(severity_types) do
local count = counts[type_name]
if count and count > 0 then
table.insert(segments, {
icon = icons.get(type_name),
text = '(' .. count .. filter_icon .. ')',
highlight = 'OpencodeContext' .. type_name:gsub('^%l', string.upper),
})
end
end
end
return segments
end
local function format_winbar_text(segments)
local right_align = '%='
if #segments == 0 then
return right_align
end
local parts = {}
for i, segment in ipairs(segments) do
local segment_text = segment.icon .. segment.text
local highlight = segment.highlight and ('%#' .. segment.highlight .. '#') or ''
table.insert(parts, highlight .. segment_text .. '%*')
if i < #segments then
table.insert(parts, ' %#OpencodeContextBar#|%* ')
end
end
local show_guard_indicator = prompt_guard_indicator.is_denied()
local left = show_guard_indicator and prompt_guard_indicator.get_formatted() or ''
return left .. right_align .. table.concat(parts, '')
end
local function update_winbar_highlights(win_id)
local current = vim.api.nvim_get_option_value('winhighlight', { win = win_id })
local parts = vim.split(current, ',')
parts = vim.tbl_filter(function(part)
return not part:match('^WinBar:') and not part:match('^WinBarNC:')
end, parts)
if not vim.tbl_contains(parts, 'Normal:OpencodeNormal') then
table.insert(parts, 'Normal:OpencodeNormal')
end
table.insert(parts, 'WinBar:OpencodeContextBar')
table.insert(parts, 'WinBarNC:OpencodeContextBar')
vim.api.nvim_set_option_value('winhighlight', table.concat(parts, ','), { win = win_id })
end
function M.setup()
state.subscribe(
{ 'current_context_config', 'current_code_buf', 'is_opencode_focused', 'context_updated_at', 'user_message_count' },
function()
M.render()
end
)
end
function M.render(windows)
vim.schedule(function()
windows = windows or state.windows
local win = windows and windows.input_win
if not (win and vim.api.nvim_win_is_valid(win)) then
return
end
local segments = create_winbar_segments()
vim.wo[win].winbar = format_winbar_text(segments)
update_winbar_highlights(win)
end)
end
return M