Skip to content

Commit 601cc82

Browse files
authored
fix(config): add config metatable (#59)
Fixes #58 Makes config access less error prone and more ergonomic
1 parent ca0ff90 commit 601cc82

24 files changed

+53
-59
lines changed

lua/opencode/api.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ function M.focus_input()
300300
end
301301

302302
function M.debug_output()
303-
local config = require('opencode.config').get()
303+
local config = require('opencode.config')
304304
if not config.debug.enabled then
305305
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
306306
return
@@ -310,7 +310,7 @@ function M.debug_output()
310310
end
311311

312312
function M.debug_message()
313-
local config = require('opencode.config').get()
313+
local config = require('opencode.config')
314314
if not config.debug.enabled then
315315
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
316316
return
@@ -320,7 +320,7 @@ function M.debug_message()
320320
end
321321

322322
function M.debug_session()
323-
local config = require('opencode.config').get()
323+
local config = require('opencode.config')
324324
if not config.debug.enabled then
325325
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
326326
return

lua/opencode/config.lua

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,26 +221,19 @@ function M.setup(opts)
221221
end
222222
end
223223

224+
-- vim.notify(vim.inspect(opts))
224225
M.values = vim.tbl_deep_extend('force', M.values, opts --[[@as OpencodeConfig]])
226+
-- vim.notify(vim.inspect(M.values))
225227

226228
update_keymap_prefix(M.values.keymap_prefix, M.defaults.keymap_prefix)
227229
end
228230

229-
function M.get(key)
230-
if key then
231-
return M.values[key]
232-
end
233-
return M.values
234-
end
235-
236231
--- Get the key binding for a specific function in a scope
237232
--- @param scope 'editor'|'input_window'|'output_window'
238233
--- @param function_name string
239234
--- @return string|nil
240235
function M.get_key_for_function(scope, function_name)
241-
local config_data = M.get()
242-
243-
local keymap_config = config_data.keymap and config_data.keymap[scope]
236+
local keymap_config = M.values.keymap and M.values.keymap[scope]
244237
if not keymap_config then
245238
return nil
246239
end
@@ -278,4 +271,15 @@ function M.normalize_keymap(legacy_config, filter_functions)
278271
return converted
279272
end
280273

281-
return M
274+
---@export Config
275+
return setmetatable(M, {
276+
__index = function(_, key)
277+
return M.values[key]
278+
end,
279+
__newindex = function(_, key, value)
280+
M.values[key] = value
281+
end,
282+
__tostring = function(_)
283+
return vim.inspect(M.values)
284+
end,
285+
})

lua/opencode/context.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Gathers editor context
22

33
local util = require('opencode.util')
4-
local config = require('opencode.config').get()
4+
local config = require('opencode.config')
55
local state = require('opencode.state')
66

77
local M = {}

lua/opencode/core.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ local server_job = require('opencode.server_job')
88
local input_window = require('opencode.ui.input_window')
99
local util = require('opencode.util')
1010
local Promise = require('opencode.promise')
11-
local config = require('opencode.config').get()
11+
local config = require('opencode.config')
1212

1313
---@param parent_id string?
1414
function M.select_session(parent_id)

lua/opencode/health.lua

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,34 +111,32 @@ local function check_configuration()
111111
return
112112
end
113113

114-
local values = config.get()
115-
116114
local valid_positions = { 'left', 'right', 'top', 'bottom' }
117-
if not vim.tbl_contains(valid_positions, values.ui.position) then
115+
if not vim.tbl_contains(valid_positions, config.ui.position) then
118116
health.warn(
119-
string.format('Invalid UI position: %s', values.ui.position),
117+
string.format('Invalid UI position: %s', config.ui.position),
120118
{ 'Valid positions: ' .. table.concat(valid_positions, ', ') }
121119
)
122120
else
123-
health.ok(string.format('UI position: %s', values.ui.position))
121+
health.ok(string.format('UI position: %s', config.ui.position))
124122
end
125123

126-
if values.ui.window_width <= 0 or values.ui.window_width > 1 then
124+
if config.ui.window_width <= 0 or config.ui.window_width > 1 then
127125
health.warn(
128-
string.format('Invalid window width: %s', values.ui.window_width),
126+
string.format('Invalid window width: %s', config.ui.window_width),
129127
{ 'Window width should be between 0 and 1 (percentage of screen)' }
130128
)
131129
else
132-
health.ok(string.format('Window width: %s', values.ui.window_width))
130+
health.ok(string.format('Window width: %s', config.ui.window_width))
133131
end
134132

135-
if values.ui.input_height <= 0 or values.ui.input_height > 1 then
133+
if config.ui.input_height <= 0 or config.ui.input_height > 1 then
136134
health.warn(
137-
string.format('Invalid input height: %s', values.ui.input_height),
135+
string.format('Invalid input height: %s', config.ui.input_height),
138136
{ 'Input height should be between 0 and 1 (percentage of screen)' }
139137
)
140138
else
141-
health.ok(string.format('Input height: %s', values.ui.input_height))
139+
health.ok(string.format('Input height: %s', config.ui.input_height))
142140
end
143141

144142
health.ok('Configuration loaded successfully')

lua/opencode/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function M.setup(opts)
99
require('opencode.core').setup()
1010
config.setup(opts)
1111
api.setup()
12-
keymap.setup(config.get('keymap'))
12+
keymap.setup(config.keymap)
1313

1414
require('opencode.ui.completion').setup()
1515
require('opencode.event_manager').setup()

lua/opencode/keymap.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function M.toggle_permission_keymap(buf)
5454
local config = require('opencode.config')
5555
local api = require('opencode.api')
5656

57-
local permission_config = config.get().keymap.permission
57+
local permission_config = config.keymap.permission
5858
if not permission_config then
5959
return
6060
end

lua/opencode/state.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local config = require('opencode.config').get()
1+
local config = require('opencode.config')
22

33
---@class OpencodeWindowState
44
---@field input_win number|nil

lua/opencode/types.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
---@field defaults OpencodeConfig
115115
---@field values OpencodeConfig
116116
---@field setup fun(opts?: OpencodeConfig): nil
117-
---@field get fun(key?: string): any
118117
---@field get_key_for_function fun(scope: 'editor'|'input_window'|'output_window', function_name: string): string|nil
119118
---@field normalize_keymap fun(legacy_config: table, filter_functions?: table): table
120119

lua/opencode/ui/completion.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function M.on_complete(item)
6363
end
6464

6565
function M.get_completion_engine()
66-
local config = require('opencode.config').get()
66+
local config = require('opencode.config')
6767
local engine = config.preferred_completion
6868
if not engine then
6969
local ok_cmp = pcall(require, 'cmp')

0 commit comments

Comments
 (0)